Seeking the length of the path of Huffman with weight

Source: Internet
Author: User

"Problem description"
Two lines are known to enter a positive integer, the second line is separated by a space between the integers, please establish a Huffman tree, to enter the number for the leaf node, to find the Huffman tree with the right path length.

"Input Form"
First, enter the number of positive integers, and then the next line of positive integers, representing the leaf node, the number of positive integer not more than 1000

"Output Form"
Output the corresponding weight value

"Sample Input"

5

4 5 6) 7 8

"Sample Output"

69

About Huffman Tree--

1. Path length

A branch from one node in the tree to another node forms a path between two nodes, and the number of branches on the path is called the path length.


Figure 1 Path length from the root node to the D node is 4


1, the path length of the tree

The path length is the sum of the length of the path from the tree root to each node.


Figure 2 The path length of the tree is 1+1+2+2+3+3+4+4=20


1, Huffman Tree:

The WPL (Weighted path length) is the smallest two-tree, also known as the optimal two-tree.

Example: Wpl=1*5 + 2*15 + 3*40 + 4*30 + 4*10= 315

First of all, we can get the algorithm description of constructing Huffman tree through the steps just now.

1, according to the given n weights {w[1],w[2],..., W[n]} constitute a set of n binary tree f={t[1],t[2],... T[n]}, where each binary tree t[i] has only one root node with a weighted w[i], and the left and right subtrees are empty.


2, in F, select two root node of the minimum weight of the tree as the left and right sub-tree to construct a new. Two fork tree, and the weight of the root node of the new two-fork tree is the sum of the weights of the root nodes on the left and right sub-tree.


3. Delete the two trees in F and add the newly obtained two tree to F.


4 Repeat steps 2 and 3 until F contains only one tree. This tree is the Huffman tree.

To illustrate this algorithm with an example







Fig. 3 The construction process of Huffman tree




Figure 4 Final Result


Then the minimum weighted path length can be calculated from the Huffman tree above

WPL = 1*9 + 2*5 + 3*2 + 4*1 + 4*2 =37

In addition, there can be another method, combined with the algorithm description carefully observed that the least-weighted path length is a non-leaf node, i.e.

wpl= 19 + 10 +5 +3=37


As for the correctness of the algorithm, all of a sudden can not imagine what good way to prove, but should be logically deduced.


So to achieve this program, by the above algorithm description diagram we already know almost, mainly divided into three steps:

First, sort until only one number in the array exits

Second, the minimum two numbers plus, that is, non-leaf nodes, accumulated into the accumulator

Add up the minimum two numbers as a new value in the array, remove the minimum two values, and jump back to the first step


#include <stdio.h> #include <stdlib.h>                          //qsort () #define N 1010int Rising (const void *a, const void *b)    {    return * (int*) A-* (int*) b;} int main () {    int leaf[n] = {0}, N, I, sum = 0;    scanf ("%d", &n);    for (i=0; i<n; i++)        scanf ("%d", leaf+i);    for (i=0; i<n-1; i++)    {        qsort (leaf+i, n-i, sizeof (leaf[0]), rising);    Sorting and culling of used leaf nodes        leaf[i+1] + = Leaf[i];     Merge two smallest leaf nodes into a new node (placed in leaf[i+1])        sum + = leaf[i+1];         Total path length = sum of all non-leaf nodes    printf ("%d\n", sum);        return 0;}

Seeking the length of the path of Huffman with weight

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.