IT3011 Week 11 Searching binary
IT3011 Week 11 Searching binary
LIỆU VÀ GIẢI
THUẬT
DATA STRUCTURES &
ALGORITHMS
SEARCHING
3
CONTENT
• Sequential search
• Binary search
• Binary Search Trees
4
Sequential Search
5
Binary Search
1 2 3 4 5 6 7 8 9 10
2 4 5 8 9 12 14 16 17 20 x = 17
6
Binary Search
• 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 if(L = R){
if(aL = x) return L;
• Base on divide and conquer:
return -1;
• Compare the input key with the key of the item in the }
middle of the sequence and decide to perform binary m = (L + R)/2;
search on the left subsequence or the right if(am = x) return m;
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
2 4 5 8 9 12 14 16 17 20 x = 17
7
Binary Search
• Exercise Given a sequence of distinct elements a1, a2, …, aN and a value b. Count the number of
pairs (ai, aj) having ai + aj = b (i < j)
8
Binary Search Trees - BST
• Binary Search Tree (or BST) is a data structure storing objects
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
9
Binary Search Trees - BST
• Binary Search Tree (or BST) is a data structure storing objects
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
• In-order traversal gets a sequence of keys sorted in an increasing
10 26
order: 3, 7, 8, 10, 15, 20, 23, 26, 30
7 15 23 30
3 8
10
Binary Search Trees
• Operations
• search(r, k): return the object having key k in the BST rooted
at r
20
• insert(r, k): insert a new object having key k to the BST rooted
at r
• remove(r, k): remove the node having key k from the BST
10 26
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
• 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
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
11
Binary Search Trees
• search(r, k): return the object having key k in the BST rooted search(r, k){
at r if r = null then return null;
• If k = r.key then return r if k = r.key then return r;
• If k > r.key then perform the search on the right sub-tree if k > r.key then
• If k < r.key then perform the search on the left sub-tree return search(r.rightChild, k);
if k < r.key then
• Time complexity O(h): h is the height of the BST rooted at r return search(r.leftChild, k);
}
12
Binary Search Trees
• insert(r, k): insert a new object having key k to the BST insert(r, k){
rooted at r, and return pointer to the resulting BST if r = null then return Node(k);
• If k = r.key then return r (do not insert) if k = r.key then return r;
• If k > r.key then perform the insertion on the right sub- if k > r.key then
tree return insert(r.rightChild, k);
• If k < r.key then perform the insertion on the left sub-tree if k < r.key then
return insert(r.leftChild, k);
• Time complexity O(h): h is the height of the BST rooted at r }
13
Binary Search Trees
• remove(r, k): remove the object having key k from the BST remove(r, k){
rooted at r, and return pointer to the resulting BST if r = null then return null;
• If k = r.key then perform the removal of the root if k = r.key then
• If k > r.key then perform the removal of key k from the return removeRoot(r);
right sub-tree (recursion) if k > r.key then
• 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
return remove(r.leftChild, k);
}
14
Binary Search Trees
• removeRoot(r): remove the the root of the BST removeRoot(r){
rooted at r, and return pointer to the resulting if r = NULL then return NULL;
BST tmp = r;
• If r does not have right child, then return the if r.rightChild = NULL then {
pointer to the left child r = r.leftChild; free(tmp); return r;
}
p = r.rightChild; pp = r;
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;
}
15
Binary Search Trees
• removeRoot(r): remove the the root of the BST removeRoot(r){
rooted at r, and return pointer to the resulting if r = NULL then return NULL;
BST tmp = r;
• If r has a right child, then find the node p if r.rightChild = NULL then {
having minimum key of the right sub-tree, r = r.leftChild; free(tmp); return r;
then copy the data (key) of p to the root r, }
then remove p (let the left child pointer of pp p = r.rightChild; pp = r;
(parent of p) point to the right child of p) 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;
}
16
Binary Search Trees
• findMin(r): return the pointer to the object (node) findMin(r){
having minimum value of key on the BST rooted if r = null then return null;
at r. lmin = findMin(r.leftChild);
• findMax(r): return the pointer to the object (node) if lmin != null then return lmin;
having maximum value of key on the BST rooted return r;
at r. }
findMax(r){
if r = null then return null;
rmax = findMax(r.rightChild);
if rmax != null then return rmax;
return r;
}
17
Binary Search Trees
• findSuccessor(x): return the pointer to the object
(node) having the smallest key but the key is greater
than the key of x
• Successor(20) = 23 20
• Successor(30) = null
• Successor(8) = 10
10 26
7 15 23 30
3 8
18
Binary Search Trees
• findSuccessor(x): return the pointer to the object findSuccessor(x){
(node) having the smallest key but the key is greater if x = null return null;
than the key of x if r.rightChild != null then
• If x has a right child, then the successor of x is return findMin(r.rightChild);
the object y with y.key is minimum among objects
of the right sub-tree of x: function p = x.parent;
findMin(x.rightChild) while p != null do {
if p.leftChild != null then return p;
p = p.parent;
}
return null; // not found successor
}
19
Binary Search Trees
• findSuccessor(x): return the pointer to the object findSuccessor(x){
(node) having the smallest key but the key is greater if x = null return null;
than the key of x if r.rightChild != null then
• If x has a right child, then the successor of x is return findMin(r.rightChild);
the object y with y.key is minimum among objects
of the right sub-tree of x: function p = x.parent;
findMin(x.rightChild) while p != null do {
• If x does not have a right child, then the if p.leftChild != null then return p;
successor of x is the nearest ancestor of x (use p = p.parent;
the parent pointer of nodes) having a left child }
(which is x or an ancestor of x). return null; // not found successor
}
20
Binary Search Trees
• findPredecessor(x): return the pointer to the object
having the highest key but the key is smaller than
the key of x
20
• Predecessor(20): 15
• Predecessor(3) = null
• Predecessor(23) = 20 10 26
7 15 23 30
3 8
21
Binary Search Trees
• 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;
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);
the object y with y.key is maximum among
objects of the left sub-tree of x: function p = x.parent;
findMax(x.leftChild) while p != null do {
if p.rightChild != null then return p;
p = p.parent;
}
return null; // not found predecessor
}
22
Binary Search Trees
• 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;
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);
the object y with y.key is maximum among
objects of the left sub-tree of x: function p = x.parent;
findMax(x.leftChild) while p != null do {
• If x does not have a left child, then the if p.rightChild != null then return p;
predecessor of x is the nearest ancestor of x p = p.parent;
(use the parent pointer of nodes) having a right }
child (which is x or an ancestor of x). return null; // not found predecessor
}
23
Binary Search Trees
• Time complexity of most of the basic operations on a BST is proportional to the height of the BST.
• Worst case: O(n) (in which n is the number of nodes of the BST)
20 20
10 26
7 30
3 33
1 43
24
THANK YOU !
25