/** RedBlack Tree Node class **/ import java.awt.*; import ciips.animation.*; import ciips.animation.tree.*; /** Red-Black Tree node **/ class RBNode extends TreeNode { private RBNode parent; private boolean color_red; private static int width = 12; private static int height = 15; private static int offset = 2; public RBNode( int d, int pos_x, int pos_y, RBNode p, RBNode sentinel ) { super( null, d ); parent = p; color_red = true; setColour( color_red?Color.red:Color.black ); left = right = sentinel; } public boolean IsBlack() { return !isRed(); } public boolean isBlack() { return (!color_red); } public boolean IsRed() { return isRed(); } public boolean isRed() { return color_red; } // public RBNode getLeft() { return (RBNode)left; } // public RBNode getRight() { return (RBNode)right; } public RBNode getParent() { return parent; } public Color getColour() { return (color_red?Color.red:Color.black); } public void SetLeft(RBNode y){ left = y; } public void SetRight(RBNode y) { right = y; } public void SetParent(RBNode y) { parent = y; } public void SetColorBlack() { color_red = false; setColour( Color.black ); setLabelColour( Color.red ); } public void SetColorRed() { color_red = true; setColour( Color.red ); setLabelColour( Color.black ); } public void SetData( int d ){ weight = d; } public void Highlight( Graphics g, Color c ) { setColour( c ); // draw( g ); } public void Unhighlight_Node( Graphics g, Color orig ) { setColour( color_red?Color.red:Color.black ); // draw( g ); } public void drawEdge( Graphics g, TreeNode child ) { g.setColor( Color.black ); // g.drawLine( x + width/2, y + height, child.getX() + width/2, child.getY()); g.drawLine( x, y + height, child.getX(), child.getY()); } // public void draw( Graphics g ) { // Draws a node and descending edges .. except those to the sentinel // System.out.println("RBNode:draw (" + getWeight() + ")" ); // g.setColor( nodeColor ); // g.fillRect(x, y, width, height ); // g.setColor(Color.black); // g.drawRect(x, y, width, height ); // g.setColor( Color.white ); // g.setFont( bigFont ); // g.drawString( ""+getWeight(), x + offset, y+height - offset ); // } public void draw( Graphics g, RBNode sentinel ) { draw( g ); // Now draw descending edges, unless it's a sentinel if ( left != sentinel ) { drawEdge( g, left ); } if ( right != sentinel ) { drawEdge( g, right ); } } public static int getWidth() { return width; } public String toString() { return "RBNode (" + getWeight() + ")"; } }