public class AvlTreeMapImpl<K,V> extends java.lang.Object implements AvlTreeMap<K,V>
| Modifier and Type | Field and Description |
|---|---|
private boolean |
allowDuplicates
flag to allow storing duplicate keys
|
private LinkedAvlMapNode<K,V> |
first
node representing the start of the doubly linked list formed with the tree nodes
|
private java.util.Comparator<K> |
keyComparator
The Comparator used for comparing the keys
|
private LinkedAvlMapNode<K,V> |
last
node representing the end of the doubly linked list formed with the tree nodes
|
private LinkedAvlMapNode<K,V> |
root
the root of the tree
|
private int |
size
size of the map
|
private java.util.Comparator<V> |
valueComparator
The Comparator used for comparing the values
|
| Constructor and Description |
|---|
AvlTreeMapImpl(java.util.Comparator<K> keyComparator,
java.util.Comparator<V> valueComparator)
Creates a new instance of AVLTreeMap without support for duplicate keys.
|
AvlTreeMapImpl(java.util.Comparator<K> keyComparator,
java.util.Comparator<V> valueComparator,
boolean allowDuplicates)
Creates a new instance of AVLTreeMap without support for duplicate keys.
|
| Modifier and Type | Method and Description |
|---|---|
private void |
balance(java.util.List<LinkedAvlMapNode<K,V>> treePath)
Balances the tree by visiting the nodes present in the List of nodes present in the
treePath parameter.
This really does the balancing if the height of the tree is greater than 2 and the balance factor is greater than +1 or less than -1. For an excellent info please read the Wikipedia article on AVL tree. |
private void |
balanceNodesAfterRemove(java.util.List<LinkedAvlMapNode<K,V>> treePath,
LinkedAvlMapNode<K,V> delNode)
changes the order of nodes after a delete operation and then
balances the tree
|
private void |
detachNodes(LinkedAvlMapNode<K,V> node,
LinkedAvlMapNode<K,V> parentNode)
Detach a LinkedAvlMapNode from its parent
|
private LinkedAvlMapNode<K,V> |
fetchNonNullNode(K key,
LinkedAvlMapNode<K,V> startNode,
LinkedAvlMapNode<K,V> parent) |
LinkedAvlMapNode<K,V> |
find(K key)
Find a LinkedAvlMapNode with the given key value in the tree.
|
private LinkedAvlMapNode<K,V> |
find(K key,
LinkedAvlMapNode<K,V> startNode) |
private java.util.List<LinkedAvlMapNode<K,V>> |
find(K key,
LinkedAvlMapNode<K,V> startNode,
java.util.List<LinkedAvlMapNode<K,V>> path)
Find a LinkedAvlMapNode with the given key value in the tree starting from the startNode.
|
LinkedAvlMapNode<K,V> |
find(K key,
V value)
Find a LinkedAvlMapNode with the given key and value in the tree.
|
LinkedAvlMapNode<K,V> |
findGreater(K key)
Finds a LinkedAvlMapNode
|
LinkedAvlMapNode<K,V> |
findGreaterOrEqual(K key)
Finds a LinkedAvlMapNode
|
LinkedAvlMapNode<K,V> |
findLess(K key)
Finds a LinkedAvlMapNode
|
LinkedAvlMapNode<K,V> |
findLessOrEqual(K key)
Finds a LinkedAvlMapNode
|
private java.util.List<LinkedAvlMapNode<K,V>> |
findMax(LinkedAvlMapNode<K,V> startNode)
Find the LinkedAvlMapNode having the max key value in the tree starting from the startNode.
|
private java.util.List<LinkedAvlMapNode<K,V>> |
findMin(LinkedAvlMapNode<K,V> startNode)
Find the LinkedAvlMapNode having the min key value in the tree starting from the startNode.
|
private int |
getBalance(LinkedAvlMapNode<K,V> node)
Get balance-factor of the given LinkedAvlMapNode.
|
LinkedAvlMapNode<K,V> |
getFirst() |
java.util.Comparator<K> |
getKeyComparator() |
java.util.List<K> |
getKeys() |
LinkedAvlMapNode<K,V> |
getLast() |
LinkedAvlMapNode<K,V> |
getRoot() |
int |
getSize()
returns the number of nodes present in this tree.
|
java.util.Comparator<V> |
getValueComparator() |
V |
insert(K key,
V value)
Inserts a LinkedAvlMapNode with the given key and value.
|
private V |
insertDupKey(V value,
LinkedAvlMapNode<K,V> existingNode) |
private void |
insertInList(LinkedAvlMapNode<K,V> node,
LinkedAvlMapNode<K,V> parentNode,
int pos) |
boolean |
isDupsAllowed()
tells if the duplicate keys are supported or not.
|
boolean |
isEmpty()
Tests if the tree is logically empty.
|
void |
printTree()
Prints the contents of AVL tree in pretty format
|
SingletonOrOrderedSet<V> |
remove(K key)
Removes a node associated with the given key
The entire node will be removed irrespective of whether duplicate keys
are enabled or not
|
V |
remove(K key,
V value)
Removes the LinkedAvlMapNode present in the tree with the given key and value
|
void |
removeAll()
removes all the nodes from the tree
|
private void |
removeFromList(LinkedAvlMapNode<K,V> node) |
private void |
replaceNode(LinkedAvlMapNode<K,V> deleteNode,
LinkedAvlMapNode<K,V> replaceNode,
LinkedAvlMapNode<K,V> parentNode)
Replace a LinkedAvlMapNode to be removed with a new existing LinkedAvlMapNode
|
private void |
rotateSingleLeft(LinkedAvlMapNode<K,V> node,
LinkedAvlMapNode<K,V> parentNode)
Rotate the node left side once.
|
private void |
rotateSingleRight(LinkedAvlMapNode<K,V> node,
LinkedAvlMapNode<K,V> parentNode)
Rotate the node right side once.
|
(package private) void |
setFirst(LinkedAvlMapNode<K,V> first)
Set the first element of the tree
Note : this method is used by the deserialization method
|
(package private) void |
setLast(LinkedAvlMapNode<K,V> last)
Set the last element of the tree
Note : this method is used by the deserialization method
|
(package private) void |
setRoot(LinkedAvlMapNode<K,V> root)
Set the root of the tree.
|
private void |
visit(LinkedAvlMapNode<K,V> node,
LinkedAvlMapNode<K,V> parentNode) |
private LinkedAvlMapNode<K,V> root
private java.util.Comparator<K> keyComparator
private java.util.Comparator<V> valueComparator
private LinkedAvlMapNode<K,V> first
private LinkedAvlMapNode<K,V> last
private boolean allowDuplicates
private int size
public AvlTreeMapImpl(java.util.Comparator<K> keyComparator, java.util.Comparator<V> valueComparator)
keyComparator - the comparator to be used for comparing keysvalueComparator - the comparator to be used for comparing valuespublic AvlTreeMapImpl(java.util.Comparator<K> keyComparator, java.util.Comparator<V> valueComparator, boolean allowDuplicates)
keyComparator - the comparator to be used for comparing keysvalueComparator - the comparator to be used for comparing valuesallowDuplicates - are duplicates keyComparators allowed?public java.util.Comparator<K> getKeyComparator()
getKeyComparator in interface AvlTreeMap<K,V>public java.util.Comparator<V> getValueComparator()
getValueComparator in interface AvlTreeMap<K,V>public V insert(K key, V value)
insert in interface AvlTreeMap<K,V>key - the item to be insertedvalue - the value associated with the keyprivate V insertDupKey(V value, LinkedAvlMapNode<K,V> existingNode)
private void removeFromList(LinkedAvlMapNode<K,V> node)
private void insertInList(LinkedAvlMapNode<K,V> node, LinkedAvlMapNode<K,V> parentNode, int pos)
public SingletonOrOrderedSet<V> remove(K key)
remove in interface AvlTreeMap<K,V>key - the key of the node to be removedpublic V remove(K key, V value)
remove in interface AvlTreeMap<K,V>key - the key of the node to be removedvalue - the value of the nodeprivate void balanceNodesAfterRemove(java.util.List<LinkedAvlMapNode<K,V>> treePath, LinkedAvlMapNode<K,V> delNode)
treePath - the path traversed to find the node tempdelNode - the node to be deletedprivate void balance(java.util.List<LinkedAvlMapNode<K,V>> treePath)
treePath - the traversed list of LinkedAvlMapNodes after performing an insert/delete operation.public boolean isEmpty()
isEmpty in interface AvlTreeMap<K,V>public int getSize()
getSize in interface AvlTreeMap<K,V>void setRoot(LinkedAvlMapNode<K,V> root)
root - the root of the treevoid setFirst(LinkedAvlMapNode<K,V> first)
first - the first element to be addedvoid setLast(LinkedAvlMapNode<K,V> last)
last - the last element to be addedpublic LinkedAvlMapNode<K,V> getRoot()
getRoot in interface AvlTreeMap<K,V>public java.util.List<K> getKeys()
getKeys in interface AvlTreeMap<K,V>public void printTree()
printTree in interface AvlTreeMap<K,V>public LinkedAvlMapNode<K,V> getFirst()
getFirst in interface AvlTreeMap<K,V>public LinkedAvlMapNode<K,V> getLast()
getLast in interface AvlTreeMap<K,V>private void rotateSingleLeft(LinkedAvlMapNode<K,V> node, LinkedAvlMapNode<K,V> parentNode)
node - the LinkedAvlMapNode to be rotatedparentNode - parent LinkedAvlMapNode of nodeprivate void rotateSingleRight(LinkedAvlMapNode<K,V> node, LinkedAvlMapNode<K,V> parentNode)
node - the LinkedAvlMapNode to be rotatedparentNode - parent LinkedAvlMapNode of nodeprivate void detachNodes(LinkedAvlMapNode<K,V> node, LinkedAvlMapNode<K,V> parentNode)
node - the LinkedAvlMapNode to be detachedparentNode - the parent LinkedAvlMapNode of the nodeprivate void replaceNode(LinkedAvlMapNode<K,V> deleteNode, LinkedAvlMapNode<K,V> replaceNode, LinkedAvlMapNode<K,V> parentNode)
deleteNode - the LinkedAvlMapNode to be deletedreplaceNode - the LinkedAvlMapNode to replace the deleteNodeparentNode - the parent LinkedAvlMapNode of deleteNodeprivate java.util.List<LinkedAvlMapNode<K,V>> find(K key, LinkedAvlMapNode<K,V> startNode, java.util.List<LinkedAvlMapNode<K,V>> path)
key - the key to findstartNode - starting node of a subtree/treepath - the list to be filled with traversed nodespublic LinkedAvlMapNode<K,V> findGreater(K key)
findGreater in interface AvlTreeMap<K,V>key - the keypublic LinkedAvlMapNode<K,V> findGreaterOrEqual(K key)
findGreaterOrEqual in interface AvlTreeMap<K,V>key - the keypublic LinkedAvlMapNode<K,V> findLess(K key)
findLess in interface AvlTreeMap<K,V>key - the keypublic LinkedAvlMapNode<K,V> findLessOrEqual(K key)
findLessOrEqual in interface AvlTreeMap<K,V>key - the keyprivate LinkedAvlMapNode<K,V> fetchNonNullNode(K key, LinkedAvlMapNode<K,V> startNode, LinkedAvlMapNode<K,V> parent)
public LinkedAvlMapNode<K,V> find(K key)
find in interface AvlTreeMap<K,V>key - the key to findpublic LinkedAvlMapNode<K,V> find(K key, V value)
find in interface AvlTreeMap<K,V>key - the key of the nodevalue - the value of the nodeprivate LinkedAvlMapNode<K,V> find(K key, LinkedAvlMapNode<K,V> startNode)
private java.util.List<LinkedAvlMapNode<K,V>> findMax(LinkedAvlMapNode<K,V> startNode)
startNode - starting node of a subtree/treeprivate java.util.List<LinkedAvlMapNode<K,V>> findMin(LinkedAvlMapNode<K,V> startNode)
startNode - starting node of a subtree/treeprivate int getBalance(LinkedAvlMapNode<K,V> node)
node - a LinkedAvlMapNodeprivate void visit(LinkedAvlMapNode<K,V> node, LinkedAvlMapNode<K,V> parentNode)
public boolean isDupsAllowed()
isDupsAllowed in interface AvlTreeMap<K,V>public void removeAll()