Huffman Tree in Java

Source: Internet
Author: User

1  PackageCom.ietree.basic.datastructure.tree;2 3 ImportJava.util.ArrayDeque;4 Importjava.util.ArrayList;5 Importjava.util.List;6 ImportJava.util.Queue;7 8 /**9 * Created by IetreeTen * 2017/5/1 One  */ A  Public classHuffmantree { -  -      Public Static classNode<e> { the E data; -         Doubleweight; - Node leftchild; - Node rightchild; +  -          PublicNode (E data,Doubleweight) { +              This. data =data; A              This. Weight =weight; at         } -  -          PublicString toString () { -             return"Node[data=" + Data + ", weight=" + weight + "]"; -         } -     } in  -      Public Static voidMain (string[] args) { to  +list<node> nodes =NewArraylist<node>(); -Nodes.Add (NewNode ("A", 40)); theNodes.Add (NewNode ("B", 8)); *Nodes.Add (NewNode ("C", 10)); $Nodes.Add (NewNode ("D", 30));Panax NotoginsengNodes.Add (NewNode ("E", 10)); -Nodes.Add (NewNode ("F", 2)); the  +Node root =huffmantree.create (nodes); A System.out.println (Breadthfirst (root)); the  +     } -  $     /** $ * Construction Huffman Tree -      * -      * @paramNodes Node Collection the      * @returnThe root node of the Huffman tree constructed -      */Wuyi     Private StaticNode Create (list<node>nodes) { the  -         //as long as there are more than 2 nodes in the nodes array Wu          while(Nodes.size () > 1) { - QuickSort (nodes); About             //get the two nodes with the least weighted value $Node left = Nodes.get (Nodes.size ()-1); -Node right = Nodes.get (Nodes.size ()-2); -             //To generate a new node, the weight of the new node is the sum of the weights of two child nodes -Node parent =NewNode (NULL, Left.weight +right.weight); A             //make the new node the parent of the two nodes with the least weight +Parent.leftchild =Left ; theParent.rightchild =Right ; -             //Delete the two nodes with the lowest weighted value $Nodes.remove (Nodes.size ()-1); theNodes.remove (Nodes.size ()-1); the             //to add a newly generated parent node to the collection the Nodes.Add (parent); the         } -         //returns the only node in the Nodes collection, which is the root node in         returnNodes.get (0); the     } the  About     //swaps the elements at the I and J indices of the specified array the     Private Static voidSwap (list<node> nodes,intIintj) { the Node tmp; theTMP =Nodes.get (i); + Nodes.set (I, Nodes.get (j)); - Nodes.set (J, TMP); the     }Bayi  the     //implement a quick sort algorithm for sorting nodes the     Private Static voidSubsort (list<node> nodes,intStartintend) { -  -         //need to sort the         if(Start <end) { the             //take the first element as a cutoff value theNode base =nodes.get (start); the             //I search from the left, searching for an index of an element greater than the cutoff value -             inti =start; the             //J Search from the right, search for the index of the element less than the cutoff value the             intj = End-1; the              while(true) {94                 //The index of the element that is greater than the cutoff value is found, or I is already at end the                  while(I < end && Nodes.get (++i). Weight >=base.weight); the                 //Find the index of the element that is less than the cutoff value, or J is at start the                  while(J > Start && nodes.get (--j). Weight <=base.weight);98                 if(I <j) { About swap (nodes, I, j); -}Else {101                      Break;102                 }103             }104 Swap (nodes, start, j); the             //recursive left sub-tree sequence106Subsort (nodes, start, j-1);107             //Recursive right sub-tree sequence108Subsort (nodes, J + 1, end);109         } the     }111  the      Public Static voidQuickSort (list<node>nodes) {113Subsort (nodes, 0, Nodes.size ()-1); the     } the  the     //breadth-First traversal117      Public StaticList<node>Breadthfirst (Node root) {118 119queue<node> queue =NewArraydeque<node>(); -list<node> list =NewArraylist<node>();121         if(Root! =NULL) {122             //put the root element into the queue123 Queue.offer (root);124         } the          while(!Queue.isempty ()) {126             //Add the "tail" element of the queue to the list127 List.add (Queue.peek ()); -Node p =Queue.poll ();129             //If the left Dial hand node is not NULL, add it to the queue the             if(P.leftchild! =NULL) {131 Queue.offer (p.leftchild); the             }133             //If the right child node is not NULL, add it to the queue134             if(P.rightchild! =NULL) {135 Queue.offer (p.rightchild);136             }137         }138         returnlist;139  $     }141 142}

Huffman Tree in Java

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.