C 模板實現哈夫曼樹

2021-08-07 10:57:29 字數 3468 閱讀 3846

哈夫曼樹(霍夫曼樹)又稱為最優樹.

1、路徑和路徑長度

在一棵樹中,從乙個結點往下可以達到的孩子或孫子結點之間的通路,稱為路徑。通路中分支的數目稱為路徑長度。若規定根結點的層數為1,則從根結點到第l層結點的路徑長

度為l-1。

2、結點的權及帶權路徑長度

若將樹中結點賦給乙個有著某種含義的數值,則這個數值稱為該結點的權。結點的帶權路徑長度為:從根結點到該結點之間的路徑長度與該結點的權的乘積。

3、樹的帶權路徑長度

給定n個權值作為n個葉子結點,構造一棵二叉樹,若帶權路徑長度達到最小,稱這樣的二叉樹為最優二叉樹,也稱為哈夫曼樹(huffman tree)。哈夫曼樹是帶權路徑長度最短的樹,權值較大的結點離根較近。

bintreenode.h

templateclass binarytree;

templatevoid huffman(type *, int, binarytree&);

templateclass bintreenode

bintreenode(type item,bintreenode*left=null,bintreenode*right=null)

:m_data(item),m_pleft(left),m_pright(right){}

void destroy()

} type getdata()

bintreenode*copy(const bintreenode*copy); //copy the node

private:

bintreenode*m_pleft,*m_pright;

type m_data;

};templatebintreenode* bintreenode::copy(const bintreenode*copy)

bintreenode*temp=new bintreenode(copy->m_data);

temp->m_pleft=copy(copy->m_pleft);

temp->m_pright=copy(copy->m_pright);

return temp;

}

binarytree.h

#include "bintreenode.h"

templatevoid huffman(type *, int, binarytree&);

templateclass binarytree

binarytree(type item)

binarytree(const binarytree©)

binarytree()

void destroy()

~binarytree()

binarytree& operator=(binarytreecopy); //evaluate node

friend void huffman(type *, int, binarytree&);

friend bool operator < (binarytree&l, binarytree& r);

friend bool operator > (binarytree&l, binarytree& r);

friend bool operator <= (binarytree&l, binarytree& r);

friend ostream& operator<< (ostream& ,binarytree&); //output the data

private:

bintreenode*m_proot;

void print(bintreenode*start,int n=0); //print the tree with the root of start

};templatebool operator

templatebool operator >(binarytree&l, binarytree&r)

templatebool operator <=(binarytree&l, binarytree&r)

templatevoid binarytree::print(bintreenode*start, int n)

templateostream& operator<

templatebinarytree& binarytree::operator=(binarytreecopy)

minheap.h
templateclass minheap

public:

bool insert(const type item);

bool deletemin(type &first);

private:

void adjust(const int start, const int end); //adjust the elements from start to end

private:

const int m_nmaxsize;

type *m_pheap;

int m_ncurrentsize;

};templatevoid minheap::adjust(const int start, const int end)

if(temp <= m_pheap[j])

else

} m_pheap[i] = temp;

}templateminheap::minheap(type heap, int n):m_nmaxsize(n)

}templatebool minheap::deletemin(type &first)

templatebool minheap::insert(const type item)

else

} m_pheap[j] = temp;

m_ncurrentsize++;

return 1;

}

huffman.h

#include "binarytree.h"

#include "minheap.h"

templatevoid huffman(type *elements, int n, binarytree&tree)

minheap> heap(node, n);

for (int i=0; igetdata() == second.m_proot->getdata())

else

heap.insert(tree);}}

main.cpp

#include using namespace std;

#include "huffman.h"

int main();

huffman(init,10,tree);

cout << tree;

tree.destroy();

return 0;

}

java實現哈夫曼樹模板

package template public class huffmantree int n huffmannode huffman 從huffman中找出兩個最小的權值,並儲存小標到m1和m2 int m1,m2 public void selectmin 找次小的 minw integer.m...

哈夫曼樹與哈夫曼編碼(C 實現)

1 對給定的n個權值構成n棵二叉樹的初始集合f 其中每棵二叉樹ti中只有乙個權值為wi的根結點,它的左右子樹均為空。2 在f中選取兩棵根結點權值最小的樹作為新構造的二叉樹的左右子樹,新二叉樹的根結點的權值為其左右子樹的根結點的權值之和。3 從f中刪除這兩棵樹,並把這棵新的二叉樹同樣以公升序排列加入到...

哈夫曼樹 C 實現

include using namespace std define maxbit 10 define maxvalue 10000 define maxleaf 100 define maxnode maxleaf 2 1 定義哈夫曼樹編碼型別 typedef structcodetype 定義哈...