Given n weights as the leaf node of n, construct a binary tree, if the length of the belt weight path is minimal, the two-fork tree is called the optimal binary tree , also known as Huffman tree (Huffman trees). Huffman tree is a tree with the shortest length of the weighted path, and the nodes with larger weights are closer to the root.
1. Path and path lengthA pathway between a child or grandson node that can be reached from a node down a tree, called
Path。 The number of branches in the pathway is called
Path Length。 If the number of layers of the root node is 1, then from the root node to the first L-level node
Path Length
degreeFor L-1.
2. The right of the node and the length of the path with the rightIf you assign a node in the tree to a value that has a certain meaning, this value is called the
the right to the knot.。 Node's
Weighted path lengthis the product of the length of the path from the root node to the node and the right of the node.
3, the length of the tree with the right pathThe weighted path length of the tree is defined as the sum of the weighted path lengths of all leaf nodes, recorded as WPL.
The Huffman algorithm for constructing the optimal binary tree is as follows:
(1) According to the given n weights {wi,w2,..., wn}, constitute the set of n binary tree T2,..., Tn}, where each binary tree is only a root node with a weight of%, and its left and right subtree are empty.
(2) in F, select two tree with the lowest weight of two tree as the left and right sub-tree to construct a new two-fork tree, the weight value of the root node of the new structure two fork tree is the sum of the weights of its left and right subtree nodes.
(3) Remove the two trees from F and add the newly obtained two forks to F.
(4) Repeat (2), (3) until the F contains only one tree. This tree is the optimal binary tree (Huffman tree).
For example, given the weight set {8,5,2,6} according to the weight set given in the question, the process of constructing Huffman tree is shown.
Data Structure two fork Tree