4

I am trying to implement the CLRS pseudocode of Red Black Tree. When I am trying to run the program, NullPointerException is thrown. Please review the code and find whats wrong in it. Any further suggestion is welcome.

public class RedBlackTree {

Node nil;
Node root;
String RED = "red";
String BLACK = "black";

public void left_rotate(RedBlackTree T, Node x) {
    Node y = x.right;
    x.right = y.left;
    if (y.left != T.nil)
        y.left.parent = x;
    y.parent = x.parent;
    if (x.parent == T.nil)
        T.root = y;
    else if (x == x.parent.left)
        x.parent.left = y;
    else
        x.parent.right = y;
    y.left = x;
    x.parent = y;
}

public void right_rotate(RedBlackTree T, Node x) {
    Node y = x.left;
    x.left = y.right;
    if (y.right != T.nil)
        y.right.parent = x;
    y.parent = x.parent;
    if (x.parent == T.nil)
        T.root = y;
    else if (x == x.parent.right)
        x.parent.right = y;
    else
        x.parent.left = y;
    y.right = x;
    x.parent = y;
}

public void rb_insert_fixup(RedBlackTree T, Node z) {

    while (z.parent.color == RED) {
        if (z.parent == z.parent.parent.left) {
            Node y = z.parent.parent.right;
            if (y.color == RED) {
                z.parent.color = BLACK;
                y.color = BLACK;
                z.parent.parent.color = RED;
                z = z.parent.parent;
            } else {
                if (z == z.parent.right) {
                    z = z.parent;
                    left_rotate(T, z);
                }
                z.parent.color = BLACK;
                z.parent.parent.color = RED;
                right_rotate(T, z.parent.parent);
            }
        } else {
            Node y = z.parent.parent.left;
            if (y.color == RED) {
                z.parent.color = BLACK;
                y.color = BLACK;
                z.parent.parent.color = RED;
                z = z.parent.parent;
            } else {
                if (z == z.parent.left) {

                    z = z.parent;
                    right_rotate(T, z);
                }
                z.parent.color = BLACK;
                z.parent.parent.color = RED;
                left_rotate(T, z.parent.parent);
            }
        }

    }
    T.root.color = BLACK;
}

public void insert(RedBlackTree T, Node z) {
    Node y = T.nil;
    Node x = T.root;
    while (x != T.nil) {
        y = x;
        if (z.key < x.key)
            x = x.left;
        else
            x = x.right;
    }
    z.parent = y;
    if (y == T.nil)
        T.root = z;
    else if (z.key < y.key)
        y.left = z;
    else
        y.right = z;
    z.left = T.nil;
    z.right = T.nil;
    z.color = RED;
    rb_insert_fixup(T, z);
}

public void inorder_tree_walk(Node x) {
    if (x != null) {
        inorder_tree_walk(x.left);
        System.out.println(x.key + ":" + x.color + " ");
        inorder_tree_walk(x.right);
    }
}

public static void main(String[] args) {
    RedBlackTree rbt = new RedBlackTree();

    Node a = new Node(12, "a");
    rbt.insert(rbt, a);
    Node b = new Node(5, "b");
    rbt.insert(rbt, b);
    Node c = new Node(18, "c");
    rbt.insert(rbt, c);
    Node d = new Node(2, "d");
    rbt.insert(rbt, d);
    Node e = new Node(9, "e");
    rbt.insert(rbt, e);

    rbt.inorder_tree_walk(rbt.root);
    }

}

class Node {
int key;
String data;
String color;
Node left, right, parent;

public Node(int key, String data) {
    this.key = key;
    this.data = data;
    }
}

StackTrace is:

Exception in thread "main" java.lang.NullPointerException at RedBlackTree.rb_insert_fixup(RedBlackTree.java:42) at RedBlackTree.insert(RedBlackTree.java:102) at RedBlackTree.main(RedBlackTree.java:117)

aditya
  • 43
  • 5
  • Please go through this http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it see whether you can fix if not then can you please add the complete stacktrace – Shadow Droid Oct 19 '15 at 07:11
  • Thank You.But i couldnt find the mistake.As one of the answer suggested that Node nil and root need to be initialized.But even after that,itis still throwing NullPointerException.Can you please help? – aditya Oct 19 '15 at 08:45
  • can you provide the link from where you referred the CLRS pseudocode of Red Black Tree..bcoz I tried running your program and found many exception...Even if I give you fix for the above stackTrace exception it will give another exception bcoz you have not implemented insert code properly. – Shadow Droid Oct 19 '15 at 09:08
  • I tried to implement the pseudocode myself.I am fairly new in learning data structure, thats why many mistakes may have happen.If you have CLRS, then can you please check where I have made mistake in implementation.I will be highly thankful to you. – aditya Oct 20 '15 at 04:54
  • I got the mistake.It was because I didnt initialize nil and root nodes.And there was a minor mistake in code.After correcting those mistakes the program ran correctly.Thank you for help. – aditya Oct 20 '15 at 06:52

1 Answers1

0

You must initialize nil and root:

public class RedBlackTree {

Node nil;  <-- is null
Node root; <-- is null

Otherwise, this is also null:

while (z.parent.color == RED) { <-- z.parent is null
Juvanis
  • 25,000
  • 3
  • 61
  • 84
KostasC
  • 856
  • 5
  • 18
  • 35
  • Even after i initialize nil and root to null,it is still throwing NullPointerException. – aditya Oct 19 '15 at 08:36
  • I got the mistake.It was because I didnt initialize nil and root nodes.I was confused between nil and null node.Now i understand.And there was a minor mistake in code.After correcting those mistakes the program ran correctly.Thank you for help. – aditya Oct 20 '15 at 06:53
  • @aditya glad to help you! – KostasC Oct 20 '15 at 08:14