public void insert(int data) {
if (root == null) root = new AVLNode(data);
else {
AVLNode node = root;
while (node != null) {
if (node.data <= data) {
node = node.right;
}
else {
node = node.left;
}
}
node = new AVLNode(data);
}
}
im trying to make a binary search tree (nonrecursive) and then later on access is and do a preorder traversal, but when i print root i only get 1 element. all the insertions work, i think, but then root just has the first insertion in it and its left and right dont refer to anything, i think thats why im only getting 1 element. how do i refer root to the top of the node tree? here is the node class btw
class AVLNode
{
AVLNode left, right;
int data;
/* Constructor */
public AVLNode()
{
left = null;
right = null;
data = 0;
}
/* Constructor */
public AVLNode(int n)
{
left = null;
right = null;
data = n;
}
}