What is Huffman tree?
Encode this question in binary form, equal length code. High frequency of non-debut coding, efficiency can be improved.
Convert percentile's exam results to a five-point score.
Decision Tree
The same thing, we use a decision tree, we can draw different efficiency
So ..... How do I search for trees more efficiently based on a node-less lookup frequency structure? This is the problem that Haffmanshu to solve.
The definition of Huffman tree:
weighted path Length (WPL): set two fork tree has n leaf nodes, each leaf node with weight wk, from the root node to the length of the leaf node is LK, then the sum of the length of each leaf node with the weighted path is
Huffman Tree is also called the optimal binary tree. Huffman tree is to let WPL value least.
The structure of Huffman tree
Huffman Tree thinking: is the weight from small to large to sort, each time the minimum weight of two lessons two fork tree merge, forming a new two fork tree,
Then the two nodes with the smallest weights are then together, that is, 3 3 4 5 together. The first 3 is the number of weights of 1 and 2. Finally formed a
Huffman Tree algorithm:
Idea, how to find the smallest two elements? is actually a heap problem, build a minimal heap and find the smallest two elements inside.
huffmantree Huffman (minheap H) {inti; Huffmantree T; Buildminheap (H); for(i =0; I < h->size; i++) {T= malloc (sizeof(structTreeNode));//Create a new node.T->left = Deletemin (H);//deletes a node as the left child node of the smallest heap. T->right = Deletemin (H);//deletes a node as the right child node of the smallest heap. T->weight = T->left->weight + t->right->Weight; Insert (H, T); //inserts a new t into the minimum heap} T=deletemin (H); returnT;}
The greatest time complexity is nlog2n
Several features of Huffman Tree:
- There is no node with a degree of 1. Because Huffman tree originally found two of the smallest to merge.
- The Huffman tree with n leaf nodes has a total of 2n-1.
- There are n N0.
- N1 Huffman tree is a node with no degree of 1.
- N2 N0 = n2 + 1;
- The left and right subtree of any non-leaf node of Huffman tree is still Huffman tree after exchange.
- For the same set of weights {W1,W2,W3}, is there a different structure of the two Huffman trees? Yes, there may be.
Huffman code
How to avoid the ambiguity?
Prefix code: The encoding of people and characters is not a prefix of another character encoding
- There can be no two-meaning encoding.
Use a binary tree for coding
How to construct such a non-debut code with Haffman
Data Structure--tree (bottom)--Huffman Tree and Huffman coding