Dynamic Dictionaries Primary Operations: get(key) => search put(key, element) => insert remove(key) => delete Additional operations: ascend() get(index) remove(index)
Complexity Of Dictionary Operations get(), put() and remove() n is number of elements in dictionary
Complexity Of Other Operations ascend(), get(index), remove(index) D is number of buckets
The Operation put() Put a pair whose key is
The Operation remove() Three cases: Element is in a leaf. Element is in a degree 1 node. Element is in a degree 2 node.
Remove From A Leaf Remove a leaf element. key =
Remove From A Degree 1 Node Remove from a degree 1 node. key =
Remove From A Degree 1 Node (contd.) Remove from a degree 1 node. key =
Remove From A Degree 2 Node Remove from a degree 2 node. key =
Remove From A Degree 2 Node Replace with largest key in left subtree (or smallest in right subtree)
Remove From A Degree 2 Node Replace with largest key in left subtree (or smallest in right subtree).
Remove From A Degree 2 Node Replace with largest key in left subtree (or smallest in right subtree).
Remove From A Degree 2 Node Largest key must be in a leaf or degree 1 node
Another Remove From A Degree 2 Node Remove from a degree 2 node. key =
Remove From A Degree 2 Node Replace with largest in left subtree
Remove From A Degree 2 Node Replace with largest in left subtree.
Remove From A Degree 2 Node Replace with largest in left subtree.
Remove From A Degree 2 Node Complexity is O(height). 35 7
Yet Other Operations Priority Queue Motivated Operations: find max and/or min remove max and/or min initialize meld
Max And/Or Min Follow rightmost path to max element. Follow leftmost path to min element. Search and/or remove => O(h) time
Initialize Sort n elements. Initialize search tree. Output in inorder (O(n)). Initialize must take O(n log n) time, because it isn’t possible to sort faster than O(n log n)
Meld
Meld And Merge Merge 2 sorted lists A and B. Create binary search trees for A and B. Meld the trees. Output in inorder. Comparisons 0 f(m,n) 0 So, using meld, we can merge using f(m,n) comparisons.
Meld And Merge Worst-case number of comparisons to merge two sorted lists of size n each is n-1. So, complexity of melding two binary search trees of size n each is (n). So, logarithmic time melding isn’t possible.
O(log n) Height Trees Full binary trees. Exist only when n = 2 k –1. Complete binary trees. Exist for all n. Cannot insert/delete in O(log n) time =
Balanced Search Trees Height balanced. AVL (Adelson-Velsky and Landis) trees Weight Balanced. Degree Balanced. 2-3 trees trees red-black trees B-trees
AVL Tree binary tree for every node x, define its balance factor balance factor of x = height of left subtree of x – height of right subtree of x balance factor of every node x is – 1, 0, or 1
Balance Factors This is an AVL tree.
Height Of An AVL Tree The height of an AVL tree that has n nodes is at most 1.44 log 2 (n+2). The height of every n node binary tree is at least log 2 (n+1). log 2 (n+1) <= height <= 1.44 log 2 (n+2)
Proof Of Upper Bound On Height Let N h = min # of nodes in an AVL tree whose height is h. N 0 = 0. N 1 = 1.
N h, h > 1 Both L and R are AVL trees. The height of one is h-1. The height of the other is h-2. The subtree whose height is h-1 has N h-1 nodes. The subtree whose height is h-2 has N h-2 nodes. So, N h = N h-1 + N h LR
Fibonacci Numbers F 0 = 0, F 1 = 1. F i = F i-1 + F i-2, i > 1. N 0 = 0, N 1 = 1. N h = N h-1 + N h-2 + 1, i > 1. N h = F h+2 – 1. F i ~ i /sqrt(5). sqrt
AVL Search Tree