赫夫曼树
经过四个小时的不懈努力,终于制作出了数据结构中的赫夫曼树,当然,还没有经过严密的测试,不过,我还是想放出来,如果有懂的,看到了我的bug,希望可以帮我指出一下,非常感谢!
//Huffman tree is constructed using arrays #include<iostream> using namespace std; int m, n; //Vitural type typedef char ElemType; //Node class of Huffman tree class HuffmanTreeNode { public: int rightnode; int leftnode; int parent; ElemType content; int weight; HuffmanTreeNode(); }; //The constructor of the class of the node of Huffman tree node HuffmanTreeNode::HuffmanTreeNode() { rightnode = -1; leftnode = -1; parent = -1; weight = -1; content = '-1'; } //The class of Huffman tree class HuffmanTree { public: HuffmanTreeNode * head; int length; int root; void CreatHuffmanTree(); void PreSequenceTraversal(int); void MediumSequenceTraversal(int); void PrintHuffmanTree(); void FindMin(int,int&,int&); HuffmanTree(); ~HuffmanTree(); }; //The constructor of the class of Huffman tree HuffmanTree::HuffmanTree() { head = NULL; length = 0; } //The destructor of the class of Huffman tree HuffmanTree::~HuffmanTree() { delete[]head; } //Creat Huffman tree void HuffmanTree::CreatHuffmanTree() { int min1=-1, min2=-1; cout << "Plearse enter how many nodes are in total:"; cin >> n; length = n; m = 2 * n-1 ; head = new HuffmanTreeNode[m]; //Enter the content and weight for each node for (int i = 0; i < n; i++) { cout << "Enter the content " << i + 1 << ":"; cin >> head[i].content; cout << "Enter the weight" << i + 1 << ":"; cin >> head[i].weight; } //Creat Huffman tree for (int i = n; i < m; i++) { FindMin(i, min1, min2); head[min1].parent = head[min2].parent = i; head[i].leftnode = min1; head[i].rightnode = min2; head[i].weight = head[min1].weight + head[min2].weight; } root = m-1; } //Find the two nodes with smallest weights void HuffmanTree::FindMin(int n,int&m1,int&m2) { for (int i = 0; i < n; i++) { if (head[i].parent == -1) { m1 = i; break; } } for (int i = 0; i < n; i++) { if ((head[i].weight < head[m1].weight) && head[i].parent == -1) { m1 = i; } } for (int i = 0; i < n; i++) { if (i != m1 && head[i].parent == -1) { m2 = i; break; } } for (int i = 0; i < n; i++) { if ((i != m1) && (head[i].weight < head[m2].weight) && (head[i].parent == -1)) { m2 = i; } } } //Print Huffman tree void HuffmanTree::PrintHuffmanTree() { cout << " content:"; for (int i = 0; i < length; i++) cout << head[i].content << " "; cout << endl; cout << " leftnode:"; for (int i = 0; i < length; i++) cout << head[i].leftnode << " "; cout << endl; cout << "rightnode:"; for (int i = 0; i < length; i++) cout << head[i].rightnode << " "; cout << endl; cout << " weight:"; for (int i = 0; i < length; i++) cout << head[i].weight << " "; cout << endl; cout << " parent:"; for (int i = 0; i < length; i++) cout << head[i].parent << " "; cout << endl; } //Medium sequence traversal void HuffmanTree::MediumSequenceTraversal(int root) { if (root == -1) return; if(head[root].leftnode!=-1) MediumSequenceTraversal(head[root].leftnode); cout << head[root].content << " "; if(head[root].rightnode!=-1) MediumSequenceTraversal(head[root].rightnode); } //Pre-sequence traversal void HuffmanTree::PreSequenceTraversal(int root) { if (root == -1) return; cout << head[root].content << " "; if (head[root].leftnode != -1) MediumSequenceTraversal(head[root].leftnode); if (head[root].rightnode != -1) MediumSequenceTraversal(head[root].rightnode); } //The main function int main() { HuffmanTree *a = new HuffmanTree; a->CreatHuffmanTree(); cout << "(MediumSequenceTraversal)The content is:" << endl; a->MediumSequenceTraversal(a->root); cout << endl; a->PreSequenceTraversal(a->root); system("pause"); return 0; }