Package com.zybooks.dsaj.searchtree
Class AVLTreeMap<K,V>
java.lang.Object
com.zybooks.dsaj.map.AbstractMap<K,V>
com.zybooks.dsaj.map.AbstractSortedMap<K,V>
com.zybooks.dsaj.searchtree.TreeMap<K,V>
com.zybooks.dsaj.searchtree.AVLTreeMap<K,V>
- Type Parameters:
K- The key type (keys must be unique and comparable)V- The value type
An implementation of a sorted map using an AVL tree.
-
Nested Class Summary
Nested classes/interfaces inherited from class com.zybooks.dsaj.searchtree.TreeMap
TreeMap.BalanceableBinaryTree<K,V> Nested classes/interfaces inherited from class com.zybooks.dsaj.map.AbstractMap
AbstractMap.MapEntry<K,V> -
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty map using the natural ordering of keys.AVLTreeMap(Comparator<K> comp) Constructs an empty map using the given comparator to order keys. -
Method Summary
Modifier and TypeMethodDescriptionprotected intReturns the height of the given tree position.protected booleanisBalanced(Position<Entry<K, V>> p) Returns whether a position has balance factor between -1 and 1 inclusive.protected voidUtility used to rebalance after an insert or removal operation.protected voidrebalanceDelete(Position<Entry<K, V>> p) Overrides the TreeMap rebalancing hook that is called after a deletion.protected voidrebalanceInsert(Position<Entry<K, V>> p) Overrides the TreeMap rebalancing hook that is called after an insertion.protected voidrecomputeHeight(Position<Entry<K, V>> p) Recomputes the height of the given position based on its children's heights.tallerChild(Position<Entry<K, V>> p) Returns a child of p with height no smaller than that of the other child.Methods inherited from class com.zybooks.dsaj.searchtree.TreeMap
ceilingEntry, dump, entrySet, firstEntry, floorEntry, get, higherEntry, isExternal, isInternal, isRoot, lastEntry, left, lowerEntry, oneSidedAncestor, parent, put, rebalanceAccess, remove, restructure, right, root, rotate, set, sibling, size, subMap, treeMax, treeMinMethods inherited from class com.zybooks.dsaj.map.AbstractSortedMap
compare, compare, compare, compareMethods inherited from class com.zybooks.dsaj.map.AbstractMap
isEmpty, keySet, values
-
Constructor Details
-
AVLTreeMap
public AVLTreeMap()Constructs an empty map using the natural ordering of keys. -
AVLTreeMap
Constructs an empty map using the given comparator to order keys.- Parameters:
comp- comparator defining the order of keys in the map
-
-
Method Details
-
height
Returns the height of the given tree position. -
recomputeHeight
Recomputes the height of the given position based on its children's heights. -
isBalanced
Returns whether a position has balance factor between -1 and 1 inclusive. -
tallerChild
Returns a child of p with height no smaller than that of the other child. -
rebalance
Utility used to rebalance after an insert or removal operation. This traverses the path upward from p, performing a trinode restructuring when imbalance is found, continuing until balance is restored. -
rebalanceInsert
Overrides the TreeMap rebalancing hook that is called after an insertion.- Overrides:
rebalanceInsertin classTreeMap<K,V> - Parameters:
p- the position which was recently inserted
-
rebalanceDelete
Overrides the TreeMap rebalancing hook that is called after a deletion.- Overrides:
rebalanceDeletein classTreeMap<K,V> - Parameters:
p- the position of the sibling of the removed leaf
-