/*
This file is part of the algoviz@vt collection of algorithm
visualizations.
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this distribution. If not, see
.
*/
package bst;
public class Node
{
/* This node's key */
public K key = null;
/* This node's element */
public V value = null;
/* The node's left and right child */
Node left;
Node right;
boolean check=false;
public int height = 0;
/**
* Creates a new node, and sets the left and right children to null.
*/
public Node()
{
left = right = null;
}
/**
* Creates a new node with the specified parameters.
*
* @param key The node's key
* @param e The node's value
* @param l The node's left child
* @param r The node's right child
*/
public Node(K key, V value, Node l, Node r)
{
this.key = key;
this.value = value;
left = l;
right = r;
}
/**
* Convenience method to see if this node is a leaf.
*/
public boolean isLeaf()
{
return (left == null) && (right == null);
}
public void updateHeight()
{
height = 1 + Math.max(left == null ? -1 : left.height, right == null ? -1 : right.height);
//System.out.println("[Height] " + key + ": " + height);
}
/**
* Negative for left heavy, positive for right heavy, 0 for balanced
*
* @return the balance factor
*/
public int getBalanceFactor()
{
//System.out.println("[BFactor] " + key + ": " + ((right == null ? -1 : right.height) - (left == null ? -1 : left.height)));
return (right == null ? -1 : right.height) - (left == null ? -1 : left.height);
}
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode()
{
return value.hashCode();
}
}