0% found this document useful (0 votes)
3 views6 pages

Week11 SearchingBinarySearch

The document discusses various searching algorithms, including sequential search and binary search, along with their implementations and time complexities. It also covers Binary Search Trees (BST), detailing their structure, operations such as search, insert, remove, and traversal methods, along with their respective time complexities. The document emphasizes the efficiency of binary search and BST operations compared to sequential search.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Week11 SearchingBinarySearch

The document discusses various searching algorithms, including sequential search and binary search, along with their implementations and time complexities. It also covers Binary Search Trees (BST), detailing their structure, operations such as search, insert, remove, and traversal methods, along with their respective time complexities. The document emphasizes the efficiency of binary search and BST operations compared to sequential search.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DATA STRUCTURES &

ALGORITHMS
SEARCHING

CONTENT Sequential Search

• Sequential search • Given a sequence of n items a1, a2, . . ., an.


• Binary search • Given a value x, find an index i such that ai = x, or return -1, if no such index found
• Binary Search Trees
sequentialSearch(a1, a2, . . ., an, x){
for I = 1 to n do
if ai = x then return i;
return -1; // not found
}

• Time complexity O(n)


• Can also be applied for linked list

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

Binary Search Binary Search Trees - BST

• 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

Binary Search Trees Binary Search Trees


• search(r, k): return the object having key k in the BST rooted search(r, k){ • insert(r, k): insert a new object having key k to the BST rooted insert(r, k){
at r if r = null then return null; at r, and return pointer to the resulting BST if r = null then return Node(k);
• If k = r.key then return r if k = r.key then return r; • If k = r.key then return r (do not insert) 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 insertion on the right sub- if k > r.key then

• If k < r.key then perform the search on the left sub-tree return search(r.rightChild, k); tree return insert(r.rightChild, k);

• Time complexity O(h): h is the height of the BST rooted at r


if k < r.key then • If k < r.key then perform the insertion on the left sub-tree if k < r.key then
return search(r.leftChild, k); return insert(r.leftChild, k);
}
• Time complexity O(h): h is the height of the BST rooted at r }

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

Binary Search Trees Binary Search Trees


• removeRoot(r): remove the the root of the BST removeRoot(r){ • findMin(r): return the pointer to the object (node) findMin(r){
rooted at r, and return pointer to the resulting if r = NULL then return NULL; having minimum value of key on the BST rooted if r = null then return null;
BST tmp = r;
at r. lmin = findMin(r.leftChild);
• If r has a right child, then find the node p if r.rightChild = NULL then { • findMax(r): return the pointer to the object if lmin != null then return lmin;
having minimum key of the right sub-tree, r = r.leftChild; free(tmp); return r; (node) having maximum value of key on the BST return r;
then copy the data (key) of p to the root r, } rooted at 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 { findMax(r){
r.key = p.key; tmp = p; if r = null then return null;
r.rightChild = p.rightChild; rmax = findMax(r.rightChild);
free(tmp); return r; if rmax != null then return rmax;
} 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 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

Binary Search Trees Binary Search Trees


• findSuccessor(x): return the pointer to the object findSuccessor(x){ • findPredecessor(x): return the pointer to the object
(node) having the smallest key but the key is greater if x = null return null; having the highest key but the key is smaller than
than the key of x if r.rightChild != null then
the key of x
• If x has a right child, then the successor of x is the 20
return findMin(r.rightChild); • Predecessor(20): 15
object y with y.key is minimum among objects of
the right sub-tree of x: function • Predecessor(3) = null
p = x.parent;
findMin(x.rightChild) while p != null do { • Predecessor(23) = 20 10 26
• If x does not have a right child, then the successor if p.leftChild != null then return p;
of x is the nearest ancestor of x (use the parent p = p.parent;
pointer of nodes) having a left child (which is x or } 7 15 23 30
an ancestor of x). return null; // not found successor
}

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

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

THANK YOU !
10 26

7 30

3 33

1 43

23 24

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy