Professional Documents
Culture Documents
Lec#7
Dr M Shamim Baig
Divide-and-Conquer
7 29 4 2 4 7 9
72 2 7
77 22
94 4 9
99 44
Divide-and-Conquer
Divide-and-Conquer
Divide-and conquer is a general algorithm design paradigm:
The base case for the recursion are subproblems of constant size Analysis can be done using recurrence equations
2004 Goodrich, Tamassia
Divide: divide the input data S in two or more disjoint subsets S1, S2 , Recur: solve the subproblems recursively Conquer: combine the solutions for S1, S2, , into a solution for S
Divide-and-Conquer
Divide-and-Conquer
Divide-and-Conquer
Binary Search
Divide-and-Conquer
Binary Search
Divide-and-Conquer
Divide-and-Conquer
Divide-and-Conquer
10
Divide-and-Conquer
11
Divide-and-Conquer
12
Divide-and-Conquer
13
Divide-and-Conquer
14
Divide-and-Conquer
15
Divide-and-Conquer
16
Divide-and-Conquer
17
An inorder traversal of a binary search trees visits the keys in increasing order
6 2 1 4 8 9
Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w)
18
Search
Algorithm TreeSearch(k, v) To search for a key k, if T.isExternal (v) we trace a downward path starting at the root return NO_SUCH_KEY if k < key(v) The next node visited depends on the return TreeSearch(k, T.left(v)) outcome of the else if k = key(v) comparison of k with return v the key of the current else { k > key(v) } node return TreeSearch(k, T.right(v)) If we reach a leaf, the 6 key is not found and we < return null 2 9 Example: find(4): >
Call TreeSearch(4,root)
4 =
19
Insertion
To perform operation insert(k, o), we search for key k (using TreeSearch) Assume k is not already in the tree, and let w be the leaf reached by the search We insert k at node w and expand w into an internal node Example: insert 5
1
<
2 1
6 9 4
> > w
6 8
2 4 8
w
5
20
Deletion
To perform operation remove(k), we search for key k Assume key k is in the tree, and let v be the node storing k If node v has a leaf child w, we remove v and w from the tree with operation removeExternal(w), which removes w and its parent Example: remove 4 <
2 1 6 9 4 v 8 5
>
w
6 2 1 5 8 9
21
Deletion (cont.)
We consider the case where the key k to be removed is stored at a node v whose children are both internal
1 3 2
v
8 6 9
we find the internal node w that follows v in an inorder traversal we copy key(w) into node v we remove node w and its left child z (which must be a leaf) by means of operation removeExternal(z)
w z
1 5 2
v
8 6 9
Example: remove 3
22
Performance
Consider a dictionary with n items implemented by means of a binary search tree of height h
the space used is O(n) methods find, insert and remove take O(h) time
The height h is O(n) in the worst case and O(log n) in the best case
23