| | |
Is there something with my "insert" method for my binary search tree?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2009
Posts: 35
Reputation:
Solved Threads: 0
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:
Constructor for node:
This is the method for insert:
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?
My constructor for the BST:
Java Syntax (Toggle Plain Text)
public BST() { root = null; first = null; size = 0; }
Constructor for node:
Java Syntax (Toggle Plain Text)
protected Node(K key, V value) { if ((key == null) || (value == null)) { throw new NullPointerException(); } else { this.key = key; this.value = value; left = null; right = null; parent = null; predecessor = null; successor = null; };
This is the method for insert:
Java Syntax (Toggle Plain Text)
public V put (K key, V value) { if (root == null) { root = new Node(key, value); if (size == 0) { System.out.println("Setting first root!"); first = root; } size++; System.out.println("Inserted " + root.key); root = first; System.out.println(size); return root.value; } else { int compareKeyToNode = key.compareTo(root.key); // Compares key to root's key, returns -1 if less than, 1 if greater than, 0 if keys are equal if (compareKeyToNode > 0) { System.out.println("Moved right of " + root.key); root = root.right; } if (compareKeyToNode < 0) { System.out.println("Moved left of " + root.key); root = root.left; } } return (put(key, value)); }
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?
![]() |
Similar Threads
- Create code for Binary Search Tree using compareTo method using I/O (Java)
- how to insert a struct record into a binary search tree (C++)
- Binary Search Tree within in BT ? (C++)
- Binary Search Tree (Java)
- Help implementing a binary search tree in Java (Java)
- Binary Search Tree (C++)
- searching and inserting node in a binary search tree (C)
- recursive findaverage function for Binary search tree (C)
- Insertion in a binary search tree (C++)
Other Threads in the Java Forum
- Previous Thread: Java Minimum essentials
- Next Thread: How to run a *.exe file in Java
| Thread Tools | Search this Thread |
911 addball android api applet application apps array arrays automation binary bluetooth businessintelligence button card chat class classes client code collision component crashcourse css database draw eclipse ee error event exception fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me java javadoc javafx javaprojects jni jpanel julia jvm key linux list loan loop machine map method methods migrate mobile netbeans newbie nls oracle output phone physics print problem program programming project radio recursion scanner screen server service set size sms socket software sort sql string swing textfield threads time transfer tree trolltech ubuntu utility windows





