Professional Documents
Culture Documents
Searching Via Traversals Searching A Binary Search Tree (BST) Binary Search On A Sorted Array Data Structure Conversion and Helper Modules
Searching Via Traversals Searching A Binary Search Tree (BST) Binary Search On A Sorted Array Data Structure Conversion and Helper Modules
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
Head
35 42 12 5 \\
12 Found!
Searching in an Unordered Collection
Head
35 42 12 5 \\
13 Not Found!
Searching in an Ordered Collection
Head
5 12 35 42 \\
13 Not Found!
Search Examples
Arrays
Linked Lists
Binary Trees
Search Example on a List
An Iterative Search
Steps:
– Traverse until we find a match
or reach the end.
– Print the results.
An Iterative Un-Ordered Search
procedure Search ( ??? )
loop
???
exitif( ??? )
???
endloop
loop
???
exitif( ??? )
???
endloop
loop
exitif( cur = NIL )
cur <- cur^.next
endloop
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
loop
exitif((cur = NIL) OR (cur^.data >= target))
cur <- cur^.next
endloop
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
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
12 42 35 101 77 5
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
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
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
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
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
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
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
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
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
my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Page 130
14
9 42
3 28 56
Searching With Binary Trees
3 types of traversals:
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
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.
It must be to the
right!
Less
Greater
Than
Than
14
14
The Binary Search Algorithm
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
7 12 42 59 71 86 104 212
A Better Search
1 7 9 12 33 42 59 76 81 84 91 92 93 99
The Algorithm
1 7 9 12 33 42 59 76 81 84 91 92 93 99
The Algorithm
1 7 9 12 33 42 59 76 81 84 91 92 93 99
The Algorithm
1 7 9 12 33 42 59 76 81 84 91 92 93 99
The Binary Search Algorithm
7 12 42 59 71 86 104 212
F L M L
Looking Right
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
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
Convert
The Conversion Helper Modules
Original New
Structure Structure
Convert
Initialize
Traverse Original
algorithm BetterListToTree
TreePtr isoftype Ptr toa Tree_Node
ListPtr isoftype Ptr toa List_Node
endalgorithm // BetterListToTree
The “Wrapper” Module