Week11 SearchingBinarySearch
Week11 SearchingBinarySearch
ALGORITHMS
SEARCHING
3 4
Binary Search Binary Search
• Sequence of items (indexed from L, L+1, . . ., R) is sorted in • Sequence of items (indexed from L, L+1, . . ., R) is sorted in binarySearch(a1, a2, . . ., an, L, R, x){
a non-increasing (non-decreasing) order of keys a non-increasing (non-decreasing) order of keys if(L = R){
if(aL = x) return L;
• Base on divide and conquer: • Base on divide and conquer:
return -1;
• Compare the input key with the key of the item in the • Compare the input key with the key of the item in the }
middle of the sequence and decide to perform binary middle of the sequence and decide to perform binary m = (L + R)/2;
search on the left subsequence or the right search on the left subsequence or the right if(am = x) return m;
subsequence of the object in the middle subsequence of the object in the middle
if(am < x)
• Running time: O(log(R-L)) return binarySearch(a1, a2, . . ., an,m+1,R,x);
return binarySearch(a1, a2, . . ., an,L,m,x);
}
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
2 4 5 8 9 12 14 16 17 20 x = 17 2 4 5 8 9 12 14 16 17 20 x = 17
5 6
• Exercise Given a sequence of distinct elements a1, a2, …, aN and a value b. Count the number of • Binary Search Tree (or BST) is a data structure storing objects
pairs (ai, aj) having ai + aj = b (i < j) under a binary tree:
• Key of each node is greater than the keys of nodes of the left
sub-tree and smaller than the keys of nodes of the right sub- 20
tree
struct Node{
key; // key of the node 10 26
leftChild; // pointer to the left-child
rightChild; // pointer to the right-child
parent; // pointer to the parent of the current node
// parent of the root is null (by convention) 7 15 23 30
}
3 8
7 8
Binary Search Trees - BST Binary Search Trees
• Binary Search Tree (or BST) is a data structure storing objects • Operations
under a binary tree: • search(r, k): return the object having key k in the BST rooted at
• Key of each node is greater than the keys of nodes of the left r
sub-tree and smaller than the keys of nodes of the right sub- 20 20
• insert(r, k): insert a new object having key k to the BST rooted
tree at r
• In-order traversal gets a sequence of keys sorted in an increasing • remove(r, k): remove the node having key k from the BST
10 26 10 26
order: 3, 7, 8, 10, 15, 20, 23, 26, 30 rooted at r
• findMin(r): return the pointer to the object having minimum
value of key in the BST rooted at r
7 15 23 30 7 15 23 30
• findMax(r): return the pointer to the object having maximum
value of key in the BST rooted at r
• findSuccessor(x): return the pointer to the object having the
3 8 smallest key but the key is greater than the key of x 3 8
• findPredecessor(x): return the pointer to the object having the
highest key but the key is smaller than the key of x
9 10
• If k < r.key then perform the search on the left sub-tree return search(r.rightChild, k); tree return insert(r.rightChild, k);
11 12
Binary Search Trees Binary Search Trees
• remove(r, k): remove the object having key k from the BST remove(r, k){ • removeRoot(r): remove the the root of the BST removeRoot(r){
rooted at r, and return pointer to the resulting BST if r = null then return null; rooted at r, and return pointer to the resulting if r = NULL then return NULL;
• If k = r.key then perform the removal of the root if k = r.key then
BST tmp = r;
• If k > r.key then perform the removal of key k from the return removeRoot(r); • If r does not have right child, then return the if r.rightChild = NULL then {
right sub-tree (recursion) if k > r.key then pointer to the left child r = r.leftChild; free(tmp); return r;
• If k < r.key then perform the removal of key k from the return remove(r.rightChild, k); }
left sub-tree (recursion) if k < r.key then p = r.rightChild; pp = r;
return remove(r.leftChild, k); if p.leftChild = NULL then {
} r.key = p.key; tmp = p;
r.rightChild = p.rightChild;
free(tmp); return r;
}
while p.leftChild != NULL do {
pp = p; p = p.leftChild;
}
pp.leftChild = p.rightChild; r.key = p.key; free(p);
return r;
}
13 14
15 16
Binary Search Trees Binary Search Trees
• findSuccessor(x): return the pointer to the object • findSuccessor(x): return the pointer to the object findSuccessor(x){
(node) having the smallest key but the key is greater (node) having the smallest key but the key is greater if x = null return null;
than the key of x than the key of x if r.rightChild != null then
• Successor(20) = 23 20 • If x has a right child, then the successor of x is the return findMin(r.rightChild);
• Successor(30) = null object y with y.key is minimum among objects of
the right sub-tree of x: function
• Successor(8) = 10 p = x.parent;
10 26 findMin(x.rightChild) while p != null do {
if p.leftChild != null then return p;
p = p.parent;
7 15 23 30 }
return null; // not found successor
}
3 8
17 18
3 8
19 20
Binary Search Trees Binary Search Trees
• findPredecessor(x): return the pointer to the object findPredecessor(x){ • findPredecessor(x): return the pointer to the object findPredecessor(x){
having the highest key but the key is smaller than if x = null return null; having the highest key but the key is smaller than if x = null return null;
the key of x if r.leftChild != null then
the key of x if r.leftChild != null then
• If x has a left child, then the predecessor of x is return findMax(r.leftChild); • If x has a left child, then the predecessor of x is return findMax(r.leftChild);
the object y with y.key is maximum among the object y with y.key is maximum among
objects of the left sub-tree of x: function p = x.parent; objects of the left sub-tree of x: function p = x.parent;
findMax(x.leftChild) while p != null do {
findMax(x.leftChild) while p != null do {
if p.rightChild != null then return p; • If x does not have a left child, then the if p.rightChild != null then return p;
p = p.parent; predecessor of x is the nearest ancestor of x p = p.parent;
} (use the parent pointer of nodes) having a right }
return null; // not found predecessor child (which is x or an ancestor of x). return null; // not found predecessor
} }
21 22
20 20
THANK YOU !
10 26
7 30
3 33
1 43
23 24