| | |
Problem debugging method for Binary Tree
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 1,649
Reputation:
Solved Threads: 206
Java Syntax (Toggle Plain Text)
public void generateBits(Node root, int current, int counter){ if (root== null) return; if (root.left == null && root.right == null){ list.add(new HuffCode(root.theChar, current, counter)); } else{ current<<= 1; //move all the bits 1 spot to the left generateBits(root.left, current, counter+1); generateBits(root.right, (current | 1), counter+1); } }
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.
•
•
Join Date: Sep 2008
Posts: 1,649
Reputation:
Solved Threads: 206
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.
![]() |
Other Threads in the Java Forum
- Previous Thread: Panel Size
- Next Thread: I have tried to set a label to change its text at each time.
Views: 289 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool keyword linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






