red black tree balancing?

Posted by Anirudh Kaki on Stack Overflow See other posts from Stack Overflow or by Anirudh Kaki
Published on 2012-03-21T11:21:01Z Indexed on 2012/06/05 22:40 UTC
Read the original article Hit count: 283

Filed under:
|
|

i am working to generate tango tree, where i need to check whether every sub tree in tango is balanced or not.

if its not balanced i need to make it balance? I trying so hard to make entire RB-tree balance but i not getting any proper logic so can any one help me out??

here i am adding code to check how to find my tree is balanced are not but when its not balanced how can i make it balance.

static boolean verifyProperty5(rbnode n) {

    int left = 0, right = 0;
    if (n != null) {
        bh++;
        left = blackHeight(n.left, 0);
        right = blackHeight(n.right, 0);
    }
    if (left == right) {
        System.out.println("black height  is :: " + bh);
        return true;
    } else {
        System.out.println("in balance");
        return false;
    }
}

public static int blackHeight(rbnode root, int len) {
        bh = 0;
        blackHeight(root, path1, len);          
        return bh;
    }

    private static void blackHeight(rbnode root, int path1[], int len) {
        if (root == null)
            return; 

        if (root.color == "black"){
            root.black_count = root.parent.black_count+1;

        } else{
            root.black_count = root.parent.black_count;
        }
         if ((root.left == null) && (root.right == null)) {             
            bh = root.black_count;              
        }               
        blackHeight(root.left, path1, len);
        blackHeight(root.right, path1, len);
    }   

© Stack Overflow or respective owner

Related posts about java

Related posts about algorithm