Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

SEARCHING

Searching

• Searching is the process of


selecting particular information
from a collection of data based on
specific criteria.
• A specific data component has to
be identified as the key.
• Finding an item within a sequence
using a search key to identify the
specific item.

Data Structures and Algorithms 2 FIT – HNUE 2021


Searching problem

• Input: collection of data A: A1, A2, …, An; and X


• Output: the position of the X in A or false if not found
• Example:
• A: 6, 9, 15, 3, 21, 7
• X = 15; return 3
• X = 20; return false

Data Structures and Algorithms 3 FIT – HNUE 2021


Algorithms

▪ The linear search


▪ The binary search
▪ Binary search tree

Data Structures and Algorithms 4 FIT – HNUE 2021


THE LINEAR SEARCH

Data Structures and Algorithms 5 FIT – HNUE 2020


Algorithm of the linear search

an unsorted array

• Idea:
• A count-controlled loop is used to traverse through the sequence during which each
element is compared against the target value.
• If the item is in the sequence, the loop is terminated and True is returned.
• Otherwise, a full traversal is performed and False is returned after the loop
terminates.
Data Structures and Algorithms 6 FIT – HNUE 2021
Implementation of the linear search

• Analyze the sequential search algorithm:


• the best case: O(1)
• the worst case: O(n)

Data Structures and Algorithms 7 FIT – HNUE 2021


Searching a Sorted Sequence

a sorted array

• It's possible to terminate the search early when the value is not in the
sequence instead of always having to perform a complete traversal.

Data Structures and Algorithms 8 FIT – HNUE 2021


Implementation of the linear search on a sorted sequence

• Analyze the sequential search algorithm:


• the best case: O(1)
• the worst case: O(n)
Data Structures and Algorithms 9 FIT – HNUE 2021
Finding the Smallest Value
• Problem: search for the smallest value
• Idea: keep track of the smallest value found for each iteration through
the loop

• Analyze the sequential search algorithm: O(n)


Data Structures and Algorithms 10 FIT – HNUE 2021
THE BINARY SEARCH

Data Structures and Algorithms 11 FIT – HNUE 2020


Search a word in dictionary

• Looking up a phone number or a word in the dictionary


• Start in middle of book
• If name you’re looking for comes before names on page, look in first half
• Otherwise, look in second half

Data Structures and Algorithms 12 FIT – HNUE 2021


The binary search

• Search 10??

Data Structures and Algorithms 13 FIT – HNUE 2021


The binary search algorithm

• The algorithm starts by examining the middle item of the sorted


sequence
• The middle item is :
• the target value: the process terminates successfully
• less than the target value : repeat the process on the upper half of the array.
• larger than the target value: repeat the process on the lower half of the array.
• We can eliminate half the values in the list when the target value is
not found at the middle position.

Data Structures and Algorithms 14 FIT – HNUE 2021


Implementation

• Recursive??
Data Structures and Algorithms 15 FIT – HNUE 2021
BINARY SEARCH TREE

Data Structures and Algorithms 16 FIT – HNUE 2020


Definition of a binary search tree (BST)

• A binary search tree (BST) is a binary


tree in which each node contains a
search key within its payload and the
tree is structured such that for each
interior node V:
• All keys less than the key in node V are
stored in the left subtree of V.
• All keys greater than the key in node V
are stored in the right subtree of V .

Data Structures and Algorithms 17 FIT – HNUE 2021


Implementation of the Map ADT using a binary search tree

Data Structures and Algorithms 18 FIT – HNUE 2021


Operations

• Searching
• Find min/max
• Insert
• Delete

Data Structures and Algorithms 19 FIT – HNUE 2021


Searching

• Idea:
• The target value is compared to the key in
the root node
• If the root contains the target value: the
process terminates successfully
• Otherwise:
• if the target is less than the root’s key: search
the left subtree
• if the target is greater than the root’s key:
search the right subtree

Data Structures and Algorithms 20 FIT – HNUE 2021


Searching

Data Structures and Algorithms 21 FIT – HNUE 2021


Searching for a target key in a binary search tree.

Data Structures and Algorithms 22 FIT – HNUE 2021


Min and Max Values

• Problem: Finding the minimum or


maximum key values.
• The node with the minimum key value?
• The node with the maximum key value?

Data Structures and Algorithms 23 FIT – HNUE 2021


Find the element with the minimum key value

Data Structures and Algorithms 24 FIT – HNUE 2021


Building the tree

• Key list [60, 25, 100, 35, 17, 80]

• Insert 30???

Data Structures and Algorithms 25 FIT – HNUE 2021


Insertions

• Inserting a new node into a binary search tree:


• searching for the node’s location
• linking the new node into the tree

Data Structures and Algorithms 26 FIT – HNUE 2021


Insert a key into a binary tree.

Data Structures and Algorithms 27 FIT – HNUE 2021


The recursive steps of the _bstInsert()

Data Structures and Algorithms 28 FIT – HNUE 2021


Deletions

• A deletion involves searching for the


node that contains the target key
and then unlinking the node to
remove it from the tree.
• There are three cases
• The node is a leaf.
• The node has a single child.
• The node has two children.

Data Structures and Algorithms 29 FIT – HNUE 2021


Removing a Leaf Node

After finding the node, it


has to be unlinked, which
can be done by setting the
left child field of its parent,
node 29, to None

a null reference will be assigned to the appropriate link field in the


parent

Data Structures and Algorithms 30 FIT – HNUE 2021


Removing an Interior Node with One Child

Incorrectly unlinking the interior node.

Data Structures and Algorithms 31 FIT – HNUE 2021


Removing an Interior Node with One Child

redirecting the link from the node’s parent to its child subtree

Data Structures and Algorithms 32 FIT – HNUE 2021


Removing an Interior Node with Two Children

• Attempting to remove an interior node with two children by replacing


the node with one of its children ???

Data Structures and Algorithms 33 FIT – HNUE 2021


Removing an Interior Node with Two Children

• Idea:
1) Find the logical successor, S,
of the node to be deleted, N.
2) Copy the key from node S to
node N.
3) Remove node S from the tree.

Data Structures and Algorithms 34 FIT – HNUE 2021


Delete a key from the binary search tree.

Data Structures and Algorithms 35 FIT – HNUE 2021


Efficiency of Binary Search Trees

Operation Worst case


Search(root, k) O(n)
Minimum(root) O(n)
Insert(root, k) O(n)
Delete(root, k) O(n)

Data Structures and Algorithms 36 FIT – HNUE 2021

You might also like