Reputation: 503
Here is my code. The LinkedBinaryTree, and Position classes are from the textbook and I can provide the code for those if I need to, but I'm not sure it's necessary. This class is supposed to be able to add a new node to a binary tree in such a way that for each internal node, the element stored to the left of the parent node is less than the parent's element, and the element stored to the right of the parent node is greater than the parent node, so it's pretty much supposed to create a binary search tree. The problem is that when I print the resulting tree out (see below) I only get the root element.
import net.datastructures.LinkedBinaryTree;
import net.datastructures.Position;
import java.util.Iterator;
public class IntLinkedBinaryTree extends LinkedBinaryTree<Integer> {
// define instance variables and methods, including a constructor(s) as needed
private Position<Integer> root;
private int size;
/**
* Creates an empty IntLinkedBinaryTree
*/
public IntLinkedBinaryTree() {
root = null;
size = 0;
}
/**
* Add a new node with e to the tree rooted at position p
*
* @param p The root of the tree to which new node is added
* @param e The element of the new node
* @return If a node with e does not exist, a new node with e is added and
* reference to the node is returned. If a node with e exists, null is returned.
*/
public Position<Integer> add(Position<Integer> p, Integer e) {
if (p == null) {
root = addRoot(e);
size++;
return p;
}
Position<Integer> x = p;
Position<Integer> y = x;
while (x != null) {
if (x.getElement().equals(e)) {
return null;
} else if (x.getElement() > e) {
y = x;
x = left(x);
} else {
y = x;
x = right(x);
}
} // end while
Position<Integer> temp;
if (y.getElement() > e) {
temp = createNode(e, validate(y), null, null);
addLeft(temp, e);
} else {
temp = createNode(e, validate(y), null, null);
addRight(temp, e);
}
size++;
return temp;
}
public static void main(String[] args) {
// create a new binary tree instance
IntLinkedBinaryTree t = new IntLinkedBinaryTree();
// add some integers
t.add(t.root, 100);
t.add(t.root, 50);
t.add(t.root, 150);
t.add(t.root, 70);
// print all integers in the tree in increasing order
// after adding above four integers, the following should be printed
// 50 70 100 150
Iterator<Position<Integer>> it = t.inorder().iterator();
System.out.println();
while (it.hasNext()) {
System.out.print(it.next().getElement() + " ");
}
System.out.println();
}
}
I'm only getting 100 as the output, and I'm about to pull my hair out. The addRoot(), addLeft(), and addRight(), inorder(), and iterator(), methods were provided in the LinkedBinaryTree class by the instructor, and the entire main method was as well. When I debugged the code using intelliJ, it called the add* methods but then only 100 prints to the console.
Upvotes: 2
Views: 252
Reputation: 503
I changed addLeft() and addRight() to setLeft() and setRight(). It now works but I'm not sure why because addLeft/Right() calls setLeft/Right() in the super class...
Upvotes: 1