Results 1 to 3 of 3

Math Help - Object oriented and trees question

  1. #1
    Newbie
    Joined
    Apr 2010
    Posts
    8

    Object oriented and trees question

    Hi all

    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)
    Nivg
    Jack of Clubs


    Posts: 5
    Joined: Tue Apr 13, 2010 6:26 am
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Senior Member
    Joined
    Jun 2009
    Posts
    251
    Consider this Java Tips - Binary Search Tree Implementation in Java

    Why do you have so many classes? For example, what's the difference between your tree class and node class? Isn't it like having a class for a list and a class for a node? I may be wrong, but that's my initial impression. Could you explain your assignment?
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Apr 2010
    Posts
    8
    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

    that how we learnt trees, in BinaryNode the insert operation is random, all the other tree data structures I believe you know, we learned to start from the most basic data structure and use it for the complex ones.

    all the tree classes are working as shell, when a user create a tree he don't need to handle all the small operation in the node, he need only the tree.

    for example in BSN we got all the setters and getters (that call with casting to the setters and getters in the BN) with the full insert and remove operation (which are new here, they don't use the super ones as they work differently), while in the BST we only have remove and insert (both calls the BSN remove and insert operations, insert create the root if it's the first node), both of-course have constructors , in the BST the constructor call the super in the BT and init the root as null (meaning creating an empty tree)...hope you get the quick and undetailed explanation.
    Last edited by Nivg; May 4th 2010 at 02:55 AM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Object-Oriented Formal Specification - Help please
    Posted in the Discrete Math Forum
    Replies: 0
    Last Post: May 4th 2011, 01:54 AM
  2. Graph Theory question about trees
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: November 27th 2009, 02:52 AM
  3. Graph Theory question about trees
    Posted in the Differential Geometry Forum
    Replies: 0
    Last Post: November 26th 2009, 03:30 PM
  4. how can you plant 10 trees in 5 rows of 4 trees each?
    Posted in the Discrete Math Forum
    Replies: 4
    Last Post: December 22nd 2008, 11:43 AM
  5. Trees question
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: January 6th 2007, 08:00 AM

Search Tags


/mathhelpforum @mathhelpforum