Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: AVL Tree. root not changing from rotations

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default AVL Tree. root not changing from rotations

    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)


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: AVL Tree. root not changing from rotations

    someone please help. there are views but no posts.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: AVL Tree. root not changing from rotations

    Its an abstract, academic question without code to look at, compile and test.
    If your code is not executing like you want it to, its very hard for anyone to suggest coding fixes for unseen code.

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: AVL Tree. root not changing from rotations

    maybe if i try to generalize it. i have a class that is an object that contains a set of other objects. the class that creates the objects making up the set alter the set of objects. when they are altered and i go to print in the first class it doesn't show one case of the alterations because of the fact that the root is a variable of the first class and isn't being changed by the second class.
    Last edited by beginnerprogrammer; June 11th, 2011 at 01:56 PM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: AVL Tree. root not changing from rotations

    the root is a variable of the first class and isn't being changed by the second class
    Ok so if it needs to be changed, why isn't it?
    In your description: Which is the first class and which is the second?

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: AVL Tree. root not changing from rotations

    Quote Originally Posted by Norm View Post
    Ok so if it needs to be changed, why isn't it?
    In your description: Which is the first class and which is the second?
    the first class is the AVLTree.java which makes the tree and the second class is the BinTreeNode.java which im just modifying to create the AVL nodes.


    idk why its not changing thats the trouble im having. i know exactly what the problem is but dont see the way to fix it as of yet.

    AVLTree.java

    has a private BinTreeNode AVLRoot

    so since the root is actually a variable of the tree thats the difference it has from the nonroot nodes. so my guess is that the tree is being modified but whats stored in the root variable is never receiving the change because its happening in another class that doesnt pass anything back.

    i dont know if i can still change the function to return a bintreenode and return the root maybe. so if its different it will change and if its not it will just pass back what it already was. i think that would work but im not sure if i can just change the parameters or not ill have to check next time i work on it. does that sound like a plausible solution?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: AVL Tree. root not changing from rotations

    Sorry, without seeing some code, I really can't make any useful comments.

Similar Threads

  1. Java Calculator Square Root Function
    By laser1992 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 3rd, 2011, 09:34 AM
  2. Square root not working
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 7th, 2011, 06:25 PM
  3. Finding square root
    By Tracy22 in forum The Cafe
    Replies: 1
    Last Post: October 18th, 2010, 06:18 PM
  4. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM
  5. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 01:14 AM