Harman tree, Harman

Source: Internet
Author: User

Harman tree, Harman
Harman tree

Given n weights as n leaf nodes, construct a binary tree. If the length of the weighted path reaches the minimum, such a binary tree is called the optimal binary tree, it is also called the Huffman tree ). A tree with the shortest length of a weight path, a node with a large weight value is closer to the root. The weighted length of a node is defined as follows: the weight of a node * The path length from the root node to the node. The Length of the Weighted Path (Weighted Path Length of Tree, which is abbreviated as WPL) indicates the Weighted Length and of all nodes. The Harman tree is the smallest tree in WPL and is full of Binary Trees. Its constructor method is the user's method. Built like this:

Assume that there are n weights, then the constructed Harman tree has n leaf nodes. Set n weights to w1, w2 ,... And wn, then the construction rule of the Harman tree is: (from Baidu encyclopedia)

User ID

When talking about the user tree, we have to mention the user's coding. Huffman Coding is a type of Coding method, which is a type of variable-length Coding (VLC. In 1952, Huffman proposed a encoding method. This method is based entirely on the probability of occurrence of characters to construct a code word with the shortest average length of different character headers, which is sometimes called the best encoding, generally, it is called the Huffman encoding (also known as the Hoffman encoding ). The structure of the tree is relatively simple: first build the Harman tree, mark the path of the Left subtree as 0, and mark the path of the right subtree as 1. The Harman encoding of the leaf node is the 0 and 1 sequences from the root node to the simple path of the leaf node.

The following shows the c ++ code used to construct the Harman tree and Harman encoding:

Code

// Huffman tree # include <iostream> # include <iomanip> # include <stack> using namespace std; // max weight value const int MAXWEIGHT = 100; // Huffman node typedef struct {int weight; // node weight int parent; // parent node subscript int lchild; // left child subscript int rchild; // right child subscript} HuffNode, * HuffTree; // HuffCodetypedef struct {// weight int weight; // stack storage code stack <char> code;} HuffCode; /* Create a Huffman tree */HuffNode * buildHuffTree (int weight [], int n) {if (! Weight | n <1) return NULL;/* stores the Huffman tree in sequence. The Huffman tree is a full binary tree. n indicates the number of leaf nodes, and the number of internal nodes is n-1, there are 2 * n-1 nodes */int m = 2 * n-1; // total number of nodes HuffNode * tree = new HuffNode [m]; // initialize the Huffman tree int I, j; for (I = 0; I <m; I ++) {// store the initial node in the front of the array if (I <n) tree [I]. weight = weight [I]; tree [I]. parent = tree [I]. lchild = tree [I]. rchild =-1;}/* build only n-1 cycles. w1 is the current minimum weight, p1 is its subscript, and w2 is the current minimum weight, p2 is its subscript */int w1, w2, p1, p2; for (I = n; I <m; I ++) {// It must be initialized before each loop Value and subscript w1 = w2 = MAXWEIGHT; p1 = p2 = 0; // find the smallest and secondary least weight nodes for (j = 0; j <I; j ++) {// The parent node subscript is-1, indicating that the node is not using if (tree [j]. parent =-1) {if (tree [j]. weight <w1) {w2 = w1; p2 = p1; w1 = tree [j]. weight; p1 = j;} else if (tree [j]. weight <w2) {w2 = tree [j]. weight; p2 = j ;}}// place the generated node into the position itree [p1]. parent = tree [p2]. parent = I; tree [I]. weight = tree [p1]. weight + tree [p2]. weight; tree [I]. lchild = p1; tree [I]. rchild = p2;} return Tree;}/* construct Huffman Encoding Based on the Huffman tree */HuffCode * huffCode (HuffTree tree, int n) {HuffCode * Code = new HuffCode [n]; int I, child, parent; for (I = 0; I <n; I ++) {stack <char> stack; child = I; parent = tree [child]. parent; while (parent! =-1) {// The left Child Branch is 1if (tree [parent]. lchild = child) stack. push ('0'); else // The branch of the right child is 1 stack. push ('1'); child = parent; parent = tree [child]. parent;} Code [I]. weight = tree [I]. weight; Code [I]. code = stack;} return Code;} // print the Huffman tree void printHuffTree (HuffTree tree, int n) {int I; cout. setf (ios: left); for (I = 0; I <n; I ++) {cout <setw (2) <I <"weight: "<setw (3) <tree [I]. weight <"FATHER:" <setw (3) <tree [I]. Parent <"left child:" <setw (3) <tree [I]. lchild <"right child:" <setw (3) <tree [I]. rchild <endl ;}int main () {cout <"*** Huffman tree *** by David ***" <endl; int n; cout <"Number of input weights"; while (cin> n & n <1) cout <"error, re-input "; int * weight = new int [n]; cout <"input weight Sequence"; int I = 0; while (I <n) cin> weight [I ++]; cout <"create Huffman tree" <endl; HuffTree tree = buildHuffTree (weight, n); printHuffTree (tree, 2 * n- 1); cout <endl; cout <"Huffman encoding" <endl; HuffCode * code = huffCode (tree, n); for (I = 0; I <n; I ++) {cout <"Weight:" <setw (3) <code [I]. weight <"Huffman encoding:"; stack <char> stack = code [I]. code; while (! Stack. empty () {cout <stack. top (); stack. pop () ;}cout <endl ;}// release space delete [] weight; delete [] tree; delete [] code; system ("pause "); return 0 ;}

Run



Reprint please indicate the source, this article address: http://blog.csdn.net/zhangxiangdavaid/article/details/37696533


If this is helpful, try again!


Column Directory: Data Structure and algorithm directory



Harman tree

Such a definite algorithm must be unique. The number is as follows:

1.00
/\
/0 \ 1
0.44 0.56
/\/\
/0/1/0/1
0.21 0.23 0.27 0.29
/\
/0 \ 1
0.13 0.16
/\
/0 \ 1
0.07 0.09
Encoding:
0.07: 1110
0.09: 1111
0.13: 110
0.21: 00
0.23: 01
0.27: 10
No prefix for anyone. OK.

For additional questions:
No. The two smallest values are always used as the leaves when a user generates a Harman tree ..

For supplementary question 002: from the bottom up of the tree above, the minimum two values of ownership are 0.07 and 0.09, and the sum is 0.16;
The original weight table is: 0.13, 0.16 (instead of 0.07 and 0.09), 0.21, 0.23, 0.27;
Now the minimum two values are 0.13, 0.16, and the sum is 0.29;
The previous weight table became: 0.21, 0.23, 0.27, 0.29 (instead of 0.13 and 0.16 );
Now the minimum two values are 0.21, 0.23, and the sum is 0.44;
The previous table of weights became: 0.27, 0.29, 0.44;
The next step is the same.

Harman tree

Such a definite algorithm must be unique. The number is as follows:

1.00
/\
/0 \ 1
0.44 0.56
/\/\
/0/1/0/1
0.21 0.23 0.27 0.29
/\
/0 \ 1
0.13 0.16
/\
/0 \ 1
0.07 0.09
Encoding:
0.07: 1110
0.09: 1111
0.13: 110
0.21: 00
0.23: 01
0.27: 10
No prefix for anyone. OK.

For additional questions:
No. The two smallest values are always used as the leaves when a user generates a Harman tree ..

For supplementary question 002: from the bottom up of the tree above, the minimum two values of ownership are 0.07 and 0.09, and the sum is 0.16;
The original weight table is: 0.13, 0.16 (instead of 0.07 and 0.09), 0.21, 0.23, 0.27;
Now the minimum two values are 0.13, 0.16, and the sum is 0.29;
The previous weight table became: 0.21, 0.23, 0.27, 0.29 (instead of 0.13 and 0.16 );
Now the minimum two values are 0.21, 0.23, and the sum is 0.44;
The previous table of weights became: 0.27, 0.29, 0.44;
The next step is the same.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.