Skip to content

Instantly share code, notes, and snippets.

@aortiz49
Created April 13, 2020 23:13
Show Gist options
  • Save aortiz49/38302c528ecc004eab92dbe40282b56f to your computer and use it in GitHub Desktop.
Save aortiz49/38302c528ecc004eab92dbe40282b56f to your computer and use it in GitHub Desktop.
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* University of the Andes
* Department of Systems Engineering
* Licensed under Academic Free License version 2.1
* Project 2
* Author: Andy Ortiz
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
package model.data_structures.RedBlackTree;
/**
* Represents a node in the Red-Black tree
*/
public class TreeNode<K extends Comparable<K>, V extends Comparable<V>> {
// -------------------------------------------------------------
// Constants
// -------------------------------------------------------------
/**
* Represents a red link
*/
static final boolean RED = true;
/**
* Represents a black link
*/
static final boolean BLACK = false;
// -------------------------------------------------------------
// Attributes
// -------------------------------------------------------------
/**
* The node's key
*/
private K key;
/**
* The node's value
*/
private V value;
/**
* The node's left subtree
*/
private TreeNode<K, V> left;
/**
* The node's right subtree
*/
private TreeNode<K, V> right;
/**
* The number of nodes in this subtree (including self)
*/
private int nodeCount;
/**
* Node's color from its parent to the node.
*/
private boolean color;
// -------------------------------------------------------------
// Constructor
// -------------------------------------------------------------
/**
* Creates a new tree node.
*
* @param pKey The node's key.
* @param pValue The node's value.
*/
TreeNode(K pKey, V pValue, int pNodeCount, boolean pColor) {
key = pKey;
value = pValue;
nodeCount = pNodeCount;
color = pColor;
}
// -------------------------------------------------------------
// Methods
// -------------------------------------------------------------
/**
* Returns the node's key.
*
* @return the node's key.
*/
public K getKey() {
return key;
}
/**
* Sets the note's key.
*
* @param pKey The key to be set.
*/
public void setKey(K pKey) {
key = pKey;
}
/**
* Returns the node's value.
*
* @return node's value
*/
public V getValue() {
return value;
}
/**
* Sets the value of the node.
*
* @param pValue The node to be set
*/
public void setValue(V pValue) {
value = pValue;
}
/**
* Returns the node's color.
*
* @return node's color
*/
public boolean getColor() {
return color;
}
/**
* Sets the node's color.
*
* @param pColor node's color
*/
void setColor(boolean pColor) {
color = pColor;
}
/**
* Returns the node's left tree.
*
* @return node's left tree
*/
public TreeNode<K, V> getLeft() {
return left;
}
/**
* Set the node's left node.
*
* @param pLeft The left node to be set.
*/
void setLeft(TreeNode<K, V> pLeft) {
this.left = pLeft;
}
/**
* Return's the right node.
*
* @return the right node
*/
public TreeNode<K, V> getRight() {
return right;
}
/**
* Set the node's right node.
*
* @param pRight The right node to be set.
*/
void setRight(TreeNode<K, V> pRight) {
right = pRight;
}
/**
* Returns the number of nodes of the subtrees.
*
* @return the number of nodes of the subtrees
*/
public int getNodeCount() {
return nodeCount;
}
/**
* Sets the number of nodes
*
* @param nodeCount the number of nodes to set
*/
void setNodeCount(int nodeCount) {
this.nodeCount = nodeCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment