Introduction to algorithms Chapter 1 greedy Algorithms

Source: Internet
Author: User
Harman tree

1. Basic Concepts
The Huffman tree, also known as the optimal binary tree, is a type of tree with the shortest length of the right path and is widely used.
Before discussing the hapman tree, you must first understand the concept of path and path length. The path between two nodes in the tree is composed of the branches from one node to another. The path length between two nodes is the number of branches on the path. The tree path length is the sum of the path lengths from the root node to each node.
A binary tree has n leaf nodes, each of which has a weight of W 1, W 2 ,...... W n, the path length from the root node to each leaf node is L1, L2 ...... ln, the length of the tree's weighted path is the product of the path length of each leaf and the leaf's weight value. It is generally recorded as WPL = l k. w k. In order to be intuitive, the weighted leaf nodes are drawn in a square shape, and other non-leaf nodes are still circular. See the three Binary Trees in Figure 6.21 and their authorization path length.

(A) WPL = 38 (B) WPL = 49 (c) WPL = 36 figure 6.21 binary tree with different weights
Note:
The three binary trees have the same number of leaf nodes, and their weights are the same, but their WPL weights vary in path length. Figure 6.21 (c) Minimum WPL. It is the Haman tree, the optimal tree. In a tree with the shortest length of a weight path. It is also called the Optimal Tree.
2. Construction of the Harman tree
Construct a user tree
For the known weights of a group of leaves, W 1, W 2..., W n
① Regard n leaf nodes as N trees (a binary tree with only one node) and regard them as a forest.
② In the forest, combine the two trees with the minimum and minimum weights into a tree. the weights of the root node are the sum of the weights of the two trees. There are n-1 trees in the forest.
③ Repeat Step 2 until there is only one tree in the forest. This tree is the Harman tree. The specific weights of A group (n = 4) are 2, 4, 5, and 8. The following describes the construction process:

Figure 6.22 construction process of the Harman tree
Figure 6.22 (a) is a forest with 4 small trees. Figure 6.22 (B) has 3 sub-trees in the forest, and figure 6.22 (c) has 2 left trees in the forest, figure 6.22 (d) There is only one tree in the forest, and this tree is the husky tree. I may ask whether the length of the weighted path is unique in the Heman tree composed of N leaves? It is unique. Is the tree unique? Not unique. This is because when we merge the two sub-trees with the minimum and minimum weights in the forest, it is not strictly limited to the left sub-tree and the right sub-tree. The practice in Figure 6.22 is to treat the left subtree with a smaller weight and the right subtree with a larger weight. If this is the case, the tree is different, but the WPL value is the same. In order to facilitate discussion and exchange, we recommend that the left sub-tree with a smaller weight be implemented, and the right sub-tree with a larger weight be implemented.

Code:

# Include <iostream> # include <string> # include <float. h> using namespace STD; # define N 6 // Number of leaf nodes # define m n * 2-1 // total number of nodes # define nil-1 // The NIL pointer is-1 struct node {int P; int left; int right; float weight; node (): p (NiL), left (NiL), right (NiL), weight (0 ){}}; struct haffuman_tree {node * array; string STR; haffuman_tree () // default constructor {array = new node [m]; // use an array of m to store the node STR = ""; // Save the encoding };}; // construct the void Huffman (haffuman_tree & T) {int I, j, P1, P2; // pointer: P1 points to the minimum value, P2 points to the next small value // small1 equals to the minimum weight, small2 equals to the next small value weight float small1, small2; for (I = N; I <m; I ++) // 0 .. n-1 has a weight. The goal is to convert n .. m-1 p1 = P2 = nil; small1 = small2 = flt_max; For (j = 0; j <I; j ++) // traverse all existing weights and select the minimum and minimum values {If (T. array [J]. P = nil) // only consider the case where the parent node is empty {If (T. array [J]. weight <small1) {small2 = small1; small1 = T. array [J]. weight; P2 = p1; P1 = J;} else if (T. array [J]. weight <small2) {small2 = T. array [J]. weight; P2 = J ;}} T. array [p 1]. P = T. array [P2]. P = I; T. array [I]. P = nil; T. array [I]. left = p1; T. array [I]. right = P2; T. array [I]. weight = small1 + small2 ;}// encode void huff_encode (haffuman_tree & T) {int X, Y; For (INT I = 0; I <n; I ++) // encode each leaf node {x = I; y = T. array [I]. p; while (y! = Nil) // if the parent node is not empty {// if the current node is the left child of the parent node, the code is 0if (T. array [Y]. left = x) {T. str. push_back ('0');} // if the current node is the right child of the parent node, It is encoded as 1 else if (T. array [Y]. right = x) {T. str. push_back ('1');} // continue to traverse x = y; y = T. array [Y]. p;} t. str. push_back (''); // delimiter} void main () {haffuman_tree t; // test data 5 9 12 13 16 45for (INT I = 0; I <N; I ++) {CIN> T. array [I]. weight;} Huffman (t); cout <"root node:" <Endl; For (INT I = 0; I <n; I ++) cout <t. array [I]. weight <"; cout <Endl <" non-root node: "<Endl; For (INT I = N; I <m; I ++) cout <t. array [I]. weight <"; cout <Endl; huff_encode (t); cout <" output Heman encoding: "<Endl; cout <t. STR <Endl ;}

Introduction to algorithms based on test data on p247:

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.