Class RedBlackTree<T extends Comparable<? super T>>
java.lang.Object
com.thomas.trees.BinarySearchTree<T>
com.thomas.trees.RedBlackTree<T>
- Type Parameters:
T- The data type of the nodes of the Tree, must be comparable to support ordering
A simple implentation of a Red Black Tree
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal RedBlackNode<T> A Sentinel Null node
Every null point points here instead -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondelete(RedBlackNode<T> node, T value) Deletes a value from a subtree
This is not examinedvoidDeletes a value from the tree This is not examineddeleteMin(RedBlackNode<T> node) Deletes the minimun node from a subtree
As this is a BST we assume that we can just go left to get the minimum valuefixAfterDelete(RedBlackNode<T> node) Restores Red-Black properties on the way back up after a deletion This is not examinedvoidflipColours(RedBlackNode<T> node) Sets the node to RED and its children to BLACKinsert(RedBlackNode<T> node, T value) Inserts a value into a subtree
Also fixes Red-Black violationsvoidInserts a value into the treebooleanisBlack(RedBlackNode<T> node) Returns true if the node is blackbooleanisRed(RedBlackNode<T> node) Returns true if the node is redmoveRedLeft(RedBlackNode<T> node) Ensures the left child, or its left child is red before decending left during deletionmoveRedRight(RedBlackNode<T> node) Ensures the right child, or its left child is red before decending right during deletionroot()Casts the root to a RedBlackNoderotateLeft(RedBlackNode<T> node) Rotates a node leftrotateRight(RedBlackNode<T> node) Rotates a node rightvoidsetParent(RedBlackNode<T> child, RedBlackNode<T> parent) Sets the parent of a child
Guarding against setting the parent to NILMethods inherited from class BinarySearchTree
delete, find, find, findMin, getHeight, getRoot, getSize, in_order, in_order, insert, level_order, level_order, post_order, post_order, pre_order, pre_order, recursiveHeight, recursiveSize, removeMin, setRoot
-
Field Details
-
NIL
A Sentinel Null node
Every null point points here instead
-
-
Constructor Details
-
RedBlackTree
public RedBlackTree()Creates and empty Red-Black Tree
-
-
Method Details
-
root
Casts the root to a RedBlackNode- Returns:
- The root as a RedBlackNode
- See Also:
-
isBlack
Returns true if the node is black- Parameters:
node- The node being checked- Returns:
- Whether the node is black
- See Also:
-
isRed
Returns true if the node is red- Parameters:
node- The node being checked- Returns:
- Whether the node is black
- See Also:
-
setParent
Sets the parent of a child
Guarding against setting the parent to NIL- Parameters:
child- The child nodeparent- The parent node- See Also:
-
flipColours
Sets the node to RED and its children to BLACK- Parameters:
node- The node to flip- See Also:
-
deleteMin
Deletes the minimun node from a subtree
As this is a BST we assume that we can just go left to get the minimum value- Parameters:
node- the subtree- Returns:
- The subtree with the minimum value deleted
- See Also:
-
moveRedLeft
Ensures the left child, or its left child is red before decending left during deletion- Parameters:
node- The node to fix- Returns:
- The fixed node
-
moveRedRight
Ensures the right child, or its left child is red before decending right during deletion- Parameters:
node- The node to fix- Returns:
- The fixed node
-
fixAfterDelete
Restores Red-Black properties on the way back up after a deletion This is not examined- Parameters:
node- The node to fix- Returns:
- The fixed node
-
rotateLeft
Rotates a node left- Parameters:
node- The node that needs to be rotated- Returns:
- The new root of the subtree
-
rotateRight
Rotates a node right- Parameters:
node- The node that needs to be rotated- Returns:
- The new root of the subtree
-
insert
Inserts a value into the tree- Overrides:
insertin classBinarySearchTree<T extends Comparable<? super T>>- Parameters:
value- The data to be inserted- See Also:
-
insert
Inserts a value into a subtree
Also fixes Red-Black violations- Parameters:
node- The current nodevalue- The data to be inserted- Returns:
- A colour balanced subtree
-
delete
Deletes a value from the tree This is not examined- Overrides:
deletein classBinarySearchTree<T extends Comparable<? super T>>- Parameters:
value- The value to be deleted- See Also:
-
delete
Deletes a value from a subtree
This is not examined- Parameters:
node- The curent nodevalue- The value to be deleted- Returns:
- A colour-balanced subtree
-