Object oriented and trees question
I have a general question that come up few weeks ago when writing an AVL data structure in java.
first a little bit background to help me explain the situation.
list of classes :
public class BinaryNode
public class BinaryTree
public class BinarySearchNode extends BinaryNode
public class BinarySearchTree extends BinaryTree
public class AVLSearchNode extends BinarySearchNode
public class AVLSearchTree extends BinarySearchTree
I wrote a recursive code for the method "protected BinaryNode insert(MyObject toAdd)" in the BinarySearchNode (BSN) class (it's doing a regular BSN insert).
I used this kind of recursive calls in it : this.getLeft().insert(toAdd); this.getRight().insert(toAdd);
when writing the method "protected BinaryNode insert(MyObject toAdd)" in the AVLSearchNode (ASN) class, I followed this algorithm:
first I did a regular BSN insert : BinaryNode tmp = super.insert(toAdd);
then I rotated the tree if balance was needed.
now for my question :
I notice that each recursion call in the "BSN insert" is first going to the "ASN insert" then back to "BSN insert", also in the folding back it's run on both inserts.
I wanted it to stay only in the BSN insert in runtime, finish doing it then proceeds to the ASN insert,I did try the following in the BSN insert:
I was trying casting to BSN, I was trying used a temporary BSN variable for the recursive calls, but no good, when coming from the child back to the father he always consider "this" as AVL type.
so how do I make it stay in the BSN insert finish it then proceeds to the ASN insert?
* I'm probably missing something, as I'm only beginner in Object oriented programming, hope I had success in explaining myself.
* the code worked in the end, it's just looks weird to me, I thought about the case I had "n" children's and I call a super recursive method from the first class (meaning from the oldest father), if the recursive call will go throw all the children's in the way? (sounds to me a little bit crooked)
Jack of Clubs
Joined: Tue Apr 13, 2010 6:26 am