public class RedBlackTree<T> extends Object implements Iterable<T>
This implementation is based upon Cormen, Leiserson, Rivest, Stein's Introduction to Algorithms book.
| Modifier and Type | Class and Description |
|---|---|
static class |
RedBlackTree.Node<T>
A red-black tree node is a binary tree node augmented to hold an
additional piece of information called the node's color.
|
| Constructor and Description |
|---|
RedBlackTree()
Default constructor.
|
RedBlackTree(Comparator<? super T> comparator)
Construct a new tree which uses the specified custom comparator.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clear all entries from this tree.
|
boolean |
contains(T value)
Test whether or not the specified value is an element of this tree.
|
protected RedBlackTree.Node<T> |
createNewNode(T value)
Create a new node with the specified value.
|
RedBlackTree.Node<T> |
delete(T value)
Delete the specified value from this tree.
|
protected void |
exchangeValues(RedBlackTree.Node<T> node,
RedBlackTree.Node<T> successor)
Called by
delete(T) when the node to be removed is a leaf. |
protected void |
fixAfterDeletion(RedBlackTree.Node<T> node)
Re-balance the tree after a delete operation.
|
protected void |
fixAfterInsertion(RedBlackTree.Node<T> node)
Re-balance the tree after an insert operation.
|
RedBlackTree.Node<T> |
getFirstNode()
Get the node containing the smallest value held by this tree.
|
RedBlackTree.Node<T> |
getNode(T value)
Get the node that holds the specified value.
|
RedBlackTree.Node<T> |
getPredecessor(RedBlackTree.Node<T> node)
Get the predecessor of the specified node.
|
RedBlackTree.Node<T> |
getRoot()
Get the root of this tree.
|
int |
getSize()
Get the number of elements contained within this tree.
|
RedBlackTree.Node<T> |
getSuccessor(RedBlackTree.Node<T> node)
Get the successor of the specified node.
|
RedBlackTree.Node<T> |
insert(T value)
Insert the specified value into this tree.
|
boolean |
isEmpty() |
Iterator<T> |
iterator()
Returns an Iterator over the elements of this tree.
|
protected void |
leftRotate(RedBlackTree.Node<T> node)
Perform a left rotate operation on the specified node.
|
protected void |
rightRotate(RedBlackTree.Node<T> node)
Perform a right rotate operation on the specified node.
|
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitforEach, spliteratorpublic RedBlackTree()
Comparable.compareTo(Object); for custom ordering see
RedBlackTree(Comparator).public RedBlackTree(Comparator<? super T> comparator)
comparator - the comparator to use when ordering elements.public void clear()
public boolean contains(T value)
value - the query value.protected RedBlackTree.Node<T> createNewNode(T value)
value - the value to apply to the new node.public RedBlackTree.Node<T> delete(T value)
value - the value to delete.protected void exchangeValues(RedBlackTree.Node<T> node, RedBlackTree.Node<T> successor)
delete(T) when the node to be removed is a leaf. In
this case, the node's value is exchanged with its successor as per the
typical binary tree node removal operation. This method allows a subclass
to influence value exchange behavior (e.g. if additional node information
needs to be exchanged).node - the node whose value is to be removed.successor - the node to actually be removed.protected void fixAfterDeletion(RedBlackTree.Node<T> node)
node - the deleted node or the swap node.protected void fixAfterInsertion(RedBlackTree.Node<T> node)
node - the inserted node.public RedBlackTree.Node<T> getFirstNode()
public RedBlackTree.Node<T> getNode(T value)
value - the query value.public RedBlackTree.Node<T> getPredecessor(RedBlackTree.Node<T> node)
node - the node compared to which predecessor node will be foundpublic RedBlackTree.Node<T> getRoot()
public int getSize()
public RedBlackTree.Node<T> getSuccessor(RedBlackTree.Node<T> node)
node - the node compared to which successor node will be foundpublic RedBlackTree.Node<T> insert(T value)
value - the value to insert.public boolean isEmpty()
protected void leftRotate(RedBlackTree.Node<T> node)
node - the node on which the left rotate operation will be performed.protected void rightRotate(RedBlackTree.Node<T> node)
node - the node on which a right rotate operation is to be performed.Copyright © 1994–2024 Peter Murray-Rust. All rights reserved.