Professional Documents
Culture Documents
Searching Algorithms: Sequential Search Binary Search Binary Search Tree (BST)
Searching Algorithms: Sequential Search Binary Search Binary Search Tree (BST)
Sequential Search
Binary Search
Binary Search Tree (BST)
The Problem
3
Linear Search
The linear search approach compares the key element, key, with each
element in the array list[]. The method continues to do so until the key
matches an element in the list or the list is exhausted without a match
being found. If a match is made, the linear search returns the index of
the element in the array that matches the key. If no match is found,
the search returns -1.
4
Searching Algorithms (Cont’d)
5
Searching Algorithms (Cont’d)
6
Sequential Search(Linear Search)
void main() {
int A[8] = { 1, 5, 6, 7, 9, 10, 17, 30};
int x, n, index;
cout << "Enter search element: ";
cin >> x;
index = -1;
for(n=0; n<8 && A[n]<=x; n++)
if(A[n] == x)
index = n;
if(index==-1)
cout << "Not found!!" << endl;
else
cout << "Found at: " << index << endl;
}
7
Searching
8
Binary Search
9
Binary Search Pseudo Code
10
Binary Search Efficiency
11
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
low hi
12
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
low mid hi
13
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
low hi
14
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
low mid hi
15
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
low hi
16
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
low mid hi
17
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
low
hi
18
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
low
hi
mid
19
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
low
hi
mid
20
Binary Search
void main() {
int A[8] = {1, 5, 6, 7, 9, 10, 17, 30};
int x, lower, middle, upper;
cout << "Enter search element: ";
cin >> x;
low = 0;
hi = 7; // array size - 1
mid = (low + hi)/2;
while(A[mid]!=x && low<hi){
if(x<A[mid])
hi = mid - 1;
else
low = mid + 1;
mid = (low + hi)/2;
}
if(A[mid]!=x)
cout << "Not found!!" << endl;
else
cout << "Found at: " << mid << endl;
}
21
Binary Trees, Binary Search Trees
Binary Trees
Generic
binary tree
Worst-case
binary tree
23
Node Struct of Binary Tree
24
Binary Search Trees (BST)
25
Binary Search Trees
26
Binary Search Trees
27
Searching BST
28
29
Searching (Find)
Find X: return a pointer to the node that has key X, or NULL if there
is no such node
BinaryNode * BinarySearchTree::Find(const float &x,
BinaryNode *t) const
{
if (t == NULL)
return NULL;
else if (x < t->element)
return Find(x, t->left);
else if (t->element < x)
return Find(x, t->right);
else
return t; // match
}
30
findMin/ findMax
Goal: return the node containing the smallest (largest) key in the tree
Algorithm: Start at the root and go left (right) as long as there is a left
(right) child. The stopping point is the smallest (largest) element
BinaryNode * BinarySearchTree::FindMin(BinaryNode *t) const
{
if (t == NULL)
return NULL;
if (t->left == NULL)
return t;
return FindMin(t->left);
}
31
Insertion
32
Deletion
33
Deletion under Different Cases
34
Deletion Case 3
35
Q&A
36