ciips.animation.tree
Class TreeNode

java.lang.Object
  |
  +--ciips.animation.tree.TreeNode
All Implemented Interfaces:
DrawingObj

public class TreeNode
extends java.lang.Object
implements DrawingObj

This class holds the information regarding a node from the heap/complete binary tree. This class implements the DrawingObj interface and hence can be freely added to the drawing panel using the addDrawingObj method. e.g.

	TreeNode node = new TreeNode();
	drawingPanel.addDrawingObj(node);
 
Any added drawing object can be remove from the panel by using the removeObj method. e.g.
	drawingPanel.removeObj(node);
 

See Also:
DrawingPanel.addDrawingObj(ciips.animation.DrawingObj), DrawingPanel.removeObj(ciips.animation.DrawingObj)

Field Summary
 boolean highlight
          Attribute to indicate if the node is to be highlighted.
 boolean highlightLeft
          Attribute to indicate if the left branch is to be highlighted.
 boolean highlightRight
          Attribute to indicate if the right branch is to be highlighted.
 
Constructor Summary
TreeNode()
          Create a new left node with weight 0.
TreeNode(int weight)
          Create a new leaf node with the specified weight.
TreeNode(java.lang.String label)
          Create a new leaf node with 0 weight and label as specified.
TreeNode(java.lang.String label, int weight)
          Create a new leaf node with label and weight as specified in the parameters.
TreeNode(TreeNode node)
          Create a node with the left node as set and weight of the current node set to 0.
TreeNode(TreeNode node1, TreeNode node2)
          Create a node with the left and right child nodes specified and the weight of the current node is the sum of the child node.
 
Method Summary
 void draw(java.awt.Graphics g)
          This method draws the node on the corresponding graphical context normally passed in from the drawing panel.
 int getDepth()
          Get the depth of his node.
 java.lang.String getLabel()
          Get the label of this node.
 TreeNode getLeftTreeNode()
          Get the left child of this node.
 TreeNode getRightTreeNode()
          Get the right child of this node.
 int getWeight()
          Get the weight of this node.
 int getX()
          Get the left most position of the node.
 int getY()
          Get the top most position of the node.
 void initColors(java.awt.Color nodeColor)
          Set the color of the node.
 void initFonts(java.awt.Font hugeFont, java.awt.Font bigFont)
          Assign some font instances to reduce initialization over during redraw.
 boolean isLeaf()
          Check if this node is a leaf node.
 void move(int x, int y)
          Move the node and all its branches based on the parameters.
 void moveTreeNode(int dx, int dy)
          Move the tree starting with node dx pixels to the right and dy pixels down.
 void setDepth(int depth)
          Sets the depth of this node corresponding to the root node of the tree.
 void setLabel(java.lang.String label)
          Set the label of this node.
 void setLeftTreeNode(TreeNode node)
          Link the left branch of this node to the node passed in as the parameter.
 void setRightTreeNode(TreeNode node)
          Link the right branch of this node to the node passed in as the parameter.
 void setWeight(int weight)
          Set the weight of this node.
 void setX(int x)
          Sets the x coordinate of the top-left corner of the node
 void setY(int y)
          Sets the y coordinate of the top-left corner of the node
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

highlightLeft

public boolean highlightLeft
Attribute to indicate if the left branch is to be highlighted.

highlightRight

public boolean highlightRight
Attribute to indicate if the right branch is to be highlighted.

highlight

public boolean highlight
Attribute to indicate if the node is to be highlighted.
Constructor Detail

TreeNode

public TreeNode(TreeNode node1,
                TreeNode node2)
Create a node with the left and right child nodes specified and the weight of the current node is the sum of the child node.
Parameters:
node1 - The left node of this newly created node.
node2 - The right node of this newly created node.

TreeNode

public TreeNode(TreeNode node)
Create a node with the left node as set and weight of the current node set to 0.
Parameters:
node - The left node of the newly created node.

TreeNode

public TreeNode()
Create a new left node with weight 0.

TreeNode

public TreeNode(java.lang.String label,
                int weight)
Create a new leaf node with label and weight as specified in the parameters.
Parameters:
label - The label of the new node.
weight - The weight of the new node.

TreeNode

public TreeNode(int weight)
Create a new leaf node with the specified weight.
Parameters:
weight - The weight of the new node.

TreeNode

public TreeNode(java.lang.String label)
Create a new leaf node with 0 weight and label as specified.
Parameters:
label - Label of the new node.
Method Detail

setWeight

public void setWeight(int weight)
Set the weight of this node.
Parameters:
weight - The weight to be assigned to this node.

setLabel

public void setLabel(java.lang.String label)
Set the label of this node.
Parameters:
label - The label to be assigned to this node.

getWeight

public int getWeight()
Get the weight of this node.
Returns:
Weight of this node.

getLabel

public java.lang.String getLabel()
Get the label of this node.
Returns:
Label of this node.

setLeftTreeNode

public void setLeftTreeNode(TreeNode node)
Link the left branch of this node to the node passed in as the parameter.
Parameters:
node - The new left child of this node.

setRightTreeNode

public void setRightTreeNode(TreeNode node)
Link the right branch of this node to the node passed in as the parameter.
Parameters:
node - The new right child of this node.

getLeftTreeNode

public TreeNode getLeftTreeNode()
Get the left child of this node.
Returns:
the left child this this node.

getRightTreeNode

public TreeNode getRightTreeNode()
Get the right child of this node.
Returns:
the right child this this node.

isLeaf

public boolean isLeaf()
Check if this node is a leaf node. A leaf node has both left and right nodes null.
Returns:
true if the node is a leaf node; false otherwise.

setX

public void setX(int x)
Sets the x coordinate of the top-left corner of the node

setY

public void setY(int y)
Sets the y coordinate of the top-left corner of the node

getX

public int getX()
Get the left most position of the node.
Specified by:
getX in interface DrawingObj
Returns:
The x coordinates of the top-left corner of the node

getY

public int getY()
Get the top most position of the node.
Specified by:
getY in interface DrawingObj
Returns:
The y coordinates of the top-left corner of the node

getDepth

public int getDepth()
Get the depth of his node.
Returns:
The depth of this node in a tree.

setDepth

public void setDepth(int depth)
Sets the depth of this node corresponding to the root node of the tree.
Parameters:
depth - Depth of the node.

move

public void move(int x,
                 int y)
Move the node and all its branches based on the parameters.
Specified by:
move in interface DrawingObj
Parameters:
x - The horizontal destination position of this node.
y - The vertical destination position of this node.

moveTreeNode

public void moveTreeNode(int dx,
                         int dy)
Move the tree starting with node dx pixels to the right and dy pixels down.
Parameters:
node - The root node of the tree to be moved.
dx - The change in x direction.
dy - The change in y direction.

initFonts

public void initFonts(java.awt.Font hugeFont,
                      java.awt.Font bigFont)
Assign some font instances to reduce initialization over during redraw.

initColors

public void initColors(java.awt.Color nodeColor)
Set the color of the node.
Parameters:
nodeColor - new color of the node.

draw

public void draw(java.awt.Graphics g)
This method draws the node on the corresponding graphical context normally passed in from the drawing panel.
Specified by:
draw in interface DrawingObj
Following copied from interface: ciips.animation.DrawingObj
Parameters:
g - A reference to the graphical context.