Module org.jhotdraw8.icollection
Interface RedBlackTree<K,V>
- Type Parameters:
K- the key typeV- the value type
Purely functional Red/Black Tree, inspired by Kazu Yamamoto's Haskell implementation.
Based on
- Chris Okasaki, "Red-Black Trees in a Functional Setting", Journal of Functional Programming, 9(4), pp 471-477, July 1999
- Stefan Kahrs, "Red-black trees with types", Journal of functional programming, 11(04), pp 425-432, July 2001
This class has been derived from 'vavr' RedBlackTree.java.
- RedBlackTree.java. Copyright 2023 (c) vavr. MIT License.
- github.com
-
Method Summary
Modifier and TypeMethodDescriptionceiling(K e, @NonNull Comparator<? super K> comparator) Returns the leastNodein this tree greater than or equal to the given element, orEmptyif there is no such element.booleancolor()Return theColorof this Red/Black Tree node.booleancontains(K key, Comparator<? super K> comparator) Checks, if thisRedBlackTreecontains the givenkey.default RedBlackTree<K, V> delete(K key, Comparator<? super K> comparator) Deletes a value from this RedBlackTree.static <K,V> RedBlackTree <K, V> empty()Returns the map entry of this RedBlackTree ornullif it is empty.default booleanequals(@NonNull RedBlackTree<K, V> tree) Returns true if the given tree has the same size and the same elements in the same sequence as this tree.find(K key, Comparator<? super K> comparator) Finds the value stored in this tree, if exists, by applying the underlying comparator to the tree elements and the given element.floor(K e, @NonNull Comparator<? super K> comparator) Returns the greatestNodein this tree less than or equal to the given element, orEmptyif there is no such element.getKey()Returns the key of the current tree node or throws if this is empty.getValue()Returns the value of the current tree node or throws if this is empty.higher(K e, @NonNull Comparator<? super K> comparator) Returns the leastNodein this tree strictly greater than the given element, orEmptyif there is no such element.default RedBlackTree<K, V> insert(K key, V value, Comparator<? super K> comparator) Inserts a new value into this tree.booleanisEmpty()Checks if thisRedBlackTreeis empty, i.e.booleanisRed()iterator()Returns an Iterator that iterates elements in the order induced by the underlying Comparator.Returns the key of this RedBlackTree ornullif it is empty.left()Returns the left child if this is a non-empty node, otherwise throws.lower(K e, @NonNull Comparator<? super K> comparator) Returns the greatestNodein this tree strictly less than the given element, orEmptyif there is no such element.<E> @Nullable EmapOrNull(@NonNull BiFunction<K, V, E> f) default @NonNull RedBlackTree<K, V> max()Returns the maximum element of this tree according to the underlying comparator.default @NonNull RedBlackTree<K, V> min()Returns the minimum element of this tree according to the underlying comparator.static <K,V> RedBlackTree <K, V> of(@NonNull Comparator<? super K> comparator, K... keys) static <K,V> RedBlackTree <K, V> of(@NonNull Comparator<? super K> comparator, K key, V value) static <K,V> RedBlackTree <K, V> ofAll(@NonNull Comparator<? super K> comparator, Iterable<? extends K> keys) orElse(RedBlackTree<K, V> other) Returns thisRedBlackTreeif it is nonempty, otherwise return the alternative.Returns an Iterator that iterates elements in the order induced by the underlying Comparator.right()Returns the right child if this is a non-empty node, otherwise throws.intsize()Returns the size of this tree.Returns the value of this RedBlackTree ornullif it is empty.Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
toLispString
String toLispString() -
empty
-
of
-
of
@SafeVarargs static <K,V> RedBlackTree<K,V> of(@NonNull Comparator<? super K> comparator, K... keys) -
ofAll
static <K,V> RedBlackTree<K,V> ofAll(@NonNull Comparator<? super K> comparator, Iterable<? extends K> keys) -
insert
Inserts a new value into this tree.- Parameters:
key- A key.value- A value.comparator-- Returns:
- A new tree if this tree does not contain the given value, otherwise the same tree instance.
-
color
boolean color()Return theColorof this Red/Black Tree node.An empty node is
BLACKby definition.- Returns:
- Either
REDorBLACK.
-
contains
Checks, if thisRedBlackTreecontains the givenkey.- Parameters:
key- A key.comparator-- Returns:
- true, if this tree contains the value, false otherwise.
-
isRed
boolean isRed() -
delete
Deletes a value from this RedBlackTree.- Parameters:
key- A valuecomparator-- Returns:
- A new RedBlackTree if the value is present, otherwise this.
-
find
Finds the value stored in this tree, if exists, by applying the underlying comparator to the tree elements and the given element.Especially the value returned may differ from the given value, even if the underlying comparator states that both are equal.
- Parameters:
key- A valuecomparator-- Returns:
- Some value, if this tree contains a value equal to the given value according to the underlying comparator. Otherwise None.
-
isEmpty
boolean isEmpty()Checks if thisRedBlackTreeis empty, i.e. an instance ofLeaf.- Returns:
- true, if it is empty, false otherwise.
-
left
RedBlackTree<K,V> left()Returns the left child if this is a non-empty node, otherwise throws.- Returns:
- The left child.
- Throws:
UnsupportedOperationException- if this RedBlackTree is empty
-
max
Returns the maximum element of this tree according to the underlying comparator.- Returns:
- Node, if this is not empty, otherwise Empty
-
min
Returns the minimum element of this tree according to the underlying comparator.- Returns:
- Node, if this is not empty, otherwise Empty
-
right
@NonNull RedBlackTree<K,V> right()Returns the right child if this is a non-empty node, otherwise throws.- Returns:
- The right child.
- Throws:
UnsupportedOperationException- if this RedBlackTree is empty
-
size
int size()Returns the size of this tree.- Returns:
- the number of nodes of this tree and 0 if this is the empty tree
-
equals
Returns true if the given tree has the same size and the same elements in the same sequence as this tree.- Parameters:
tree- the given tree- Returns:
- true if equal
-
getKey
K getKey()Returns the key of the current tree node or throws if this is empty.- Returns:
- The value.
- Throws:
NoSuchElementException- if this is the empty node.
-
getValue
V getValue()Returns the value of the current tree node or throws if this is empty.- Returns:
- The value.
- Throws:
NoSuchElementException- if this is the empty node.
-
iterator
Returns an Iterator that iterates elements in the order induced by the underlying Comparator.Internally an in-order traversal of the RedBlackTree is performed.
Example:
4 / \ 2 6 / \ / \ 1 3 5 7Iteration order: 1, 2, 3, 4, 5, 6, 7
-
reverseIterator
Returns an Iterator that iterates elements in the order induced by the underlying Comparator.Internally an in-order traversal of the RedBlackTree is performed.
Example:
4 / \ 2 6 / \ / \ 1 3 5 7Iteration order: 7, 6, 5, 4, 3, 2, 1
-
ceiling
Returns the leastNodein this tree greater than or equal to the given element, orEmptyif there is no such element. -
floor
Returns the greatestNodein this tree less than or equal to the given element, orEmptyif there is no such element. -
higher
Returns the leastNodein this tree strictly greater than the given element, orEmptyif there is no such element. -
lower
Returns the greatestNodein this tree strictly less than the given element, orEmptyif there is no such element. -
orElse
Returns thisRedBlackTreeif it is nonempty, otherwise return the alternative. -
keyOrNull
Returns the key of this RedBlackTree ornullif it is empty. -
valueOrNull
Returns the value of this RedBlackTree ornullif it is empty. -
entryOrNull
Returns the map entry of this RedBlackTree ornullif it is empty. -
mapOrNull
-