Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1.   public static HTree createHuffmanTree(HLinkedList list){
  2.     HTree tree1 =new HTree();
  3.     HLinkedList listInOrder=getSortedLinkedList(list);
  4.     final int j=listInOrder.getSize()-1;
  5.     for(int i=0;i<j;i++){
  6.       HTreeNode leftChild=listInOrder.getHeadNode();
  7.       leftChild.next=null;
  8.       leftChild.displayLink();
  9.       HTreeNode rightChild=listInOrder.getHeadNode();
  10.       rightChild.next=null;//and also make sure .next points to null
  11.       rightChild.displayLink();
  12.       HTreeNode parent=new HTreeNode('\u0000',leftChild.occur+rightChild.occur,null,leftChild,rightChild,null);
  13.       listInOrder.insertIntoPosition(parent, 0);//insert parent node into the linkedlist
  14.       listInOrder=getSortedLinkedList(listInOrder);//sort the linkedlist and repeat the cycle.
  15.       if(j-1 == i){
  16.         tree1.makeRoot(parent);//make sure root is the final parent node
  17.         break;
  18.       }
  19.     }
  20.     return tree1;
  21.   }  
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement