Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static HTree createHuffmanTree(HLinkedList list){
- HTree tree1 =new HTree();
- HLinkedList listInOrder=getSortedLinkedList(list);
- final int j=listInOrder.getSize()-1;
- for(int i=0;i<j;i++){
- HTreeNode leftChild=listInOrder.getHeadNode();
- leftChild.next=null;
- leftChild.displayLink();
- HTreeNode rightChild=listInOrder.getHeadNode();
- rightChild.next=null;//and also make sure .next points to null
- rightChild.displayLink();
- HTreeNode parent=new HTreeNode('\u0000',leftChild.occur+rightChild.occur,null,leftChild,rightChild,null);
- listInOrder.insertIntoPosition(parent, 0);//insert parent node into the linkedlist
- listInOrder=getSortedLinkedList(listInOrder);//sort the linkedlist and repeat the cycle.
- if(j-1 == i){
- tree1.makeRoot(parent);//make sure root is the final parent node
- break;
- }
- }
- return tree1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement