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 3 of 3

Thread: Left and Right rotation in a balanced binary tree problem

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Left and Right rotation in a balanced binary tree problem

    hi,

    need some help with these rotations guys, going mental now, stucked with it for two days already...
    trying to implement a balanced binary tree (also known as TREAP), where nodes inserted have both key and priority value. general rules are:
    if node A is left child of node B -> key[A] < key[B]
    if node A is right child of node B -> key[A] > key[B]

    thats the easy part.
    additional condition in the treap structure is as follows:
    if A is a child of B -> priority(A) > priority(B)

    which messes everything up, 'cos now with every insertion, if the priority of new node is smaller than we have to rotate the tree, either to the left or right.
    ok, enough theory, here is the code that I've come up with so far, for inserting a new node :
    public void insert(char id, double dd)
        {
        Node newNode = new Node(); // make new node
        newNode.iData = id; // key value
        newNode.dData = dd; // priority value
     
     
        if(root==null) // no node in root
            root = newNode;
        else if(root != null)  // root occupied
        {
            Node current = root; // start at root
            Node parent;
            Node temp;
     
            while(true)  //  (exits internally)
            {
                parent = current;
                if(id < current.iData && dd > current.dData) // go left?
                {
                    current = current.leftChild;
                    if(current == null) // if end of the line,
                    { // insert on left
                        parent.leftChild = newNode;
                        return;
                    }
     
                } // end if go left
                else if(id < current.iData && dd < current.dData)
                {
                    if(parent == null)
                    {
                        root = newNode;
                    }
                    else
                    {
                        temp = current;
                        current = newNode;
                        newNode.leftChild = temp.rightChild;
                        temp.rightChild = temp;
                        return;
                    }
     
                }
                else if(id > current.iData && dd > current.dData) // or go right?
                {
                    current = current.rightChild;
                    if(current == null) // if end of the line
                    { // insert on right
                        parent.rightChild = newNode;
                        return;
                    }
                }
                else if(id > current.iData && dd < current.dData)
                {
                    if(parent == null)
                    {
                        root = newNode;
                    }
                    else
                    {
                        temp = current;
                        current = newNode;
                        newNode.rightChild = temp.leftChild;
                        temp.leftChild = temp;
                        return;
                    }
                } // end else go right
            } // end while
            } // end else not root
        }    // end insert()

    any comments, hints suggestions.. anything !!! would be great, don't want to go completly bonkers, with xmas so close


  2. #2
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Left and Right rotation in a balanced binary tree problem

    I'm guessing you are a student and this due the 10th January 2011? I am probably in you class LOL!

    I need help with this too... No idea what the lecturer wants and I have no idea why he is giving us MIT based coursework when he has not prepared us for the work. I'm convinced the whole class is in the same boat and it's sinking FAST!

    So, someone please help us!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Left and Right rotation in a balanced binary tree problem

    Break the problem up into steps...you could do so by splitting the insertion into 2 steps: add the node to the binary tree, then rotate the node (if needed) based upon priority. You could do both using a while loop, or alternatively using recursion

Similar Threads

  1. need help with binary tree
    By vash0047 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 12th, 2010, 08:23 AM
  2. 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
  3. Problem with binary tree
    By Exoskeletor in forum Object Oriented Programming
    Replies: 2
    Last Post: January 8th, 2010, 01:03 PM
  4. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM
  5. Binary Search Tree implementation
    By Ceasar in forum Java Theory & Questions
    Replies: 3
    Last Post: October 9th, 2009, 12:23 AM