javared-black-treered-black-tree-insertion

Unusual Java implementation of red/black tree node insertion


I'm writing a program for class in Java regarding red/black trees. I've got a good understanding of how they usually work, and am supposed to use a recursive insertion method. What I would typically use is below, to match my professor's Node class. In regards to color, a 0 is black, a 1 is red. The Node class given to us does not deal with keys at all.

private static void put(int val,  int col)
{ root = put(root, val, col); }

private static Node put(Node n, Integer val, int col)
{
    if (n == null){
        Node t=new Node(val);
        t.setColor(1);
        return t;
    }
    int cmp = val.compareTo(n.getValue());

    if (cmp < 0) n.setLeft(put(n.getLeft(), val, col));
    else if (cmp > 0) n.setRight(put(n.getRight(), val, col));
    else n.setColor(col);

    if (isRed(n.getRight()) && !isRed(n.getLeft())) n = rotateLeft(n);
    if (isRed(n.getLeft()) && isRed(n.getLeft().getLeft())) n = rotateRight(n);
    if (isRed(n.getLeft()) && isRed(n.getRight())) flipColors(n);
    return n;
}

However, the catch is that we are supposed to return a boolean value--if the user inserts a duplicate value as is already on the tree, we return false and don't attach the node. Otherwise, we attach them and return true; the code given to us for this is below, but is not recursive (part of the project requirements). And while I hadn't implemented a way of balancing or rotating properly, the returned boolean part works.

public boolean insertNode(Node node) {

    //Here is just an example of setting colors for a node. So far, it is in green color. But you need to modify the code to dynamically adjust the color to
    //either RED or BLACK according to the red-black logic 
    Node current_node;
    // if the root exists
    if (root == null) {
        root = node; // let the root point to the current node
        root.setColor(Node.BLACK);
        return true;
    } else {
        current_node = root;
        node.setColor(1);
        while (current_node != null) {
            int value = current_node.getValue();

            if (node.getValue() < value){ // go to the left sub-tree
                if (current_node.getLeft() != null) // if the left node is not empty
                    current_node = current_node.getLeft();
                else{ // put node as the left child of current_node
                    current_node.setLeft(node);
                    node.setParent(current_node);
                    current_node = null;    }   
                //System.out.println("Left:"+current_node); 
                }

            else if (node.getValue() > value){ // go to the right
                if (current_node.getRight() != null) // if the right node is not empty
                    current_node = current_node.getRight();
                else{ // put node as the right child of current_node
                    current_node.setRight(node);
                    node.setParent(current_node);
                    current_node = null;    }   
                //System.out.println("Right: "+current_node);   
                }

            else{
                //System.out.println("Else: "+current_node);
                return false;   }


            //if(current_node!=null&&current_node.getLeft()!=null&&current_node.getRight()!=null&&current_node.getLeft().isRed()&&current_node.getRight().isRed())
            //  flipColors(node);

        }
    }

    if(node.getParent()!=null){
        node=node.getParent();
        System.out.println("Case: node has parent, val="+node.getValue());
    }

    if(node.getLeft()!=null&&node.getRight()!=null){
        if((node.getRight().isRed())&&!node.getLeft().isRed())
            node=rotateLeft(node);
        if((node.getLeft().isRed())&&(node.getParent()!=null)&&(node.getParent().getLeft().getLeft()!=null)&&(node.getParent().getLeft().getLeft().isRed()))
            node=rotateRight(node);
        if((node.getLeft().isRed()) && (node.getRight().isRed()))
            flipColors(node);
    }
    return true;
}

I wasn't able to find any comparable implementations online, and it seems that the boolean is necessary for the program's gui to work properly. If someone has a good suggestion for where to start, I would appreciate it!


Solution

  • For the recursive insertNode, I would suggest you the following: Create a function insertNode(Node node, Node current_node) which returns a boolean value. The idea is to always call the function insertNode for the currently investigated node, starting from the root node. If the node cannot be immediately added to current_node, the responsible node is called recursively to handle the node. I have provided you a short example based on your code (with some comments what the basic idea is, there is obviously some stuff missing). I hope, I got your question correctly and this helps you with your understanding.

    public boolean insertNode(Node node) {
        if (root == null) {
            root = node;
            root.setColor(Node.BLACK);
            return true;
        } else {
            boolean result = insertNode(node, root);
    
            if (result) {
                //Some other important stuff to do...
            }
    
            return result;
        }
    }
    
    public boolean insertNode(Node node, Node current_node) {
        int value = current_node.getValue();
    
        if (node.getValue() < value) {
            if (current_node.getLeft() != null) {
                // Investigate left
                return insertNode(node, current_node.getLeft());
            } else {
                // Insert node left
                return true;
            }
        } else if (node.getValue() > value) {
            if (current_node.getRight() != null) {
                // Investigate right
                return insertNode(node, current_node.getRight());
            } else {
                // Insert node right
                return true;
            }
        } else {
            return false;
        }
    }