Download presentation
Presentation is loading. Please wait.
Published bySarah Floyd Modified over 6 years ago
1
Dynamic Dictionaries Primary Operations: Additional operations:
get(key) => search put(key, element) => insert remove(key) => delete Additional operations: ascend() get(index) remove(index) Other additional ops: nearest match and range search
2
Complexity Of Dictionary Operations get(), put() and remove()
n is number of elements in dictionary Worst-case for hash table may be made O(log n) using balanced search trees for overflow handling. C++ STL and Java use linear open addressing.
3
Complexity Of Other Operations ascend(), get(index), remove(index)
D is number of buckets Hash table get and remove using O(n) select would take O(D + n) time.
4
The Operation put() Put a pair whose key is 35. 20 10 6 2 8 15 40 30
25 35 This slide begins a review of operations on an unbalanced binary search tree. Put a pair whose key is 35.
5
The Operation remove()
Three cases: Element is in a leaf. Element is in a degree 1 node. Element is in a degree 2 node.
6
Remove From A Leaf Remove a leaf element. key = 7 20 10 6 2 8 15 40 30
25 35 7 18 Remove a leaf element. key = 7
7
Remove From A Degree 1 Node
20 10 6 2 8 15 40 30 25 35 7 18 Remove from a degree 1 node. key = 40
8
Remove From A Degree 1 Node (contd.)
20 10 6 2 8 15 40 30 25 35 7 18 Remove from a degree 1 node. key = 15
9
Remove From A Degree 2 Node
20 10 6 2 8 15 40 30 25 35 7 18 Remove from a degree 2 node. key = 10
10
Remove From A Degree 2 Node
20 10 40 6 15 30 18 25 35 2 8 Replace with largest key in left subtree (or smallest in right subtree). 7
11
Remove From A Degree 2 Node
20 10 40 6 15 30 18 25 35 2 8 Replace with largest key in left subtree (or smallest in right subtree). 7
12
Remove From A Degree 2 Node
20 8 40 6 15 30 18 25 35 2 8 Replace with largest key in left subtree (or smallest in right subtree). 7
13
Remove From A Degree 2 Node
20 8 40 6 15 30 18 25 35 2 8 7 Largest key must be in a leaf or degree 1 node.
14
Another Remove From A Degree 2 Node
20 10 6 2 8 15 40 30 25 35 7 18 Remove from a degree 2 node. key = 20
15
Remove From A Degree 2 Node
20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.
16
Remove From A Degree 2 Node
20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.
17
Remove From A Degree 2 Node
18 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.
18
Remove From A Degree 2 Node
18 10 40 6 15 30 25 35 2 8 7 Complexity is O(height).
19
Yet Other Operations Priority Queue Motivated Operations:
find max and/or min remove max and/or min initialize meld Insert in PQ is same as insert in dictionary
20
Max And/Or Min Follow rightmost path to max element.
20 10 6 2 8 15 40 30 25 Could keep a bst plus 2 variables: min and max. Now, O(1) to find min and max elements. Follow rightmost path to max element. Follow leftmost path to min element. Search and/or remove => O(h) time.
21
Initialize Sort n elements.
20 10 6 2 8 15 40 30 25 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).
22
Meld 5 1 7 17 12 10 6 2 8 15 10 6 2 1 5 8 17 15 12 7 We can merge two sorted lists by melding the corresponding binary search trees. Create a binary search tree for each sorted listed that is to be merged0 compares (e.g., left skewed, right skewed, balanced, …) Meld Create merged list by inorder traversal (0 compares) The only place compares may occur is in the meld.
23
Meld And Merge Worst-case number of comparisons to merge two sorted lists of size n each is 2n-1. So, complexity of melding two binary search trees of size n each is W(n). So, logarithmic time melding isn’t possible.
24
O(log n) Height Trees = + Full binary trees. Complete binary trees.
Exist only when n = 2k –1. Complete binary trees. Exist for all n. Cannot insert/delete in O(log n) time. 8 2 1 6 15 10 10 6 2 8 15 All nodes are affected by the insert. = + 1
25
Balanced Search Trees Height balanced. Weight Balanced.
AVL (Adelson-Velsky and Landis) trees Weight Balanced. Degree Balanced. 2-3 trees 2-3-4 trees red-black trees B-trees Red-black trees are a clever binary representation of Because of time limitations, we shall not see this correspondence in class.
26
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
27
Balance Factors -1 1 1 -1 1 -1 This is an AVL tree.
28
Height Of An AVL Tree The height of an AVL tree that has n nodes is at most 1.44 log2 (n+2). The height of every n node binary tree is at least log2 (n+1). log2 (n+1) <= height <= 1.44 log2 (n+2)
29
Proof Of Upper Bound On Height
Let Nh = min # of nodes in an AVL tree whose height is h. N0 = 0. N1 = 1.
30
Nh, h > 1 L R 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 Nh-1 nodes. The subtree whose height is h-2 has Nh-2 nodes. So, Nh = Nh-1 + Nh First, observe that N increases with h as each of Nh’s subtrees is AVL and of smaller height (at least one has height h-1).
31
Fibonacci Numbers F0 = 0, F1 = 1. Fi = Fi-1 + Fi-2 , i > 1.
N0 = 0, N1 = 1. Nh = Nh-1 + Nh-2 + 1, i > 1. Nh = Fh+2 – 1. Fi ~ fi/sqrt(5). f = (1 + sqrt(5))/2 height <= 1.44 log2 (n+2) follows Note that for Fibonacci heaps Ni = Fi+2. Also, for Fibonacci heaps, i is the degree (not height).
32
AVL Search Tree 1 -1 10 7 8 3 5 30 40 20 25 35 45 60 AVL search tree is often simply referred to as an AVL tree.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.