Data structure-a java Implementation)
What is the meaning of the minimum length of the weighted path required by the Harman tree? In short, it is to multiply the path length (height-1) of all nodes by the weight of the node, and then ensure that the sum of these results is the smallest.
The most common application of the Homan tree is to solve the coding problem. Generally, we use ASCII encoding with a fixed length. For common characters, it is a waste of space to use a long length.
The following uses an example to construct a Harman encoding tree.
Set the character set S to {A, B, C, D, E, F}, AND THE OCCURRENCE FREQUENCY OF THE CHARACTERS W = {2, 3, 5, 7, 9, 12}. Perform the Harman encoding on the character set.
(1) build six Tree nodes based on the node values of the frequency tree and store them in a data set T.
(2) Select the minimum two frequencies in the frequency set W, and then add the result as the node value of the tree to build a new tree node. The two minimum values correspond to the Tree node, as the left and right children of the new node respectively. Delete the nodes corresponding to the two minimum values from T and put the new nodes in T.
(3) Repeat Step 1 until there is only one node left in step T, and the node is the required Harman tree.
In fact, it is easy to implement, but it is also a little troublesome. The following is implemented using java:
Tree node:
public class TreeNode
{ public TreeNode
leftNode; public TreeNode
rightNode; public T data;public TreeNode(T data){this.data=data;}}
Build a user-defined tree:
Public class TestTree {/** set S = {A, B, C, D, E, F}, W = {2, 3, 5, 7, 9, 12} */static HashMap
Map; public TestTree () {// TODO Auto-generated constructor stub} public static void main (String [] args) {Character [] character = {'A ', 'B', 'C', 'D', 'E', 'F'}; int [] weight = {2, 3, 5, 7, 9, 12 }; // The order or disorder are the same. map = new HashMap
(); For (int I = 0; I
> Nodes = new ArrayList
> (); For (int I = 0; I <weight. length; I ++) {nodes. add (new TreeNode
(Weight [I]);} while (true) {if (nodes. size () <= 1) break; // find the two smallest TreeNode
MinNode = nodes. get (0); TreeNode
SminNode = nodes. get (1); for (int I = 1; I <nodes. size (); I ++) {TreeNode
TempNode = nodes. get (I); if (minNode. data> = tempNode. data) {sminNode = minNode; minNode = tempNode ;}} nodes. remove (minNode); nodes. remove (sminNode); TreeNode
NewNode = new TreeNode
(MinNode. data + sminNode. data); newNode. leftNode = minNode; newNode. rightNode = sminNode; nodes. add (newNode);} TreeNode
HafmanTreeNode = nodes. get (0); getHalmanCode (hafmanTreeNode, "");} public static void getHalmanCode (TreeNode
HafmanTreeNode, String blank) {if (hafmanTreeNode = null) return; if (hafmanTreeNode. leftNode = null & hafmanTreeNode. rightNode = null) {System. out. println ("->" + getCharacter (hafmanTreeNode. data);} else {System. out. print ("0"); getHalmanCode (hafmanTreeNode. leftNode, blank + ""); System. out. print (blank + "1"); getHalmanCode (hafmanTreeNode. rightNode, blank + "") ;}// get the public static void getHalmanCode (TreeNode
HafmanTreeNode, Character character) {if (hafmanTreeNode = null) return; if (hafmanTreeNode. leftNode = null & hafmanTreeNode. rightNode = null) {if (getCharacter (hafmanTreeNode. data) = character) {System. out. print ("") ;}}// the public static Character getCharacter (int weight) {Set
> Set = map. entrySet (); for (Iterator
> Iterator = set. iterator (); iterator. hasNext ();) {Map. Entry
Entry = iterator. next (); if (entry. getValue () = weight) {map. remove (entry. getKey (); return entry. getKey () ;}} return null ;}}
Result:
D: 00
E: 01
A: 1000
B: 1001
C: 101
F: 11