Class AvlTree<T>
java.lang.Object
com.abstractkamen.datastructures.impl.trees.search.AvlTree<T>
- Type Parameters:
T
- type of elements
- All Implemented Interfaces:
BinarySearchTree<T>
,Iterable<T>
An AVL implementation of the BinarySearchTree interface. Currently, does not support concurrent modification.
-
Constructor Summary
ConstructorDescriptionAvlTree
(Comparator<T> comparator) Create anAvlTree<T>
with a custom comparator and allowed duplicate values.AvlTree
(Comparator<T> comparator, boolean allowDuplicates) Create anAvlTree<T>
with a custom comparator and flag for duplicate values. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds an item to the tree.void
clear()
Remove all elements from this tree.boolean
Check if an item exists in the tree.int
containsCount
(T item) Check how many items are equal toitem
there are in the tree.static <T extends Comparable<T>>
AvlTree<T>Create anAvlTree<T>
with natural order comparator in a type safe way.static <T extends Comparable<T>>
AvlTree<T>createComparable
(boolean allowDuplicates) Create anAvlTree<T>
with natural order comparator in a type safe way and flag for duplicate values.Get an iterator over the elements in this tree, in descending order.Get the first item greater thanitem
.int
height()
Get the height of this tree.boolean
isEmpty()
Returns true if tree has no elements.iterator()
Get the first item lesser thanitem
.max()
Get the maximum item.min()
Get the minimum item.Get a detailed representation of the structure of this tree.void
Removes an item to from the tree.int
size()
Get the current number of elements in the tree.stream()
Get a sequential stream over elements in this tree.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
AvlTree
Create anAvlTree<T>
with a custom comparator and allowed duplicate values.- Parameters:
comparator
- custom comparator
-
AvlTree
Create anAvlTree<T>
with a custom comparator and flag for duplicate values.- Parameters:
comparator
- custom comparatorallowDuplicates
- flag for duplicate values
-
-
Method Details
-
createComparable
Create anAvlTree<T>
with natural order comparator in a type safe way.- Type Parameters:
T
- comparable type
-
createComparable
Create anAvlTree<T>
with natural order comparator in a type safe way and flag for duplicate values.- Type Parameters:
T
- comparable type- Parameters:
allowDuplicates
- flag for duplicate values
-
isEmpty
public boolean isEmpty()Description copied from interface:BinarySearchTree
Returns true if tree has no elements.- Specified by:
isEmpty
in interfaceBinarySearchTree<T>
- Returns:
- true if tree has no elements.
-
size
public int size()Description copied from interface:BinarySearchTree
Get the current number of elements in the tree.- Specified by:
size
in interfaceBinarySearchTree<T>
- Returns:
- current number of elements
-
add
Description copied from interface:BinarySearchTree
Adds an item to the tree.- Specified by:
add
in interfaceBinarySearchTree<T>
- Parameters:
item
- to be added
-
remove
Description copied from interface:BinarySearchTree
Removes an item to from the tree.- Specified by:
remove
in interfaceBinarySearchTree<T>
- Parameters:
item
- to be removed
-
contains
Description copied from interface:BinarySearchTree
Check if an item exists in the tree.- Specified by:
contains
in interfaceBinarySearchTree<T>
- Parameters:
item
- to check- Returns:
- true if item exists in the tree
-
containsCount
Description copied from interface:BinarySearchTree
Check how many items are equal toitem
there are in the tree.- Specified by:
containsCount
in interfaceBinarySearchTree<T>
- Parameters:
item
- to check- Returns:
- number of items equal to
item
-
height
public int height()Description copied from interface:BinarySearchTree
Get the height of this tree.- Specified by:
height
in interfaceBinarySearchTree<T>
- Returns:
- the current height of this tree
-
greater
Description copied from interface:BinarySearchTree
Get the first item greater thanitem
.- Specified by:
greater
in interfaceBinarySearchTree<T>
- Parameters:
item
- to check- Returns:
- found greater item or null if no item is greater
-
lesser
Description copied from interface:BinarySearchTree
Get the first item lesser thanitem
.- Specified by:
lesser
in interfaceBinarySearchTree<T>
- Parameters:
item
- to check- Returns:
- found lesser item or null if no item is lesser
-
min
Description copied from interface:BinarySearchTree
Get the minimum item.- Specified by:
min
in interfaceBinarySearchTree<T>
- Returns:
- minimum item
-
max
Description copied from interface:BinarySearchTree
Get the maximum item.- Specified by:
max
in interfaceBinarySearchTree<T>
- Returns:
- maximum item
-
prettyString
Description copied from interface:BinarySearchTree
Get a detailed representation of the structure of this tree.- Specified by:
prettyString
in interfaceBinarySearchTree<T>
- Returns:
- detailed pretty string
-
stream
Description copied from interface:BinarySearchTree
Get a sequential stream over elements in this tree.- Specified by:
stream
in interfaceBinarySearchTree<T>
- Returns:
- a sequential stream over elements in this tree
-
clear
public void clear()Description copied from interface:BinarySearchTree
Remove all elements from this tree.- Specified by:
clear
in interfaceBinarySearchTree<T>
-
descendingIterator
Description copied from interface:BinarySearchTree
Get an iterator over the elements in this tree, in descending order.- Specified by:
descendingIterator
in interfaceBinarySearchTree<T>
- Returns:
- descending order iterator over the elements in this tree
-
iterator
-
toString
-