Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 167

Searching via Traversals

Searching a Binary Search Tree (BST)


Binary Search on a Sorted Array
Data Structure Conversion
and Helper Modules
LB
Plan of Attack
• Simple searching just involves traversing a data
structure until the data element is found.
• The only twist is that if the collection is ordered
(like a linked lists) you can stop if you don’t find
an element and you pass the point where it
should be located.
• So we’re going to skip numerous slides which
essentially review material we’ve already
covered.
• So we’re going to focus on Binary Search and
Data Structure Conversion
• But first the twist...
Page 122
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search

head 7 22 8 4 34
LB
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
okToExit isoftype Boolean
okToExit <- FALSE
loop
exitif(okToExit)
if(cur = NIL OR cur^.data > target) then
okToExit <- TRUE
print(“Target data not found”)
elseif(cur^.data = target) then
okToExit = TRUE
print(“Found target”)
// Could transfer data to param here
else
cur <- cur^.next
endif
endloop

endprocedure // Search
LB

Same logic can be applied to Trees

Skipping stuff starting on Page 120!


Searching via Traversals
An Example

• Imagine we have a database of students


• I want to know if Bob Smith is in my
class.

• I want to search my database


and have the module print out
“IN THE CLASS” or “NOT IN THE
CLASS.”
Another Situation

• I have the same student database.


• I need to view Alice Jones’ grades
and update them.

• Again, I need to search the database.


• This time, I need Alice’s information
returned to me so I can view and
modify them (i.e. need access).
The Scenario
• We have some collection
(stored in an array, tree, or list)
– May be sorted or not
– May be empty or not

• We want the determine if a


particular element is in the
collection or not.

• We need to search for the element.


Searching
• Given the collection and an
element to find…
• Determine whether the “target”
element was found in the collection
– Print a message
– Return a value
(an index or pointer, etc.)
• Don’t modify the collection in the
search!
Key Fields
• Often, our collections will hold
complex structures (i.e. have many
items of information in each).
• It is common to organize our
collection using one “part” (or field)
– Name
– Student Number
• This is called the “key field”
• Then we can search on the key field.
A Simple
Search
• A search traverses the collection
until
– The desired element is found
– Or the collection is exhausted
• If the collection is ordered, I might
not have to look at all elements
– I can stop looking when I know
the element cannot be in the
collection.
Searching in an Unordered Collection

• Let’s determine if the value 12 is in the collection:

Head
35 42 12 5 \\

12 Found!
Searching in an Unordered Collection

• Let’s determine if the value 13 is in the collection:

Head
35 42 12 5 \\

13 Not Found!
Searching in an Ordered Collection

• Let’s determine if the value 13 is in the collection:

Head
5 12 35 42 \\

13 Not Found!
Search Examples
Arrays

Linked Lists

Binary Trees
Search Example on a List
An Iterative Search

Let’s build a module to search a list


of numbers using iteration.

We’ll print whether the number was


found or not.

Steps:
– Traverse until we find a match
or reach the end.
– Print the results.
An Iterative Un-Ordered Search
procedure Search ( ??? )

loop
???
exitif( ??? )
???
endloop

if( ??? ) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
An Iterative Un-Ordered Search
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )

loop
???
exitif( ??? )
???
endloop

if( ??? ) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
An Iterative Un-Ordered Search
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )

loop
exitif( cur = NIL )
cur <- cur^.next
endloop

if( ??? ) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
An Iterative Un-Ordered Search
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )

loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if( ??? ) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
An Iterative Un-Ordered Search
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )

loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if( cur = NIL ) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
An Iterative Un-Ordered Search
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )

loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if( cur = NIL ) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
What’s Different for An Iterative
Ordered Search?
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )

loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if( cur = NIL ) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
An Iterative Ordered Search
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )

loop
exitif((cur = NIL) OR (cur^.data >= target))
cur <- cur^.next
endloop

if((cur = NIL) OR (cur^.data > target)) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
Tracing the Search
Now let’s call the un-ordered version of the module:

algorithm Example
. . .
Search(head, 4)
. . .
endalgorithm // Example

head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
Target
exitif((cur = NIL) OR data found = target))
(cur^.data
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop

if(cur = NIL) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 4
head 7 22 8 4 34
Tracing the Ordered Search
Now let’s call the ordered version of the module:

algorithm Example
. . .
Search(head, 9)
. . .
endalgorithm // Example

head 4 7 8 22 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data >= target))
cur <- cur^.next
endloop

if((cur = NIL) OR (cur^.data > target)) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 9
head 4 7 8 22 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data >= target))
cur <- cur^.next
endloop

if((cur = NIL) OR (cur^.data > target)) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 9
head 4 7 8 22 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data >= target))
cur <- cur^.next
endloop

if((cur = NIL) OR (cur^.data > target)) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 9
head 4 7 8 22 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data >= target))
cur <- cur^.next
endloop

if((cur = NIL) OR (cur^.data > target)) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 9
head 4 7 8 22 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data >= target))
cur <- cur^.next
endloop

if((cur = NIL) OR (cur^.data > target)) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 9
head 4 7 8 22 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data >= target))
cur <- cur^.next
endloop

if((cur = NIL) OR (cur^.data > target)) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 9
head 4 7 8 22 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur =Target data
NIL) OR not found>= target))
(cur^.data
cur <- cur^.next
endloop

if((cur = NIL) OR (cur^.data > target)) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 9
head 4 7 8 22 34
procedure Search ( cur isoftype in ptr toa Node,
target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data >= target))
cur <- cur^.next
endloop

if((cur = NIL) OR (cur^.data > target)) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
cur
target = 9
head 4 7 8 22 34
Searching an Array

12 42 35 101 77 5

• Same principles apply:


– Examine cells until we find a match
or exhaust the possibilities
– Then indicate whether the item was
in the collection (print or return
information)
Un-Ordered Iterative Array Search
procedure Search(my_array isoftype in NumArrayType,
target isoftype in Num)
i isoftype Num
i <- 1
loop
exitif((i > MAX) OR (my_array[i] = target))
i <- i + 1
endloop

if(i > MAX) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search
Calling the Module

algorithm Example2
MAX is 6
. . .
Search(MyNumArray, 13)
. . .
endalgorithm // Example2
procedure Search(my_array isoftype in NumArrayType,
target isoftype in Num)
i isoftype Num
i <- 1
loop
exitif((i > MAX) OR (my_array[i] = target))
i <- i + 1
endloop

if(i > MAX) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search

my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
procedure Search(my_array isoftype in NumArrayType,
target isoftype in Num)
i isoftype Num
i <- 1
loop
exitif((i > MAX) OR (my_array[i] = target))
i <- i + 1
endloop

if(i > MAX) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search

my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
procedure Search(my_array isoftype in NumArrayType,
target isoftype in Num)
i isoftype Num
i <- 1
loop
exitif((i > MAX) OR (my_array[i] = target))
i <- i + 1
endloop

if(i > MAX) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search

my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
procedure Search(my_array isoftype in NumArrayType,
target isoftype in Num)
i isoftype Num
i <- 1
loop
exitif((i > MAX) OR (my_array[i] = target))
i <- i + 1
endloop

if(i > MAX) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search

my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
procedure Search(my_array isoftype in NumArrayType,
target isoftype in Num)
i isoftype Num
i <- 1
loop
exitif((i > MAX) OR (my_array[i] = target))
i <- i + 1
endloop

if(i > MAX) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search

my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
procedure Search(my_array isoftype in NumArrayType,
target isoftype in Num)
i isoftype Num
i <- 1
loop
exitif((i > MAX) OR (my_array[i] = target))
i <- i + 1
endloop

if(i > MAX) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search

my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
procedure Search(my_array isoftype in NumArrayType,
target isoftype in Num)
i isoftype Num
i <- 1
loop
exitif((i > MAX) OR (my_array[i] = target))
i <- i + 1
endloop

if(i > MAX) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search

my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
procedure Search(my_array isoftype in NumArrayType,
target isoftype in Num)
i isoftype Num
i <- 1
loop
Target data found
exitif((i > MAX) OR (my_array[i] = target))
i <- i + 1
endloop

if(i > MAX) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search

my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
procedure Search(my_array isoftype in NumArrayType,
target isoftype in Num)
i isoftype Num
i <- 1
loop
exitif((i > MAX) OR (my_array[i] = target))
i <- i + 1
endloop

if(i > MAX) then


print(“Target data not found”)
else
print(“Target data found”)
endif
endprocedure // Search

my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Page 130

Binary Tree Searches

14

9 42

3 28 56
Searching With Binary Trees

Have to visit every node in the binary tree.

3 types of traversals:

Preorder Inorder Postorder


Check Node Go left Go left
Go left Check Node Go right
Go right Go right Check Node
Pre-Order Search Traversal Algorithm

• As soon as we get to a node,


check to see if we have a match
• Otherwise, look for the element in
the left sub-tree
• Otherwise, look for the element in
the right sub-tree
14

Left ??? Right ???


Search on a Binary Tree
function BTSearch returnsa Boolean
(cur iot in ptr toa Node, toFind iot in Num)
if (cur = NIL) then
BTSearch returns false
elseif (cur^.data = toFind) then
BTSearch returns true
else
BTSearch returns BTSearch(cur^.left, toFind)
OR BTSearch(cur^.right, toFind)
endif
endfunction // BTSearch
LB

cur Find 14

94

3 36

22 14 67 9
LB

Find 14

cur 94

3 36

22 14 67 9
LB

Find 14

94

cur 3 36

22 14 67 9
LB

Find 14

94

3 36

cur
22 14 67 9
LB

Find 14

94

3 36

cur
22 14 67 9
LB

Find 14...Found!!!

94

3 cur 36

22 14 67 9
LB

Search on a Binary Tree


function BTSearch returnsa Boolean
(cur iot in ptr toa Node, toFind iot in Num)
if (cur = NIL) then
BTSearch returns false
elseif (cur^.data = toFind) then
BTSearch returns true
else
BTSearch returns BTSearch(cur^.left, toFind)
OR BTSearch(cur^.right, toFind)
endif
endfunction // BTSearch
Once we’ve found the 14 do
we search the right subtree?
Summary
• Searches involve visiting elements in a
collection until
– A match is found
– Or until all possible locations are exhausted

• With sorted collections, we may be able


to “stop early”

• Once we determine if the element is in the


collection, then we do some work:
– Print the results
– Return some information (pointer, index)
Questions?
Searching a Binary Search Tree (BST)
The Scenario
• We’ve got a Binary Search Tree and we want to
determine if an element is in the collection.

14

Less
Greater
Than
Than
14
14
Cutting the Work in Half
• In searching for a match, we can ignore half of
the tree at each comparison.

14 Looking for 42…

It must be to the
right!

Less
Greater
Than
Than
14
14
The Binary Search Algorithm

if at NIL // not found


DO NOT FOUND WORK
elseif ( match ) then // found
DO FOUND WORK
elseif ( value to match < current value )
recurse left // must be to left
else
recurse right // must be to right
The Binary Search for a BST
procedure Search(cur iot in Ptr toa Node,
target isoftype in Num)
if(cur = NIL) then
print(“Not Found”)
elseif(cur^.data = target)
print(“Target Found”)
elseif(cur^.data > target)
Search(cur^.left, target)
else
Search(cur^.right, target)
endif
endprocedure // Search
. head
.
Search(head, 35)
Search(head, 87) 42
.
.

23 47

35
. head
.
Search(head, 35)
Search(head, 87) 42
.
.

23 47

35
.Procedure Search( head
. cur iot in Ptr toa Node,
Search(head, 35)num)
target iot in cur
Search(head, 87)then
if(cur = NIL) 42
. print(“Not Found”)
. elseif(cur^.data = target)
print(“Target Found”)
elseif(cur^.data > target)
Search(cur^.left, target) 23 47
else
Search(cur^.right, target)
endif
endprocedure // Search 35

target = 35
.Procedure Search( head
. cur iot in Ptr toa Node,
Search(head, 35)num)
target iot in cur
Search(head, 87)then
if(cur = NIL) 42
. print(“Not Found”)
. elseif(cur^.data = target)
print(“Target Found”)
elseif(cur^.data > target)
Search(cur^.left, target) 23 47
else
Search(cur^.right, target)
endif
endprocedure // Search 35

target = 35
.Procedure Search( head
. cur iot in Ptr toa Node,
Search(head, 35)num)
target iot in cur
Search(head, 87)then
if(cur = NIL) 42
. print(“Not Found”)
. elseif(cur^.data = target)
print(“Target Found”)
elseif(cur^.data > target)
Search(cur^.left, target) 23 47
else
Search(cur^.right, target)
endif
endprocedure // Search 35

target = 35
.Procedure Search( head
. cur iot in Ptr toa Node,
Search(head, 35)num)
target iot in cur
Search(head, 87)then
if(cur = NIL) 42
. print(“Not Found”)
. elseif(cur^.data = target)
print(“Target Found”)
elseif(cur^.data > target)
Search(cur^.left, target) 23 47
else
Search(cur^.right, target)
endif
endprocedure // Search 35

target = 35
.Procedure Search( head
. Procedure Search(
cur iot in Ptr toa Node,
cur iot in 35)
Search(head, Ptr toa Node,
target iot in num)
target iot
Search(head, in num)
87)then
if(cur = NIL) 42
. if(cur = NIL) then
print(“Not Found”)
print(“Not Found”)
. elseif(cur^.data = target) cur
elseif(cur^.data = target)
print(“Target Found”)
print(“Target Found”)
elseif(cur^.data > target)
elseif(cur^.data > target) 23 47
Search(cur^.left, target)
Search(cur^.left, target)
else
else
Search(cur^.right, target)
Search(cur^.right, target)
endif
endif
endprocedure // Search 35
endprocedure // Search

target = 35
.Procedure Search( head
. Procedure Search(
cur iot in Ptr toa Node,
cur iot in 35)
Search(head, Ptr toa Node,
target iot in num)
target iot
Search(head, in num)
87)then
if(cur = NIL) 42
. if(cur = NIL) then
print(“Not Found”)
print(“Not Found”)
. elseif(cur^.data = target) cur
elseif(cur^.data = target)
print(“Target Found”)
print(“Target Found”)
elseif(cur^.data > target)
elseif(cur^.data > target) 23 47
Search(cur^.left, target)
Search(cur^.left, target)
else
else
Search(cur^.right, target)
Search(cur^.right, target)
endif
endif
endprocedure // Search 35
endprocedure // Search

target = 35
.Procedure Search( head
. Procedure Search(
cur iot in Ptr toa Node,
cur iot in 35)
Search(head, Ptr toa Node,
target iot in num)
target iot
Search(head, in num)
87)then
if(cur = NIL) 42
. if(cur = NIL) then
print(“Not Found”)
print(“Not Found”)
. elseif(cur^.data = target) cur
elseif(cur^.data = target)
print(“Target Found”)
print(“Target Found”)
elseif(cur^.data > target)
elseif(cur^.data > target) 23 47
Search(cur^.left, target)
Search(cur^.left, target)
else
else
Search(cur^.right, target)
Search(cur^.right, target)
endif
endif
endprocedure // Search 35
endprocedure // Search

target = 35
.Procedure Search( head
. Procedure Search(
cur iot in Ptr toa Node,
cur iot in 35)
Search(head, Ptr toa Node,
target iot in num)
target iot
Search(head, in num)
87)then
if(cur = NIL) 42
. if(cur = NIL) then
print(“Not Found”)
print(“Not Found”)
. elseif(cur^.data = target) cur
elseif(cur^.data = target)
print(“Target Found”)
print(“Target Found”)
elseif(cur^.data > target)
elseif(cur^.data > target) 23 47
Search(cur^.left, target)
Search(cur^.left, target)
else
else
Search(cur^.right, target)
Search(cur^.right, target)
endif
endif
endprocedure // Search 35
endprocedure // Search

target = 35
.Procedure Search( head
. Procedure
cur
Procedure
Search(
iot in Search(
Ptr toa Node,
cur iot
Search(head, in Ptr toa Node,
35)
target
cur iot
iotin inPtr
num)
toa Node,
target
Search(head,iot in
87) num)
if(cur
target
if(cur
= iot
=
NIL)
NIL)
inthen
num)
then 42
. print(“Not
if(cur = NIL) Found”)
then
print(“Not
. elseif(cur^.data Found”)
print(“Not Found”) = target)
elseif(cur^.data = target)
print(“Target
elseif(cur^.data Found”)
= target)
print(“Target Found”)
elseif(cur^.data
print(“Target Found”)
> target)
elseif(cur^.data > target) 23 47
Search(cur^.left,
elseif(cur^.data > target)
target)
Search(cur^.left, target)
else
Search(cur^.left, target)
else
Search(cur^.right,
else target)
Search(cur^.right, target)
endif
Search(cur^.right, target)
endif
endprocedure
endif // Search 35
endprocedure // Search
endprocedure // Search
cur

target = 35
.Procedure Search( head
. Procedure
cur
Procedure
Search(
iot in Search(
Ptr toa Node,
cur iot
Search(head, in Ptr toa Node,
35)
target
cur iot
iotin inPtr
num)
toa Node,
target
Search(head,iot in
87) num)
if(cur
target
if(cur
= iot
=
NIL)
NIL)
inthen
num)
then 42
. print(“Not
if(cur = NIL) Found”)
then
print(“Not
. elseif(cur^.data Found”)
print(“Not Found”) = target)
elseif(cur^.data = target)
print(“Target
elseif(cur^.data Found”)
= target)
print(“Target Found”)
elseif(cur^.data
print(“Target Found”)
> target)
elseif(cur^.data > target) 23 47
Search(cur^.left,
elseif(cur^.data > target)
target)
Search(cur^.left, target)
else
Search(cur^.left, target)
else
Search(cur^.right,
else target)
Search(cur^.right, target)
endif
Search(cur^.right, target)
endif
endprocedure
endif // Search 35
endprocedure // Search
endprocedure // Search
cur

target = 35
.Procedure Search( head
. Procedure
cur
Procedure
Search(
iot in Search(
Ptr toa Node,
cur iot
Search(head, in Ptr toa Node,
35)
target
cur iot
iotin inPtr
num)
toa Node,
target
Search(head,iot in
87) num)
if(cur
target
if(cur
= iot
=
NIL)
NIL)
inthen
num)
then 42
. print(“Not
if(cur = NIL) Found”)
then
print(“Not
. elseif(cur^.data Found”)
print(“Not Found”) = target)
elseif(cur^.data = target)
print(“Target
elseif(cur^.data Found”)
= target)
print(“Target Found”)
elseif(cur^.data
print(“Target Found”)
> target)
elseif(cur^.data > target) 23 47
Search(cur^.left,
elseif(cur^.data > target)
target)
Search(cur^.left, target)
else
Search(cur^.left, target)
else
Search(cur^.right,
else target)
Search(cur^.right, target)
endif
Search(cur^.right, target)
endif
endprocedure
endif // Search 35
endprocedure // Search
endprocedure // Search
cur

Target Found
.Procedure Search( head
. Procedure
cur
Procedure
Search(
iot in Search(
Ptr toa Node,
cur iot
Search(head, in Ptr toa Node,
35)
target
cur iot
iotin inPtr
num)
toa Node,
target
Search(head,iot in
87) num)
if(cur
target
if(cur
= iot
=
NIL)
NIL)
inthen
num)
then 42
. print(“Not
if(cur = NIL) Found”)
then
print(“Not
. elseif(cur^.data Found”)
print(“Not Found”) = target)
elseif(cur^.data = target)
print(“Target
elseif(cur^.data Found”)
= target)
print(“Target Found”)
elseif(cur^.data
print(“Target Found”)
> target)
elseif(cur^.data > target) 23 47
Search(cur^.left,
elseif(cur^.data > target)
target)
Search(cur^.left, target)
else
Search(cur^.left, target)
else
Search(cur^.right,
else target)
Search(cur^.right, target)
endif
Search(cur^.right, target)
endif
endprocedure
endif // Search 35
endprocedure // Search
endprocedure // Search
cur
.Procedure Search( head
. Procedure Search(
cur iot in Ptr toa Node,
cur iot in 35)
Search(head, Ptr toa Node,
target iot in num)
target iot
Search(head, in num)
87)then
if(cur = NIL) 42
. if(cur = NIL) then
print(“Not Found”)
print(“Not Found”)
. elseif(cur^.data = target) cur
elseif(cur^.data = target)
print(“Target Found”)
print(“Target Found”)
elseif(cur^.data > target)
elseif(cur^.data > target) 23 47
Search(cur^.left, target)
Search(cur^.left, target)
else
else
Search(cur^.right, target)
Search(cur^.right, target)
endif
endif
endprocedure // Search 35
endprocedure // Search
.Procedure Search( head
. cur iot in Ptr toa Node,
Search(head, 35)num)
target iot in cur
Search(head, 87)then
if(cur = NIL) 42
. print(“Not Found”)
. elseif(cur^.data = target)
print(“Target Found”)
elseif(cur^.data > target)
Search(cur^.left, target) 23 47
else
Search(cur^.right, target)
endif
endprocedure // Search 35
. head
.
Search(head, 35)
Search(head, 87) 42
.
.

23 47

35
.Procedure Search( head
. cur iot in Ptr toa Node,
Search(head, 35)num)
target iot in cur
Search(head, 87)then
if(cur = NIL) 42
. print(“Not Found”)
. elseif(cur^.data = target)
print(“Target Found”)
elseif(cur^.data > target)
Search(cur^.left, target) 23 47
else
Search(cur^.right, target)
endif
endprocedure // Search 35

target = 87
.Procedure Search( head
. cur iot in Ptr toa Node,
Search(head, 35)num)
target iot in cur
Search(head, 87)then
if(cur = NIL) 42
. print(“Not Found”)
. elseif(cur^.data = target)
print(“Target Found”)
elseif(cur^.data > target)
Search(cur^.left, target) 23 47
else
Search(cur^.right, target)
endif
endprocedure // Search 35

target = 87
.Procedure Search( head
. cur iot in Ptr toa Node,
Search(head, 35)num)
target iot in cur
Search(head, 87)then
if(cur = NIL) 42
. print(“Not Found”)
. elseif(cur^.data = target)
print(“Target Found”)
elseif(cur^.data > target)
Search(cur^.left, target) 23 47
else
Search(cur^.right, target)
endif
endprocedure // Search 35

target = 87
.Procedure Search( head
. cur iot in Ptr toa Node,
Search(head, 35)num)
target iot in cur
Search(head, 87)then
if(cur = NIL) 42
. print(“Not Found”)
. elseif(cur^.data = target)
print(“Target Found”)
elseif(cur^.data > target)
Search(cur^.left, target) 23 47
else
Search(cur^.right, target)
endif
endprocedure // Search 35

target = 87
.Procedure Search( head
. Procedure Search(
cur iot in Ptr toa Node,
cur iot in 35)
Search(head, Ptr toa Node,
target iot in num)
target iot
Search(head, in num)
87)then
if(cur = NIL) 42
. if(cur = NIL) then
print(“Not Found”)
print(“Not Found”)
. elseif(cur^.data = target) cur
elseif(cur^.data = target)
print(“Target Found”)
print(“Target Found”)
elseif(cur^.data > target)
elseif(cur^.data > target) 23 47
Search(cur^.left, target)
Search(cur^.left, target)
else
else
Search(cur^.right, target)
Search(cur^.right, target)
endif
endif
endprocedure // Search 35
endprocedure // Search

target = 87
.Procedure Search( head
. Procedure Search(
cur iot in Ptr toa Node,
cur iot in 35)
Search(head, Ptr toa Node,
target iot in num)
target iot
Search(head, in num)
87)then
if(cur = NIL) 42
. if(cur = NIL) then
print(“Not Found”)
print(“Not Found”)
. elseif(cur^.data = target) cur
elseif(cur^.data = target)
print(“Target Found”)
print(“Target Found”)
elseif(cur^.data > target)
elseif(cur^.data > target) 23 47
Search(cur^.left, target)
Search(cur^.left, target)
else
else
Search(cur^.right, target)
Search(cur^.right, target)
endif
endif
endprocedure // Search 35
endprocedure // Search

target = 87
.Procedure Search( head
. Procedure Search(
cur iot in Ptr toa Node,
cur iot in 35)
Search(head, Ptr toa Node,
target iot in num)
target iot
Search(head, in num)
87)then
if(cur = NIL) 42
. if(cur = NIL) then
print(“Not Found”)
print(“Not Found”)
. elseif(cur^.data = target) cur
elseif(cur^.data = target)
print(“Target Found”)
print(“Target Found”)
elseif(cur^.data > target)
elseif(cur^.data > target) 23 47
Search(cur^.left, target)
Search(cur^.left, target)
else
else
Search(cur^.right, target)
Search(cur^.right, target)
endif
endif
endprocedure // Search 35
endprocedure // Search

target = 87
.Procedure Search( head
. Procedure Search(
cur iot in Ptr toa Node,
cur iot in 35)
Search(head, Ptr toa Node,
target iot in num)
target iot
Search(head, in num)
87)then
if(cur = NIL) 42
. if(cur = NIL) then
print(“Not Found”)
print(“Not Found”)
. elseif(cur^.data = target) cur
elseif(cur^.data = target)
print(“Target Found”)
print(“Target Found”)
elseif(cur^.data > target)
elseif(cur^.data > target) 23 47
Search(cur^.left, target)
Search(cur^.left, target)
else
else
Search(cur^.right, target)
Search(cur^.right, target)
endif
endif
endprocedure // Search 35
endprocedure // Search

target = 87
.Procedure Search( head
. Procedure
cur
Procedure
Search(
iot in Search(
Ptr toa Node,
cur iot
Search(head, in Ptr toa Node,
35)
target
cur iot
iotin inPtr
num)
toa Node,
target
Search(head,iot in
87) num)
if(cur
target
if(cur
= iot
=
NIL)
NIL)
inthen
num)
then 42
. print(“Not
if(cur = NIL) Found”)
then
print(“Not
. elseif(cur^.data Found”)
print(“Not Found”) = target)
elseif(cur^.data = target)
print(“Target
elseif(cur^.data Found”)
= target)
print(“Target Found”)
elseif(cur^.data
print(“Target Found”)
> target)
elseif(cur^.data > target) 23 47
Search(cur^.left,
elseif(cur^.data > target)
target)
Search(cur^.left, target)
else
Search(cur^.left, target)
else
Search(cur^.right,
else target) cur
Search(cur^.right, target)
endif
Search(cur^.right, target)
endif
endprocedure
endif // Search 35
endprocedure // Search
endprocedure // Search

target = 87
.Procedure Search( head
. Procedure
cur
Procedure
Search(
iot in Search(
Ptr toa Node,
cur iot
Search(head, in Ptr toa Node,
35)
target
cur iot
iotin inPtr
num)
toa Node,
target
Search(head,iot in
87) num)
if(cur
target
if(cur
= iot
=
NIL)
NIL)
inthen
num)
then 42
. print(“Not
if(cur = NIL) Found”)
then
print(“Not
. elseif(cur^.data Found”)
print(“Not Found”) = target)
elseif(cur^.data = target)
print(“Target
elseif(cur^.data Found”)
= target)
print(“Target Found”)
elseif(cur^.data
print(“Target Found”)
> target)
elseif(cur^.data > target) 23 47
Search(cur^.left,
elseif(cur^.data > target)
target)
Search(cur^.left, target)
else
Search(cur^.left, target)
else
Search(cur^.right,
else target) cur
Search(cur^.right, target)
endif
Search(cur^.right, target)
endif
endprocedure
endif // Search 35
endprocedure // Search
endprocedure // Search

Not Found
.Procedure Search( head
. Procedure
cur
Procedure
Search(
iot in Search(
Ptr toa Node,
cur iot
Search(head, in Ptr toa Node,
35)
target
cur iot
iotin inPtr
num)
toa Node,
target
Search(head,iot in
87) num)
if(cur
target
if(cur
= iot
=
NIL)
NIL)
inthen
num)
then 42
. print(“Not
if(cur = NIL) Found”)
then
print(“Not
. elseif(cur^.data Found”)
print(“Not Found”) = target)
elseif(cur^.data = target)
print(“Target
elseif(cur^.data Found”)
= target)
print(“Target Found”)
elseif(cur^.data
print(“Target Found”)
> target)
elseif(cur^.data > target) 23 47
Search(cur^.left,
elseif(cur^.data > target)
target)
Search(cur^.left, target)
else
Search(cur^.left, target)
else
Search(cur^.right,
else target) cur
Search(cur^.right, target)
endif
Search(cur^.right, target)
endif
endprocedure
endif // Search 35
endprocedure // Search
endprocedure // Search
.Procedure Search( head
. Procedure Search(
cur iot in Ptr toa Node,
cur iot in 35)
Search(head, Ptr toa Node,
target iot in num)
target iot
Search(head, in num)
87)then
if(cur = NIL) 42
. if(cur = NIL) then
print(“Not Found”)
print(“Not Found”)
. elseif(cur^.data = target) cur
elseif(cur^.data = target)
print(“Target Found”)
print(“Target Found”)
elseif(cur^.data > target)
elseif(cur^.data > target) 23 47
Search(cur^.left, target)
Search(cur^.left, target)
else
else
Search(cur^.right, target)
Search(cur^.right, target)
endif
endif
endprocedure // Search 35
endprocedure // Search
.Procedure Search( head
. cur iot in Ptr toa Node,
Search(head, 35)num)
target iot in cur
Search(head, 87)then
if(cur = NIL) 42
. print(“Not Found”)
. elseif(cur^.data = target)
print(“Target Found”)
elseif(cur^.data > target)
Search(cur^.left, target) 23 47
else
Search(cur^.right, target)
endif
endprocedure // Search 35
. head
.
Search(head, 35)
Search(head, 87) 42
.
.

23 47

35
Summary

• We can cut the work in half after each


comparison.

• Recurse in one direction – left or


right.

• When we reach NIL, then we no the


value wasn’t in the binary search
tree.
Questions?
Binary Search on a Sorted Array
The Scenario
• We have a sorted array
• We want to determine if a particular
element is in the array
– Once found, print or return
(index, boolean, etc.)
– If not found, indicate the element
is not in the collection

7 12 42 59 71 86 104 212
A Better Search

• Of course we could use our simpler


search and traverse the array

• But we can use the fact that the array


is sorted to our advantage

• This will allow us to reduce the number


of comparisons
Binary Search

• Requires a sorted array or a


binary search tree.
• Cuts the “search space” in half each time.
• Keeps cutting the search space in half
until the target is found or has exhausted
the all possible locations.
• Inappropriate for linked lists because
linked lists lack random access.
The Algorithm

look at “middle” element


if no match then
look left or right

1 7 9 12 33 42 59 76 81 84 91 92 93 99
The Algorithm

look at “middle” element


if no match then
look left or right

1 7 9 12 33 42 59 76 81 84 91 92 93 99
The Algorithm

look at “middle” element


if no match then
look left or right

1 7 9 12 33 42 59 76 81 84 91 92 93 99
The Algorithm

look at “middle” element


if no match then
look left or right

1 7 9 12 33 42 59 76 81 84 91 92 93 99
The Binary Search Algorithm

• Return found or not found (true or


false), so it should be a function.
• We’ll use recursion
• We must pass the entire array into the
module each pass, so set bounds
(search space) via index bounds
(parameters)
– We’ll need a first and last
The Binary Search Algorithm

calculate middle position


if (first and last have “crossed”) then
DO NOT FOUND WORK
elseif (element at middle = to_find) then
DO FOUND WORK
elseif to_find < element at middle then
Look to the left
else
Look to the right
Looking Left

• Use indices “first” and “last” to keep track of


where we are looking
• Move left by setting last = middle – 1

7 12 42 59 71 86 104 212

F L M L
Looking Right

• Use indices “first” and “last” to keep track of


where we are looking
• Move right by setting first = middle + 1

7 12 42 59 71 86 104 212

F M F L
Binary Search Example – Found

7 12 42 59 71 86 104 212

F M L

Looking for 42
Binary Search Example – Found

7 12 42 59 71 86 104 212

F M L

Looking for 42
Binary Search Example – Found

7 12 42 59 71 86 104 212

F
M
L
42 found – in 3 comparisons
Binary Search Example – Not Found

7 12 42 59 71 86 104 212

F M L

Looking for 89
Binary Search Example – Not Found

7 12 42 59 71 86 104 212

F M L

Looking for 89
Binary Search Example – Not Found

7 12 42 59 71 86 104 212

F L
M
Looking for 89
Binary Search Example – Not Found

7 12 42 59 71 86 104 212

L F

89 not found – 3 comparisons


Function Find returnsa boolean (A isoftype in
ArrayType, first, last, to_find isoftype in num)
// contract (Pre, Post, Purpose) here
middle isoftype num
middle <- (first + last) div 2
if (first > last) then
Find returns false
elseif (A[middle] = to_find) then
Find returns true
elseif (to_find < A[middle]) then
Find returns Find(A, first, middle–1, to_find)
else
Find returns Find(A, middle+1, last, to_find)
endfunction
Summary

• Binary search reduces the work by half at


each comparison

• With the array, we need to keep track of


which “part” is currently “visible”

• We know the element isn’t in the array


when our first and last indices “cross”
Questions?
Data Structure Conversion
and Helper Modules
The Scenario

• Imagine you have a collection of


data stored in a linked list.

• But now you need to store this data


in a binary search tree.

• You’ll need to convert the data from


one structure to the other without
altering the data itself.
An Example: List to Tree
Purpose of Structure Conversion

Given the input data arranged in one format:

• Keep the original data and


structure intact

• Return the same data, but arranged


in a different order or within a
different data structure
Conversion Examples

• Linked List to a Binary Search Tree


• Binary Search Tree to a Sorted Linked List
• Array to a Linked List
• Array to a Binary Search Tree

Any others are also valid, but going from a


dynamic structure to a static structure
may run out of allocated space.
Steps Involved

• Initialize the new structure


• Traverse the original structure
– At each element, insert the current
element into the new structure
• Return the new structure

Note that the original structure and data is


unchanged.
15 An Example: List to Tree
11

42

10

60

77

3
15 Initialize the
11
New Structure

42 Head

10

60

77

3
15
Traverse the Original
Structure and Insert into
11 the New Structure
42 Head

9
15
10

60

77

3
15
Traverse the Original
Structure and Insert into
11 the New Structure
42 Head

9
15
10
11
60

77

3
15
Traverse the Original
Structure and Insert into
11 the New Structure
42 Head

9
15
10
11 42
60

77

3
15
Traverse the Original
Structure and Insert into
11 the New Structure
42 Head

9
15
10
11 42
60
9
77

3
15
Traverse the Original
Structure and Insert into
11 the New Structure
42 Head

9
15
10
11 42
60
9
77
10
3
15
Traverse the Original
Structure and Insert into
11 the New Structure
42 Head

9
15
10
11 42
60
9 60
77
10
3
15
Traverse the Original
Structure and Insert into
11 the New Structure
42 Head

9
15
10
11 42
60
9 60
77
10 77
3
15
Traverse the Original
Structure and Insert into
11 the New Structure
42 Head

9
15
10
11 42
60
9 60
77
3 10 77
3
15 Done Traversing
11
Now Return the New Structure

42 Head

9
15
10
11 42
60
9 60
77
3 10 77
3
Needed Modules

• A module to initialize the new structure


• A module to traverse the initial structure
• A module to insert an element into the
new structure

• But all of this should be


transparent to the user!
Helper Modules

• In order to correctly perform some tasks,


helper modules are often necessary.

• Helper (or wrapper) modules are other


modules that help in the completion of a
task.

• They are often useful in hiding details.


The Conversion Helper Modules
Original New
Structure Structure

Convert
The Conversion Helper Modules
Original New
Structure Structure

Convert

Initialize

Traverse Original

Insert into New


A First Try at the Conversion Module

procedure Tree_from_List(cur isoftype


in Ptr toa List_Node, head isoftype
in/out Ptr toa Tree_Node )
// Pre: head initialized to NIL
if( cur <> NIL ) then
BST_Insert(head, cur^.data )
Tree_from_List(cur^.next, head )
endif
endprocedure
A First Try at the Algorithm
algorithm ListToTree
// some delcarations
TreePtr isoftype Ptr toa Tree_Node
ListPtr isoftype Ptr toa List_Node
// build the list work here
TreePtr <- NIL
Tree_from_List( ListPtr, TreePtr )
// now the tree is built
endalgorithm // ListToTree

The user is required to initialize the list pointer.


It is better to wrap the recursive call into a user
interface program as follows…
A Better Main Algorithm
The initialization should be hidden inside the
conversion module itself.

algorithm BetterListToTree
TreePtr isoftype Ptr toa Tree_Node
ListPtr isoftype Ptr toa List_Node

// build the list work is here

Nice_Tree_from_List( ListPtr, TreePtr )


// now the tree is built

endalgorithm // BetterListToTree
The “Wrapper” Module

procedure Nice_Tree_from_List( cur


isoftype in Ptr toa List_Node, head
isoftype out Ptr toa Tree_Node )

// perform the initialization


head <- NIL

// build the list


Tree_From_List( cur, head )
endprocedure
Summary

• Keep the original structure unchanged


• Initialize the new structure
• Traverse the old structure
– Insert each element into the new structure

• Helper (wrapper) modules


– Help hide details
– Properly abstract tasks into
separate modules
Questions?

You might also like