Then the method of sorting:
PackageHuffman binary Tree 820;/*** Data field in the node, one character, one number;@authorAdministrator **/ Public classData {PrivateString C;//characters; Private intNumber//the number of occurrences of the character; PublicData (String C,intNumber) {//Construction Method This. c=C; This. number=Number ; } PublicData () {//Construction Method } PublicString GetC () {returnC; } Public voidsetc (String c) { This. C =C; } Public intGetNumber () {returnNumber ; } Public voidSetnumber (intNumber ) { This. Number =Number ; } }
Data
PackageHuffman binary Tree 820;/*** One node class, each node has left node, right node, and data field;@authorYuanmo; **/ Public classNode {PrivateData data;//data fields, data fields are stored in two data, so define a class to represent; PublicNode left;//left dial hand node; PublicNode right;//right child node; /*** Rewrite the construction method; data is obtained in the construction method ;*/ PublicNode (data data) { This. data =data; } /*** Because the data is defined as private, the set and get methods of data are implemented;*/ PublicData GetData () {returndata; } Public voidsetData (data data) { This. data =data; } }
Node
PackageHuffman binary Tree 820; Public classHfmtree {/*** Create Huffman number; *@paramleft child node of Huffman tree;@paramright, Huffman-ripe, child node *@return */ PublicNode creathfmtree (node-left, node-right) {Data data=NewData (); String C= Left.getdata (). GetC () + Right.getdata (). GetC ();//get the new character C; intNumber = Left.getdata (). GetNumber () + Right.getdata (). GetNumber ();//get a new number;Data.setc (c);//give C to the new dataData.setnumber (number);//give the number to the new dataNode Fathernode =NewNode (data);//Create a parent node based on the data field;Fathernode.left = left;//leave left as the child of the parent nodeFathernode.right = right;//set left as the right child node of the parent node returnFathernode;//Pass the parent node back; } /*** Print Huffman tree, using pre-order traversal; *@paramnode to print the current nodes;*/ Public voidPrinthfmtree (node node) {if(Node! =NULL) {String C=node.getdata (). GetC (); intNumber=node.getdata (). GetNumber (); System.out.print ("+" character: "+ C +" Frequency: "+Number ); System.out.println (); if(Node.left! =NULL) {//when not null, the method output is called recursively;Printhfmtree (Node.left); } if(Node.right! =NULL) {//when not null, the method output is called recursively;Printhfmtree (node.right); } } }}
Hfmtree
PackageHuffman binary Tree 820;Importjava.util.ArrayList; Public classCreathfmtree { Public Static voidMain (string[] args) {ArrayList<Node> list =NewArraylist<node> ();//instantiate an array queue for storing data;List.add (NewNode (NewData ("B", 3))); List.add (NewNode (NewData ("C", 7))); List.add (NewNode (NewData ("D", 10))); List.add (NewNode (NewData ("a", 15))); //instantiation of a Huffman tree object;Hfmtree HFM =NewHfmtree (); //to create Huffman based on the nodes in the list array queue; for(inti = 0; I < list.size (); i++) {sort (list);//sorting the array queue; /*because every time we take it, we delete it, and the number of array queues changes after the deletion, which is the formation of a new array * so we need to start from the beginning, that is, the first number of the new array;*/I= 0; Node Left= List.get (i);//Get left dial hand node;List.remove (i);//Delete the node in the array queueNode right = List.get (i);//get right child node;List.remove (i);//Delete the node in the array queueNode Father= Hfm.creathfmtree (left, right);//get the parent node;List.add (father);//Add the parent node to the array;} Node Root= List.get (0);//gets the root node;Hfm.printhfmtree (root); } /*** The array queue is sorted, and the bubble sort method is used;@paramList of array queues to be sorted;*/ Public Static voidSort (arraylist<node>list) { for(intj = 0; J < List.size (); J + +) { for(inti = 0; I < List.size ()-1; i++) {Node C=List.get (i); intz =i; Z++; Node x=List.get (z); if(C.getdata (). GetNumber () >x.getdata (). GetNumber ()) {List.remove (i); List.add (i, x); List.remove (z); List.add (z, c); } } } }}
Creathfmtree
Huffman Two-fork Tree