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
ConstructorDescriptionConstructs 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 int
Returns the height of the given tree position.protected boolean
isBalanced
(Position<Entry<K, V>> p) Returns whether a position has balance factor between -1 and 1 inclusive.protected void
Utility used to rebalance after an insert or removal operation.protected void
rebalanceDelete
(Position<Entry<K, V>> p) Overrides the TreeMap rebalancing hook that is called after a deletion.protected void
rebalanceInsert
(Position<Entry<K, V>> p) Overrides the TreeMap rebalancing hook that is called after an insertion.protected void
recomputeHeight
(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, treeMin
Methods inherited from class com.zybooks.dsaj.map.AbstractSortedMap
compare, compare, compare, compare
Methods 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:
rebalanceInsert
in classTreeMap<K,
V> - Parameters:
p
- the position which was recently inserted
-
rebalanceDelete
Overrides the TreeMap rebalancing hook that is called after a deletion.- Overrides:
rebalanceDelete
in classTreeMap<K,
V> - Parameters:
p
- the position of the sibling of the removed leaf
-