Thread: Binary search tree rotations

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    1

    Binary search tree rotations

    Rotations doesnt work, no idea why:/ been stuck for days with this


    Code:
    void skirtumas(structnode *tree){
        if(tree ->left){
            LEFT = height(tree -> left);
           printf("left\n");
        }
        else {LEFT = 0;}
    
    
        if(tree ->right){
            RIGHT = height(tree -> right);
           printf("right\n");
         }
        else{RIGHT = 0;}
    
    
        if(LEFT >RIGHT){
            difference =LEFT - RIGHT;
        }
        else{
            difference =RIGHT - LEFT;
        }
    }
    
    
    int height(struct node*tree){
    
    
        int r = 0, l = 0;
       if(tree->left)l=height(tree->left);
       if(tree->right)r=height(tree->right);
    
    
        if(l > r){
            return(l+1);
        }else{
            return(r+1);
        }
    }
    
    
    struct node*balansas(struct node *tree)
    {
    
    
        struct node *temp;
        struct node *temp2;
        struct node *temp3;
        skirtumas(tree);
        if(difference >1)
        {
            if(RIGHT >LEFT)
            {
               skirtumas(tree -> right);
                if(LEFT >RIGHT)// RIGHT subtree is LEFT heavy, double rotation
                {
                    temp3 =tree -> right;
                    temp3-> left = NULL;
                    temp3-> left = tree -> right -> left ->right;
    
    
                    temp2 =tree -> right -> left;
                    temp2-> right = NULL;
                    temp2-> right = temp3;
    
    
                    temp =tree;
                    temp ->right = NULL;
                    temp ->right = temp2;
                    tree =NULL;
                    tree =temp;
    
    
                    temp =NULL;
                    temp =tree -> right;
                    temp ->left = tree;
                    temp->left -> right = tree -> right ->left;
                    tree ->right = NULL;
                    tree =temp;
    
    
                }
                else //single RIGHT rotation
                {
                    temp =NULL;
                    temp =tree -> right;
                    temp ->left = tree;
                    temp ->left -> right = tree -> right ->left;
                    tree ->right = NULL;
                    tree =temp;
                }
            }
        else if(RIGHT <LEFT)
            {
    
    
               skirtumas(tree -> left);
                if(LEFT <RIGHT)// LEFT subtree is RIGHT heavy, double rotation
                {
                    temp3 =tree -> left;
                    temp3-> right = NULL;
                    temp3-> right = tree -> left -> right ->left;
                    printf("aaaaaaaa\n");
    
    
                    temp2 =tree -> left -> right;
                    temp2-> left = NULL;
                    temp2-> left = temp3;
    
    
                    temp =tree;
                    temp ->left = NULL;
                    temp ->left = temp2;
                    tree =NULL;
                    tree =temp;
    
    
                    temp =NULL;
                    temp =tree -> left;
                    temp ->right = tree;
                    temp ->right ->left = tree -> left -> right;
                    tree ->right = NULL;
                    tree =temp;
    
    
                }
                else //single LEFT rotation
                {
                    temp =NULL;
                    temp =tree -> left;
                    temp ->right = tree;
                    temp ->right ->left = tree -> left -> right;
                    tree ->right = NULL;
                    tree =temp;
                }
            }
    
    
           skirtumas(tree);
        }
        temp3 = NULL;
        temp2 = NULL;
        temp = NULL;
        return(tree) ;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
            if(RIGHT >LEFT)
            {
               skirtumas(tree -> right);
                if(LEFT >RIGHT)// RIGHT subtree is LEFT heavy, double rotation
    You really need to make skirtumas() return values by NOT using global variables.

    For example, are you expecting to compare the new LEFT with the old RIGHT (for example)?

    > temp3-> left = tree -> right -> left ->right;
    Does this crash?
    Because if any of the -> run into a NULL pointer, then you're out of luck.

    Short, Self Contained, Correct Example
    Construct a simple main() that builds the smallest possible unbalanced tree and use that to TEST your code with the help of a debugger.

    If you're still stuck, then you can post a program we can test as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possible rotations. Binary search tree
    By Jonsh in forum Tech Board
    Replies: 2
    Last Post: 03-28-2014, 10:01 PM
  2. Binary Search Tree-search method help?
    By shocklightning in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2012, 10:57 PM
  3. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  4. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM