Download presentation
Presentation is loading. Please wait.
Published byMagdalene Edwards Modified over 9 years ago
1
Binary Search Trees Cormen (cap 12, Edition 3) Estruturas de Dados e seus Algoritmos (Cap 4)
2
2 Dictionary Data Structures Goal: – Design a data structure to store a small set of keys S={k 1,k 2,..,k n } from a large universe U. – It shall efficiently support –Query(x): determine whether a key x is in S or not –Insert(x): Add x to the set S if x is not there –Delete(x): Remove x from S if x is there Additional Goals – Low memory consumption – Efficient construction
3
3 Dictionary Data Structures Linked Lists Query(x): O(n) time Insert(x): Insert at the beginning of the list: O(1) time Delete(x): Find and then remove, O(n) time Construction time: O(n) time Space consumption: O(n)
4
4 Binary Search Trees n A Binary search tree(BST) T for a list K= (k 1 < ··· < k n ) of n keys is a rooted tree that satisfies the following properties: – It has n internal nodes and n+1 leaves – Each internal node of T is associated with a distinct key – [Search Property] If node v is associated with key k i then: all nodes at the left subtree of v are associated with keys smaller than k i all nodes at the right subtree of v are associated with keys larger than k i
5
How does the BST works? Search Property: x y x
6
How does the BST works? Search Property: x y x x z
7
7 Binary Search Trees n Binary Search Trees k2k2 k1k1 k4k4 k3k3 k5k5
8
8 Binary Search Trees n Keys are elements from a totally ordered set U – U can be the set of integers – U can be the set of students from a university
9
9 Binary Search Trees n Additional Properties – The key with minimum value is stored in the leftmost node of the tree – The key with maximum value is stored in the righttmost node of the tree
10
10 Binary Search Trees n Basic Operations – Query(x): Determine whether x belongs to T or not – Insert(x): if x is not in T, then insert x in T. – Delete(x): If x in T, then remove x from T
11
BST: Query(x) Algorithm Query(x) If x = leaf then Return “element was not found” End If If x = root then Return “element was found” Else if x < root then search in the left subtree else search in the right subtree End If
12
12 Binary Search Trees n Query(x) k2k2 k1k1 k4k4 k3k3 k5k5
13
Inseting a new key Add a new element in the tree at the correct position in order to keep the search property. Algorithm Insert(x, T) If x = root.key then Return ‘x is already in T’ End If If root(T) is a leaf then Associate the leaf with x Return End If If x < root.key then Insert (x, left tree of T) else Insert (x, right tree of T) End If
14
Example: Insert(50), Insert(20), Insert(39), Insert(8), Insert(79), Insert(26) 50 20 839 26 79 Inseting a new key
15
Removing a node in a BST SITUATIONS: Removing a leaf Removing an internal node with a unique child Removing an internal node with two children
16
Removing a Leaf 6 2 1 8 3 4
17
6 2 1 8 3 4
18
6 2 1 8 4 6 2 1 8 4 3
19
Removing a node in a BST SITUATIONS: Removing a leaf Removing an internal node with a unique child Removing an internal node with two children
20
It is necessary to correct the pointer, “jumping” the node: the only grandchild becomes the right (left) son. Removing an internal node with a unique child
21
6 2 1 8 3 4
22
6 2 1 8 3 4
23
6 2 1 8 3 4
24
6 2 1 8 3 6 2 1 8 3 4
25
Removing a node in a BST SITUATIONS: Removing a leaf Removing an internal node with a unique child Removing an internal node with two children
26
Find the element which preceeds the element to be removed considering the ordering n (this corresponds to remove the rightmost element from the left subtree) Switch the information of the node to be removed with the node found Removing an internal node with two children
27
6 2 1 8 3 4
28
6 2 1 8 3 4 6 2 1 8 3 4
29
6 2 1 8 3 4
30
6 2 1 8 3 4 4 2 1 8 3 6
31
4 2 1 8 3 6
32
4 2 1 8 3 6
33
4 2 1 8 3 6 4 2 1 8 3 6
34
34 Binary Search Trees: Operations Complexity n Basic Operations – Query(x): Determine whether x belongs to T or not Number of operations = O( height of T) – Insert(x): if x is not in T, then insert x in T. Number of operations = O( height of T) – Delete(x): If x in T, then remove x from T Number of operations = O( height of T) – Max(T) and Min(T) Number of operations = O( height of T)
35
35 Binary Search Trees: Operations Complexity n Basic Operations – Query(x): Determine whether x belongs to T or not Number of operations = O( height of T) – Insert(x): if x is not in T, then insert x in T. Number of operations = O( height of T) – Delete(x): If x in T, then remove x from T Number of operations = O( height of T) – Max(T) and Min(T) Number of operations = O( height of T) n Shallow trees are desirable
36
36 Binary Search Trees: Construction Simple Approach: let k 1,…, k n be the set of key not necessarily ordered. Proceed as follows: insert( k 1 ), insert( k 2 ),..., insert( k n )
37
Example: 50, 20, 39, 8, 79, 26, 58, 15, 88, 4, 85, 96, 71, 42, 53. 50 20 8 415 39 2642 79 58 5371 88 8596
38
38 Binary Search Trees: Construction Simple Approach: let k 1,…, k n be the set of key not necessarily ordered. Proceed as follows: insert( k 1 ), insert( k 2 ),..., insert( k n ) The structure has height O(n) if the set of keys is ordered. For a random permutation of the n first integers, its expected height is O(log n) (Cormen, 12.3)
39
39 Binary Search Trees: Construction Simple Approach: let k 1,…, k n be the set of key not necessarily ordered. Proceed as follows: Sort the keys BST(1:n) root(T) ‘median key’ left(root) <- BST(1,n/2) right(root) <- BST(n/2+1,n) End
40
Relation between #nodes and height of a binary tree At each level the number of nodes duplicates, such that for a binary tree with height h we have at most: 2 0 + 2 1 + 2 2 +...+ 2 h-1 = 2 h – 1 nodes
41
Relation between #nodes and height of a binary tree At each level the number of nodes duplicates, such that for a binary tree with height h we have at most: 2 0 + 2 1 + 2 2 +...+ 2 h-1 = 2 h – 1 nodes Or equivalently: The height of every binary search tree with n nodes is at least log n
42
The tree may become unbalanced Remove: node 8 6 2 1 8 3 4 6 2 1 3 4
43
The tree may become unbalanced Remove: node 8 Remove node 1 6 2 1 8 3 4 6 2 1 3 4
44
The tree may become unbalanced Remove: node 8 Remove node 1 6 2 1 8 3 4 6 2 1 3 4 6 2 3 4
45
The tree may become unbalanced The binary tree may become degenerate after operations of insertion and remotion: becoming a list, for example.
46
Balanced Trees Cormen (cap 13, Edition 3) Estruturas de Dados e seus Algoritmos (Cap 5)
47
AVL TREES (Adelson-Velskii and Landis 1962) BST trees that maintain a reasonable balanced tree all the time. Key idea: if insertion or deletion get the tree out of balance then fix it immediately All operations insert, delete,… can be done on an AVL tree with N nodes in O(log N) time (worst case)
48
AVL TREES AVL Tree Property: It is a BST in which the heights of the left and right subtrees of the root differ by at most 1 and in which the right and left subtrees are also AVL trees Height: length of the longest path from the root to a leaf.
49
AVL TREES: Example: An example of an AVL tree where the heights are shown next to the nodes: 88 44 1778 3250 4862 2 4 1 1 2 3 1 1
50
AVL TREES Other Examples:
51
AVL TREES Other Examples:
52
Let r be the root of an AVL tree of height h Let N h denote the minimum number of nodes in an AVL tree of height h Relation between #nodes and height of na AVL tree
53
Let r be the root of an AVL tree of height h Let N h denote the minimum number of nodes in an AVL tree of height h r Te Td T Relation between #nodes and height of na AVL tree
54
Let r be the root of an AVL tree of height h Let N h denote the minimum number of nodes in an AVL tree of height h r Te Td h-1 T Relation between #nodes and height of na AVL tree
55
Let r be the root of an AVL tree of height h Let N h denote the minimum number of nodes in an AVL tree of height h r Te Td h-1h-1 ou h-2 T Relation between #nodes and height of na AVL tree
56
Let r be the root of an AVL tree of height h Let N h denote the minimum number of nodes in an AVL tree of height h It grows faster than Fibonacci series Nh ≥ 1.618 h-2 r Te Td h-1h-1 ou h-2 T Nh ≥ 1 + Nh-1 + Nh-2 Relation between #nodes and height of na AVL tree
57
Let r be the root of an AVL tree of height h Let N h denote the minimum number of nodes in an AVL tree of height h It grows faster than Fibonacci series Nh ≥ 1.618 h-2 Height of AVL Tree <= 1.44 log N (N is the number of nodes) r Te Td h-1h-1 ou h-2 T Nh ≥ 1 + Nh-1 + Nh-2 Relation between #nodes and height of na AVL tree
58
58
59
Relation between #nodes and height of na AVL tree Nh ≥ 1 + Nh-1 + Nh-2 ≥ 2Nh-2 + 1
60
Relation between #nodes and height of na AVL tree Nh ≥ 1 + Nh-1 + Nh-2 ≥ 2Nh-2 + 1 ≥ 2Nh-2
61
Relation between #nodes and height of na AVL tree Nh ≥ 1 + Nh-1 + Nh-2 ≥ 2Nh-2 + 1 ≥ 2Nh-2 ≥ 2(2Nh-4)
62
Relation between #nodes and height of na AVL tree Nh ≥ 1 + Nh-1 + Nh-2 ≥ 2Nh-2 + 1 ≥ 2Nh-2 ≥ 2(2Nh-4) ≥ 2 2 (Nh-4)
63
Relation between #nodes and height of na AVL tree Nh ≥ 1 + Nh-1 + Nh-2 ≥ 2Nh-2 + 1 ≥ 2Nh-2 ≥ 2(2Nh-4) ≥ 2 2 (Nh-4) ≥ 2 2 (2 Nh-6)
64
Relation between #nodes and height of na AVL tree Nh ≥ 1 + Nh-1 + Nh-2 ≥ 2Nh-2 + 1 ≥ 2Nh-2 ≥ 2(2Nh-4) ≥ 2 2 (Nh-4) ≥ 2 2 (2 Nh-6) ≥ 2 3 Nh-6
65
Relation between #nodes and height of na AVL tree Nh ≥ 1 + Nh-1 + Nh-2 ≥ 2Nh-2 + 1 ≥ 2Nh-2 ≥ 2(2Nh-4) ≥ 2 2 (Nh-4) ≥ 2 2 (2 Nh-6) ≥ 2 3 Nh-6 ≥ 2 i Nh-2i
66
Relation between #nodes and height of na AVL tree Nh ≥ 1 + Nh-1 + Nh-2 ≥ 2Nh-2 + 1 ≥ 2Nh-2 ≥ 2(2Nh-4) ≥ 2 2 (Nh-4) ≥ 2 2 (2 Nh-6) ≥ 2 3 Nh-6 ≥ 2 i Nh-2i Cases: h=1 Nh = 1 h=2 Nh = 2
67
Relation between #nodes and height of na AVL tree Nh ≥ 1 + Nh-1 + Nh-2 ≥ 2Nh-2 + 1 ≥ 2Nh-2 ≥ 2(2Nh-4) ≥ 2 2 (Nh-4) ≥ 2 2 (2 Nh-6) ≥ 2 3 Nh-6 ≥ 2 i Nh-2i Cases: h=1 Nh = 1 h=2 Nh = 2 Solving the base case we get: n(h) > 2 h/2-1 Thus the height of an AVL tree is O(log n)
68
Relation between #nodes and height of na AVL tree Nh ≥ 1 + Nh-1 + Nh-2 ≥ 2Nh-2 + 1 ≥ 2Nh-2 ≥ 2(2Nh-4) ≥ 2 2 (Nh-4) ≥ 2 2 (2 Nh-6) ≥ 2 3 Nh-6 ≥ 2 i Nh-2i Cases: h=1 Nh = 1 h=2 Nh = 2 Solving the base case we get: n(h) > 2 h/2-1 Thus the height of an AVL tree is O(log n) We can also get to this limit by the Fibonacci number (Nh =Nh-1 + Nh-2) We can also get to this limit by the Fibonacci number (Nh =Nh-1 + Nh-2)
69
Height of AVL Tree Height of the tree is O(logN) n Where N is the number of elements contained in the tree This implies that tree search operations n Query(), Max(), Min() take O(logN) time.
70
Insertion in an AVL Tree Insertion is as in a binary search tree (always done by expanding an external node)
71
Insertion in an AVL Tree Insertion is as in a binary search tree (always done by expanding an external node) Example: 44 1778 325088 4862
72
Insertion in an AVL Tree Insertion is as in a binary search tree (always done by expanding an external node) Example: Insert node 54 44 1778 325088 4862
73
Insertion in an AVL Tree Insertion is as in a binary search tree (always done by expanding an external node) Example: 44 1778 325088 4862 54 Insert node 54 44 1778 325088 4862
74
Insertion in an AVL Tree Insertion is as in a binary search tree (always done by expanding an external node) Example: Insert node 54 4 44 1778 325088 4862 54 44 1778 325088 4862
75
Insertion in an AVL Tree Insertion is as in a binary search tree (always done by expanding an external node) Example: Insert node 54 4 3 44 1778 325088 4862 54 44 1778 325088 4862
76
Insertion in an AVL Tree Insertion is as in a binary search tree (always done by expanding an external node) Example: Insert node 54 4 3 1 44 1778 325088 4862 54 44 1778 325088 4862
77
Insertion in an AVL Tree Insertion is as in a binary search tree (always done by expanding an external node) Example: Insert node 54 4 3 1 44 1778 325088 4862 54 44 1778 325088 4862 Unbalanced!!
78
Insertion in an AVL Tree Insertion is as in a binary search tree (always done by expanding an external node) Example: 44 1778 325088 4862 54 Insert node 54 4 3 1 Unbalanced!! 44 1778 325088 4862
79
After insertion and deletion we will examine the tree structure and see if any node violates the AVL tree property n If the AVL property is violated at node x, it means the heights of left(x) and right(x) differ by exactly 2 If it does violate the property we can modify the tree structure using “rotations” to restore the AVL tree property How does the AVL tree work?
80
Rotations Two types of rotations n Single rotations – two nodes are “rotated” n Double rotations – three nodes are “rotated”
81
Localizing the problem Two principles: Imbalance will only occur on the path from the inserted/deleted node to the root (only these nodes have had their subtrees altered - local problem) Rebalancing should occur at the deepest unbalanced node (local solution too)
82
Single Rotation (Right): Case I Rotate x with left child y x and y satisfy the AVL property after the rotation
83
Single Rotation (Left): Case II Rotate x with right child y x and y satisfy the AVL property after the rotation
84
Single Rotation - Example Tree is an AVL tree by definition. h h+1
85
h h+2 Node 02 added Tree violates the AVL definition! Perform rotation. Single Rotation - Example
86
Tree has this form. h h h+1 A B C x y Single Rotation - Example
87
Example – After Rotation Tree has this form. A BC x y
88
Single Rotation Sometimes a single rotation fails to solve the problem k2 k1 X Y Z X Y Z k2 h+2 h h In such cases, we need to use a double-rotation
89
Double Rotations: Case IV
90
Tree is an AVL tree by definition. h h+1 Delete node 94 Double Rotations
91
AVL tree is violated. h h+2 Double Rotations
92
Tree has this form. B1B2 C A x y z Double Rotations
93
AB1B2C xy z Tree has this form After Double Rotations
94
Insertion We keep the height of each node x to check the AVL properrty Part 1. Perform normal BST insertion Part 2. Check AVL property and restore the property if necessary. – To check whether the AVL property persists we only need to check the nodes in the path from the new leaf to the root of the BST because the balance of the other nodes are not affected – Check if node x is balanced using the identity Height(x) = 1 + max { Height (left(x)), Height(right(x) } – We should update the heights of the visited nodes in this process
95
Insertion: Part 2 Detailed For each x in the path from the inserted leaf towards the root. If the heights of left(x) and right(x) height differ at most by 1 Do ‘nothing’ Else we know that one of the subtrees of x has height h and the other h+2 If the height of left(x) is h+2 then – If the height of left(left(x)) is h+1, we single rotate with left child (case 1) – Otherwise, the height of right(left(x)) is h+1 and we double rotate with left child (case 3) Otherwise, the height of right(x) is h+2 – If the height of right(right(x)) is h+1, then we rotate with right child (case 2) – Otherwise, the height of left(right(x)) is h+1 and we double rotate with right child (case 4) Break For
96
Insertion: Correctness Let x be the deepest node that does not satisfy the AVL property. Assume that case 2 occurs (the new element is inserted in tree C) x and y satisfy the property after the rotation. The ancestors of x satisfy the property because the height(x) before the insertion is h+2 and height(y) after the rotation is also h+2
97
Insertion: Correctness Let x be the deepest node that does not satisfy the AVL property. Assume that case 2 occurs (the new element is inserted in tree C) The nodes in the path between the new element and y also satisfy the AVL property due to the assumption that x is the deepest node for which the AVL property does not hold Nodes that are not in the path from the root to the new element are not affected
98
Insertion: Correctness Let x be the deepest node that does not satisfy the AVL property. Assume that case 4 occurs (the new element is inserted in tree B1) x, y and z satisfy the property after the rotation The ancestors of x are balanced after the rotation because the height of x is h+2 before the insertion and the height of z is h+2 after the rotation.
99
Insertion: Correctness Let x be the deepest node that does not satisfy the AVL property. Assume that case 4 occurs (the new element is inserted in tree B1) The remaining nodes in the path between the new element and x also satisfy the property due to the assumption that x is the deepest node that does not satisfy the AVL property The nodes that are not in the path between the new element and x are not affected.
100
Insertion: Complexity The time complexity to perform a rotation is O(1) since we just update a few pointers The time complexity to find a node that violates the AVL property depends on the height of the tree, which is log(N)
101
Deletion Perform normal BST deletion Perform verification similar to those employed for the insertion to restore the tree property
102
Summary AVL Trees Maintains a Balanced Tree Modifies the insertion and deletion routine n Performs single or double rotations to restore structure Guarantees that the height of the tree is O(logn) n The guarantee directly implies that functions find(), min(), and max() will be performed in O(logn)
103
Other Balanced trees Red Black Trees (Cormen Cap 13, Jayme cap 6) 2-3 Trees (Hopcroft)
104
104 Dictionary Problem: non uniform access probabilities n We want to keep a data structure to support a sequence of INSERT, QUERY, DELETE operations – Some elements are accessed much more often than others non-uniform access probabilities
105
Consider the following AVL Tree 44 1778 325088 4862 Dictionary Problem: non uniform access probabilities
106
Consider the following AVL Tree 44 1778 325088 4862 Dictionary Problem: non uniform access probabilities Suppose we want to search for the following sequence of elements: 48, 48, 48, 48, 62, 62, 62, 48, 62.
107
Consider the following AVL Tree 44 1778 325088 4862 Suppose we want to search for the following sequence of elements: 48, 48, 48, 48, 62, 62, 62, 48, 62. Dictionary Problem: non uniform access probabilities In this case, is this a good structure?
108
Consider the following AVL Tree 48 3262 44 50 78 Suppose we want to search for the following sequence of elements: 48, 48, 48, 48, 62, 62, 62, 48, 62. Dictionary Problem: non uniform access probabilities This structure is much better! 17 88
109
109 Dictionary Problem: non uniform access probabilities Application: Building Inverted indexes n Given a text T, we want to design an inverted index S for T, that is, a structure that maintains for every word x of T, the list of positions where x occurs. T ALO ALO MEU AMIGO …. ALO AMIGO MEU Positions 123456789... 12... 30 34 40 ALO 1,4,30 AMIGO 12,34 MEU 9, 40
110
110 Dictionary Problem: non uniform access probabilities Application: Building Inverted indexes n Given a text T, we want to design an inverted index S for T, that is, a structure that maintains for every word x of T, the list of positions where x occurs. T ALO ALO MEU AMIGO …. ALO AMIGO MEU Positions 123456789... 12... 30 34 40 ALO 1,4,30 AMIGO 12,34 MEU 9, 40 n We do not know the list of words beforehand; some words may occur much more frequently than others
111
111 Dictionary Problem: non uniform access probabilities Static Case: distribution access probability is known beforehand Lists Optimal Binary Search Trees Dynamic Case: distribution access probability is not known beforehand Self Adjusted Lists Self Adjusted Binary Search Trees Splay Trees
112
112 Dictionary Problem with non uniform access probabilities Problem n Given sequence K = k 1 < k 2 <··· < k n of n sorted keys, with a search probability p i for each key k i. – We assume that we always search an element that belongs to K. This assumption can be easily removed. n Want to design a data structure with minimum expected search cost. n Actual cost = # of items examined. – For key k i, number of elements accessed before finding k i
113
Optimal Binary Search Trees Cormen (cap 15.5, Edition 3) Estruturas de Dados e seus Algoritmos (Cap 4)
114
114 Dictionary Problem: non uniform access probabilities Approach 1: Linked lists n Put the elements with highest probabilities of being accessed at the beginning of the list n Keys (1,2,3,4,5); p=(0.1, 0.3, 0.2, 0.05, 0.15) n Best possible linked list 2 3 5 1 4 Expected cost of accessing an element = 1 x 0.3 + 2x0.2 + 3x0.15 + 4x0.1 + 5x0.05
115
115 Approach 2: Binary Search Tree n Given sequence K = k 1 < k 2 <··· < k n of n sorted keys, with a search probability p i for each key k i. n Want to build a binary search tree (BST) with minimum expected search cost. n Actual cost = # of items examined. n For key k i, cost = depth T (k i ) + 1, where depth T (k i ) = depth of k i in BST T. (root is at depth 0) Dictionary Problem with non uniform access probabilities
116
116 Expected Search Cost Sum of probabilities is 1. Identity (1)
117
117 Example Consider 5 keys with these search probabilities: p 1 = 0.25, p 2 = 0.2, p 3 = 0.05, p 4 = 0.2, p 5 = 0.3. k2k2 k1k1 k4k4 k3k3 k5k5 i depth T ( k i ) depth T ( k i ) · p i 1 1 0.25 2 0 0 3 2 0.1 4 1 0.2 5 2 0.6 1.15 Therefore, E[search cost] = 2.15.
118
118 Example p 1 = 0.25, p 2 = 0.2, p 3 = 0.05, p 4 = 0.2, p 5 = 0.3. i depth T (k i ) depth T (k i )·p i 1 1 0.25 2 0 0 3 3 0.15 4 2 0.4 5 1 0.3 1.10 Therefore, E[search cost] = 2.10. k2k2 k1k1 k5k5 k4k4 k3k3 This tree turns out to be optimal for this set of keys.
119
119 Example Observations: n Optimal BST may not have smallest height. n Optimal BST may not have highest-probability key at root. Build by exhaustive checking? n Construct each n-node BST. n For each, assign keys and compute expected search cost. n But there are (4 n /n 3/2 ) different BSTs with n nodes.
120
120 Optimal Substructure Any subtree of a BST contains keys in a contiguous range k i,..., k j for some 1 ≤ i ≤ j ≤ n. If T is an optimal BST and T contains subtree T ’ with keys k i,...,k j, then T must be an optimal BST for keys k i,..., k j. Proof: Otherwise, we can obtain a tree better T by replacing T’ with an optimal BST for keys k i,..., k j. T T
121
121 Optimal Substructure One of the keys in k i, …,k j, say k r, where i ≤ r ≤ j, must be the root of an optimal subtree for these keys. Left subtree of k r contains k i,...,k r 1. Right subtree of k r contains k r+1,...,k j. To find an optimal BST: n Examine all candidate roots k r, for i ≤ r ≤ j n Determine all optimal BSTs containing k i,...,k r 1 and containing k r+1,...,k j krkr kiki k r-1 k r+1 kjkj
122
Recursive Solution When the OPT subtree becomes a subtree of a node: n Depth of every node in OPT subtree goes up by 1. n Expected search cost increases by from Identity (1)
123
123 Recursive Solution When the OPT subtree becomes a subtree of a node: n Depth of every node in OPT subtree goes up by 1. n Expected search cost increases by from Identity (1) k1k1 k4k4 k3k3 k5k5 k2k2 k1k1 k4k4 k3k3 k5k5 k0k0
124
124 Recursive Solution e[i,j]: cost of the optimal BST for k i,..,k j : If k r is the root of an optimal BST for k i,..,k j : n e[i, j ] = p r + ( e[i, r 1] + w(i, r 1) ) + ( e[r+1, j] + w(r+1, j) )= e[i, r 1] + e[r+1, j] + w(i, j). But, we don’t know k r. Hence,
125
125 Computing an Optimal Solution For each subproblem (i,j), store: expected search cost in a table e [1.. n+1, 0.. n] n Will use only entries e[i, j ], where j ≥ i 1. root[i, j ] = root of subtree with keys k i,..,k j, for 1 ≤ i ≤ j ≤ n. w[1..n+1, 0..n] = sum of probabilities n w[i, i 1] = 0 for 1 ≤ i ≤ n. n w[i, j ] = w[i, j-1] + p j for 1 ≤ i ≤ j ≤ n.
126
126 Pseudo-code 1. OPTIMAL-BST(p, q, n) 2. for i ← 1 to n + 1 3. do e[i, i 1] ← 0 4. for len ← 1 to n 5. do for i ← 1 to n len + 1 6. do j ←i + len 1 7. e[i, j ]←∞ 8. for r ←i to j 9. do t ← e[i, r 1] + e[r + 1, j ] + w[i, j ] 10. if t < e[i, j ] 11. then e[i, j ] ← t 12. root[i, j ] ←r 13. return e and root 1. OPTIMAL-BST(p, q, n) 2. for i ← 1 to n + 1 3. do e[i, i 1] ← 0 4. for len ← 1 to n 5. do for i ← 1 to n len + 1 6. do j ←i + len 1 7. e[i, j ]←∞ 8. for r ←i to j 9. do t ← e[i, r 1] + e[r + 1, j ] + w[i, j ] 10. if t < e[i, j ] 11. then e[i, j ] ← t 12. root[i, j ] ←r 13. return e and root Time: O(n 3 ) Space: O(n 2 ) Consider all trees with l keys. Fix the first key. Fix the last key Determine the root of the optimal (sub)tree
127
127 Speeding up the Algorithm Knuth principle: Let k r be the root of an optimal BST for the set of keys k i k j and k i-1 < k i. Then, (i) there is an optimal BST for the set of keys k i-1,k i,..., k j with root smaller than or equal to k r (ii) there is an optimal BST for the set of keys k i,k i+1,..., k j+1 with root larger than or equal to k r
128
128 Knuth principle: Example p 1 = 0.25, p 2 = 0.2, p 3 = 0.05, p 4 = 0.2, p 5 = 0.3. Let k 0 be a key with probability p 0 then there is an optimal BST for the set (k 0,…, k 5 ) with root smaller than or equal to k 2. k2k2 k1k1 k5k5 k4k4 k3k3
129
129 Knuth principle: Example p 1 = 0.25, p 2 = 0.2, p 3 = 0.05, p 4 = 0.2, p 5 = 0.3. Let k 6 be a key with probability p 6 then there is an optimal BST for the set (k 1,…, k 6 ) with root larger than or equal to k 2 k2k2 k1k1 k5k5 k4k4 k3k3
130
130 Speeding up the Algorithm 1. OPTIMAL-BST-Revised(p, q, n) 2. for i ← 1 to n + 1 3. do e[i, i 1] ← 0 4. for len ← 1 to n 5. do for i ← 1 to n len + 1 6. do j ←i + len 1 7. e[i, j ]←∞ 8. for r ←root[i,j-1] to root[i+1,j] 9. do t ← e[i, r 1] + e[r + 1, j ] + w[i, j ] 10. if t < e[i, j ] 11. then e[i, j ] ← t 12. root[i, j ] ←r 13. return e and root 1. OPTIMAL-BST-Revised(p, q, n) 2. for i ← 1 to n + 1 3. do e[i, i 1] ← 0 4. for len ← 1 to n 5. do for i ← 1 to n len + 1 6. do j ←i + len 1 7. e[i, j ]←∞ 8. for r ←root[i,j-1] to root[i+1,j] 9. do t ← e[i, r 1] + e[r + 1, j ] + w[i, j ] 10. if t < e[i, j ] 11. then e[i, j ] ← t 12. root[i, j ] ←r 13. return e and root Time: O(n 2 ) Space: O(n 2 ) Consider all trees with l keys. O(n l ) Determine the root of the optimal (sub)tree Optimization.
131
Speeding up the Algorithm 131
132
Lower Bound on the expected search cost 132
133
Lower Bound on the expected search cost 133
134
Lower Bound on the expected search cost 134
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.