Given n weights as n leaf nodes, a binary tree is constructed, and if the weighted path length is minimized, it is called Huffman Tree (Huffman tree). Huffman tree is a tree with the shortest length of the weighted path, and the nodes with larger weights are closer to the root.
Structure
Assuming there are n weights, the Huffman tree is constructed with n leaf nodes. N weights are set to W1, W2 、...、 WN respectively, then the structure rules of Huffman tree are:
(1) W1, W2 、..., wn as a forest with n trees (each tree has only one node);
(2) The tree merging of the minimum weights of the two root nodes in the forest is used as the left and right subtree of a new tree, and the root node weights of the new tree are the sum of their left and right subtree node weights;
(3) Remove the selected two trees from the forest and add the new tree to the forest;
(4) Repeat (2), (3) step, until there is only one tree left in the forest, the tree is the Huffman tree obtainedThe topic describes Huffman tree, the first line to enter a number n, indicating the number of leaf nodes. It is necessary to use these leaf nodes to generate Huffman tree, according to the concept of Huffman tree, these nodes have the right value, that is, weight, the topic needs to output all nodes of the value and weight of the sum of the product. Input Description:
Enter more than one set of data. Enter a number n for the first row of each group, and then enter n leaf nodes (the leaf node weights do not exceed 100,2<=n<=1000).
Output Description:
The output weight value.
Example 1 input
5 1 2 2 5 9
Output
37
Problem-solving ideas: To do this is to start thinking that each time you select two data to add a quick sort immediately.
This method is possible for small data, but the abnormal big data for Pat is running out of time.
1#include <stdio.h>2#include <stdlib.h>3 4 intcmpConst void*a,Const void*b)5 {6 return*(int*) A-* (int*) b;//sort from small to large7 }8 intnum[1002];9 intMain ()Ten { One intN; A intans; - inti; - while(SCANF ("%d", &n)! =EOF) the { - for(i=0; i<n; i++) - { -scanf"%d",&num[i]); + } -Ans =0; + for(i=0; i<n-1; i++) A { at //Remember, this is i<n-1, and the last number is the answer. -Qsort (&num[i],n-i,sizeof(num[0]), CMP); -Ans + = num[i]+ num[i+1]; -num[i+1] = num[i]+ num[i+1]; - } -printf"%d\n", ans); in } - to return 0; + -}
The above approach is easy to time out, the following use of C + + using priority queue to build a small top heap to achieve Huffman tree speed faster, the truth is the same, changed a method of implementation.
1#include <stdio.h>2#include <queue>3 using namespacestd;4priority_queue<int, vector<int>,greater<int> > Q;//build a small top pile5 //priority_queue<int> Q; //set up a large top heap by default6 7 intMain ()8 {9 intN;Ten intANS,TEMP1,TEMP2; One inti; A - while(SCANF ("%d", &n)! =EOF) - { the while(Q.empty () = =false) Q.pop ();//clear the elements in the heap - - for(i=1; i<=n; i++) - { +scanf"%d",&temp1); - Q.push (TEMP1); + } AAns =0; at while(Q.size () >1)//when the number of elements in the heap is greater than 1 - { -Temp1 =q.top (); - Q.pop (); -Temp2 =q.top (); -Q.pop ();//Take out two of the smallest elements in the heap inAns + = temp1+Temp2; -Q.push (TEMP1+TEMP2);//put the parental node weights back into the heap. to } +printf"%d\n", ans); - } the return 0; *}
Huffman Tree (use of C + + priority Queues)