Problem debugging method for Binary Tree

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 1,649
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Problem debugging method for Binary Tree

 
0
  #1
Dec 8th, 2008
  1. public void generateBits(Node root, int current, int counter){
  2. if (root== null) return;
  3.  
  4. if (root.left == null && root.right == null){
  5. list.add(new HuffCode(root.theChar, current, counter));
  6. } else{
  7. current<<= 1; //move all the bits 1 spot to the left
  8. generateBits(root.left, current, counter+1);
  9. generateBits(root.right, (current | 1), counter+1);
  10. }
  11. }

Somehow, this method is generating wrong bit sequences for the Huffman Code. Specifically, the SAME bit sequences are being generated for different Nodes, which should be impossible... the way I'm trying to generate the bit sequences is "if you move left in the tree, add a 0 to the bit sequences, if you move right in the tree, add a 1 to the bit sequence. If you're at a leaf node, stop, and create a new HuffCode". Regardless of how the tree was created, there can only be one Node at any given position. Therefore, I should never have the same bit sequence twice, but I do. So there has to be something wrong with my method, right? I can't see what.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,649
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Problem debugging method for Binary Tree

 
0
  #2
Dec 8th, 2008
Actually, I just realized that if the first things put in are 0's, it will seem like they have the same bit sequences, when in reality, one bit sequence might be 0001 and another might be 001 or something similar. I'll investigate some more and come back if I have more problems.
Last edited by BestJewSinceJC; Dec 8th, 2008 at 9:19 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 289 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC