public class RedBlackNode<Element>
{
    /**
     * A node storing element element, has references to the parent node, 
     * and the left and right children (null if not present)
     */
    private Element element;
    private RedBlackNode<Element> left, right, parent;
    private Color color;
    
    /**
     * Constructor creating a new node containing element k
     */
    public RedBlackNode(Element k) {
        left = right = parent = null;
        element = k;
        color = Color.RED;
    }
    
    // Accessor methods
    public RedBlackNode<Element> getLeft() { return left; }
    public RedBlackNode<Element> getRight() { return right; }
    public RedBlackNode<Element> getParent() { return parent; }
    public Element getElement() { return element; }
    public Color getColor() { return color; }

    /**
     * Change the color of the node
     */
    public void setColor(Color c) {
        color = c;
    }
    
    /**
     * Update element of node to element k
     */
    public void setElement(Element k) {
        element = k;        
    }

    /**
     * Change the reference to the left child; also updates the parent reference of
     * the new left child to point to this node; the old child's parent is set to null
     */
    public void setLeft(RedBlackNode<Element> child) {
        if (left != null) left.parent = null;
        left = child;
        if (child != null) {
            if (child.parent != null) {
                if (child.parent.left == child)
                    child.parent.left = null;
                else
                    child.parent.right = null;
                }
            child.parent = this;         
        }
    }

    /**
     * Change the reference to the right child; also updates the parent reference of
     * the new right child to point to this node; the old child's parent is set to null
     */
    public void setRight(RedBlackNode<Element> child) {
        if (right != null) right.parent = null;
        right = child;
        if (child != null) {
            if (child.parent != null) {
                if (child.parent.left == child)
                    child.parent.left = null;
                else
                    child.parent.right = null;
                }
            child.parent = this;         
        }
    }    
}