Is there something with my "insert" method for my binary search tree?

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

Join Date: Jul 2009
Posts: 35
Reputation: iamsmooth is an unknown quantity at this point 
Solved Threads: 0
iamsmooth iamsmooth is offline Offline
Light Poster

Is there something with my "insert" method for my binary search tree?

 
0
  #1
34 Days Ago
So my binary search tree doesn't seem to be traversing through all the nodes. each node has references to other nodes. So I'm traversing through right or left (depending on value) until I hit null, which means I can insert. After insertion, I reset root to first so it can reiterate through the top of the search tree when I call it next. But instead, it's not working as it should, seems like it only moves through the first node.

My constructor for the BST:
  1. public BST() {
  2.  
  3. root = null;
  4. first = null;
  5. size = 0;
  6.  
  7. }

Constructor for node:
  1. protected Node(K key, V value) {
  2.  
  3. if ((key == null) || (value == null)) {
  4.  
  5. throw new NullPointerException();
  6.  
  7. } else {
  8.  
  9. this.key = key;
  10. this.value = value;
  11. left = null;
  12. right = null;
  13. parent = null;
  14. predecessor = null;
  15. successor = null;
  16. };

This is the method for insert:
  1. public V put (K key, V value) {
  2. if (root == null) {
  3. root = new Node(key, value);
  4. if (size == 0) {
  5. System.out.println("Setting first root!");
  6. first = root;
  7. }
  8. size++;
  9. System.out.println("Inserted " + root.key);
  10. root = first;
  11. System.out.println(size);
  12. return root.value;
  13. }
  14. else {
  15. int compareKeyToNode = key.compareTo(root.key);
  16. // Compares key to root's key, returns -1 if less than, 1 if greater than, 0 if keys are equal
  17. if (compareKeyToNode > 0) {
  18. System.out.println("Moved right of " + root.key);
  19. root = root.right;
  20. }
  21. if (compareKeyToNode < 0) {
  22.  
  23. System.out.println("Moved left of " + root.key);
  24. root = root.left;
  25. }
  26. }
  27. return (put(key, value));
  28. }

Say for input of 15, 12, 14, 8

It should print:

Inserting 15
Moving left of 15
Inserting 12
Moving left of 15
Moving right of 12
Inserting 14
Moving left of 15
Moving left of 12
Inserting 8

Instead, it outputs:

Inserting 15
Moving left of 15
Inserting 12
Moving left of 15
Inserting 14
Moving left of 15
Inserting 8

So it only seems to be traversing through the top node and inserts right away even though it shouldn't be null. Can anyone spot a logic error in my code?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 35
Reputation: iamsmooth is an unknown quantity at this point 
Solved Threads: 0
iamsmooth iamsmooth is offline Offline
Light Poster
 
0
  #2
34 Days Ago
I figured it out. A tree can only have one root, I'm so silly.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC