so for my 2nd hw I am coding an insert and delete of an AVL Tree and I'm almost done with insert. the tree is operating properly, with inserting, checking heights, and rotating when needed but if the root is involved in the rotate an issue occurs.
so the root gets rotated and in the rotation function if i put print statements i can see that the root is now the child of another node so it IS rotating properly. but the problem occurs when i go to my print statement in the AVLTree.java class
public void print()
System.out.println(AVLroot.getData() + " " + AVLroot.left.getData() + " " + AVLroot.right.getData());
what im doing: i insert 15, then 12, then 18, then 19, then 20. this causes a rotation. so now 15's children are now 12 and 19 and 19s children are now 18 and 20. does this fine.
i then insert 21. this causes a single right rotation at the root. when i use my print statement i get 15 12 18. which 18 should be the right child of 15 because it hops off of the 19 which is now the real root. so the AVLroot variable is staying with the 15 instead of changing with the rotation.
I was given a BinTreenode.java which is where all of the inserting and rotating happens. and a BinTree.java which we are supposed to inherit from with AVLTree.java which has a private BinTreeNode AVLroot;
This must be caused by the fact that AVLroot is a variable of the AVL Tree so the variable that is called on to print isn't being changed. The only problem is that I have no clue how to fix this. the bintreenode insert method is void so I can't return anything to AVLTree.java that calls the insert and I don't know of any other way to fix it.
So I was wondering if anyone could give me a tip or something. ive asked my instructor just before this on email but it may be a couple days before a response and I can't waste time since i have more to code after.
(I didn't post any large chunks of code because its a hw assignment and I don't want to get in trouble)