Tree path length the tree path length is the sum of the path lengths from the root to each node in the tree. In a binary tree with the same number of nodes, the path length of the Complete Binary Tree is the shortest. Weight of the weighted path length of tree (wpl) node of the tree: in some applications, assign a meaningful real number to the node in the tree and the length of the weighted path of the node: the length of the path from the node to the root of the tree and the length of the weighted path (wpl) of the product tree on the node ): it is defined as the sum of the weighted path lengths of all nodes in the tree. The optimal binary tree has the right to w1, w2 ,..., among all the Binary Trees composed of n leaf nodes of wn, the binary tree with the minimum length of the weight path (that is, the minimum cost) becomes the optimal binary tree. Note: When the weights on the leaves are the same, the full binary tree must be the optimal binary tree. Otherwise, the full binary tree is not necessarily the optimal binary tree. In the optimal binary tree, the leaf node with a higher weight is not unique in the form of the nearest optimal binary tree from the root. The wpl minimum construction of the optimal binary tree Harman algorithm is based on the given n weights w1, w2 ,..., wn: Forest F = {T1, T2 ,.., tn}, where each binary tree Ti has only one root node with the weight of wi, and its left and right subtree are empty. In forest F, select the two trees with the smallest root node weight (when there are more than two trees, you can choose two from them), and name the two trees and a new one, to ensure that the new tree is still a binary tree, you need to add a new node as the root of the new tree and use the selected two trees as the left and right children of the new tree, the sum of the weights of the two children is used as the weight value of the new root tree to repeat 2 to the New Forest F until there is only one tree left in the forest F. Note that there are n binary trees in the initial forest. Each tree has an isolated node. They are both the root and the Harman tree with n leaf nodes that need to be n-1 times, generate n-1 new nodes. A total of 2n-1 nodes are obtained. The Harman tree is a strict Binary Tree, and there is no branch node with a degree of 1. The Harman tree storage structure [cpp] # include <stdio. h> # include <stdlib. h> # include <string. h> # define MAX_SIZE 100 # define MIN_NUM 1000000 struct hfmcode {int weight; int lchild; int rchild; int parent ;}; note: the lower bound of the array is 0, therefore,-1 indicates a null pointer. When the lchild, rchild, and parent values of a node in the tree are not-1, they are the subscript of the left, right, and parent nodes of the node in the vector. The role of the parent field: it is easier to search for parent nodes. The second is to identify whether the parent value is-1 to distinguish between the root node and the non-root node. Implement code (1) initialization, T [0 .. the three pointers in the 2n-1 nodes in the s-1] are set to null (=-1) and the weight is set to 0. [cpp] void initHuffmantree (struct hfmcode * tree, int len) {int I; for (I = 0; I <len; I ++) {tree [I]. weight = 0; tree [I]. lchild =-1; tree [I]. rchild =-1; tree [I]. parent =-1 ;}}( 2) input, read the weight of n leaves stored in the first n components of the vector (that is, T [0 .. n-1. [Cpp] int main () {int n, m, I; struct hfmcode hfmtree [MAX_SIZE]; while (scanf ("% d", & n )! = EOF) {/* Total Number of knots in the Harman tree */m = 2 * n-1;/* initialize the Harman tree */initHuffmantree (hfmtree, m ); /* assign permissions */for (I = 0; I <n; I ++) {scanf ("% d", & hfmtree [I]. weight);}/* construct a Harman tree */createHuffmantree (hfmtree, n, m); printf ("% d \ n", hfmtree [m-1]. weight);} return 0;} (3) Merge trees in the forest for n-1 times, the new node is sequentially inserted into the I-th component of the vector T (n <= I <= s-1 ). merge each time in two steps: In the current forest T [0 .. in all the nodes of the I-1, T [p1] and T [p2] are selected as the merging objects, where 0 <= p1, p2 <= I-1 takes the left and right sides of the two trees with the root T [p1] and T [p2] The tree is merged into a new tree, and the new root is a new node T [I]. the specific operation is: Set the parent of T [p1] and T [p2] to I, set the lchild and rchild of T [I] to the sum of the weights of T [p1] and T [p2] of the new node T [I. After merging, T [p1] and T [p2] are no longer the root in the current forest, because their parent pointers all point to T [I], therefore, the next merge will not be selected as the merged object. [Cpp] void createHuffmantree (struct hfmcode * tree, int n, int m) {int m1, m2, I, loc1, loc2, k; for (I = n; I <m; I ++) {/* minimum initial value, minimum initial value */m1 = m2 = MIN_NUM; loc1 = loc2 =-1; /* search for the smallest two trees with the minimum weight in a node that has not yet constructed a binary tree */for (k = 0; k <I; k ++) {if (tree [k]. parent =-1) {if (tree [k]. weight <m1) {m2 = m1; loc2 = loc1; m1 = tree [k]. weight; loc1 = k;} else if (tree [k]. weight <m2) {m2 = tree [k]. weight; loc2 = k ;}} }/* Modify the parent nodes of two small weight nodes */tree [loc1]. parent = I; tree [loc2]. parent = I;/* modify the weight of the parent */tree [I]. weight = tree [loc1]. weight + tree [loc2]. weight;/* modify the child of the parent */tree [I]. lchild = loc1; tree [I]. rchild = loc2 ;}} the process of coding and decoding data is encoded. Convert each character in the file into a unique binary string and decompress the data to decode it. To encode a character prefix corresponding to a binary string conversion character set, the encoding of any character in the character set is not the prefix of the encoding of other characters, this encoding becomes the optimal prefix encoding by means of the average length of the prefix encoding or the smallest prefix of the total length of the file. The optimal prefix encoding also has the best compression effect on the file.