/** AVL Tree Node class **/ import java.awt.*; import ciips.animation.*; import ciips.animation.tree.*; /** AVL Tree node **/ class AVLNode extends TreeNode { private AVLNode parent; private int balance; private boolean isAnimate; /** Constructor **/ public AVLNode( int d, int pos_x, int pos_y, AVLNode p, int b) { super( null, d ); parent = p; balance = b; } /** return parent Node **/ public AVLNode getParent() { return parent; } /** return balance **/ public int getBalance() { return balance; } /** Set parent **/ public void SetParent(AVLNode y) { parent = y; } /** Set balance **/ public void SetBalance(int y) { balance = y; } /** draw line **/ public void drawEdge( Graphics g, TreeNode child ) { g.setColor( Color.blue ); g.drawLine( x + width/2, y + height, child.getX() + width/2, child.getY()); } private static final int text_offset = 15; private static int width = 17; private static int height = 16; private static int offset = 3; private static final int text_offset_x = 17; private static final int text_offset_y = 16; private static final int bal_offset_x = -10; private static final int bal_offset_y = 10; /** draw Node **/ public void draw( Graphics g ) { // Draws a node and descending edges .. except those to the sentinel System.out.println("AVLNode:draw (" + getWeight() + ")" ); g.setColor( getNodeColor() ); // color of each node - by mingoo g.fillOval(x, y, width, height ); g.setColor(Color.black); g.drawOval(x, y, width, height ); g.setColor( Color.black ); // g.setFont( bigFont ); g.drawString( ""+getWeight(), x + offset, y + height - offset ); Font old_font = g.getFont(); Font bal_font = new Font( "Courier", Font.PLAIN, 12 ); if(!isAnimate){ g.setColor( Color.blue); g.setFont(bal_font); g.drawString(""+getBalance(), x + bal_offset_x, y + bal_offset_y); } g.setFont(old_font); } public void draw( Graphics g, AVLNode sentinel ) { draw( g ); // Now draw descending edges, unless it's a sentinel if ( left != sentinel ) { drawEdge( g, left ); } if ( right != sentinel ) { drawEdge( g, right ); } } /** return width **/ public static int getWidth() { return width; } /** check weather it is animating or not **/ public void noBalanceView(boolean noBalView){ if(noBalView) isAnimate = true; else isAnimate = false; } static Color[] NodeColours = { Color.red, Color.pink, Color.orange, Color.blue, Color.red }; static String[] NodeBalance = { "LHH", "Left Heavy", "Balanced", "Right Heavy", "RHH" }; /** set the color of nodes depends on balance **/ public Color getNodeColor(){ int colour_ix = balance + 2; if ( (colour_ix >= 0) && (colour_ix < NodeColours.length) ) { return NodeColours[colour_ix]; } else return Color.black; /** switch(balance) { case 0: return Color.orange; case 1: return Color.blue; case 2: return Color.red; case -1: return Color.pink; case -2: return Color.red; default: return Color.black; } **/ } /* public String getNodeText(){ switch(balance){ case 0: return " "; case 1: return "Right Heavy"; case 2: return " "; case -1: return "Left Heavy"; case -2: return " "; default: return " "; } } */ }