Title: Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules
1Searching via Traversals Searching a Binary
Search Tree (BST) Binary Search on a Sorted
Array Data Structure Conversionand Helper
Modules
Lecture 15
2Plan of Attack
LB
- 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
dont find an element and you pass the point
where it should be located. - So were going to skip numerous slides which
essentially review material weve already
covered. - So were going to focus on Binary Search and Data
Structure Conversion - But first the twist...
3Page 122
procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
7
22
head
8
4
34
4LB
procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) okToExit
isoftype Boolean okToExit lt- FALSE loop
exitif(okToExit) if(cur NIL OR cur.data gt
target) then okToExit lt- TRUE
print(Target data not found)
elseif(cur.data target) then okToExit
TRUE print(Found target) // Could
transfer data to param here else cur lt-
cur.next endif endloop endprocedure //
Search
5Same logic can be applied to Trees
LB
Skipping stuff starting on Page 120!
6Searching via Traversals
7An 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.
8Another 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 Alices information returned to
me so I can view and modify them (i.e. need
access).
9The 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.
10Searching
- 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.)
- Dont modify the collection in the search!
11Key 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.
12A 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.
13Searching in an Unordered Collection
- Lets determine if the value 12 is in the
collection
Head
\\
5
35
42
12
12 Found!
14Searching in an Unordered Collection
- Lets determine if the value 13 is in the
collection
Head
\\
5
35
42
12
13 Not Found!
15Searching in an Ordered Collection
- Lets determine if the value 13 is in the
collection
Head
\\
42
5
12
35
13 Not Found!
16Search Examples
Arrays
Linked Lists
Binary Trees
17Search Example on a List
18An Iterative Search
- Lets build a module to search a list of numbers
using iteration. - Well print whether the number was found or not.
- Steps
- Traverse until we find a match or reach the end.
- Print the results.
19An Iterative Un-Ordered Search
- procedure Search ( ??? )
-
- loop
- ???
- exitif( ??? )
- ???
- endloop
- if( ??? ) then
- print(Target data not found)
- else
- print(Target data found)
- endif
- endprocedure // Search
20An 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
21An Iterative Un-Ordered Search
- procedure Search ( cur isoftype in ptr toa Node,
- target isoftype in Num )
-
- loop
- exitif( cur NIL )
- cur lt- cur.next
- endloop
- if( ??? ) then
- print(Target data not found)
- else
- print(Target data found)
- endif
- endprocedure // Search
22An 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 lt- cur.next
- endloop
- if( ??? ) then
- print(Target data not found)
- else
- print(Target data found)
- endif
- endprocedure // Search
23An 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 lt- cur.next
- endloop
- if( cur NIL ) then
- print(Target data not found)
- else
- print(Target data found)
- endif
- endprocedure // Search
24An 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 lt- cur.next
- endloop
- if( cur NIL ) then
- print(Target data not found)
- else
- print(Target data found)
- endif
- endprocedure // Search
25Whats 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 lt- cur.next
- endloop
- if( cur NIL ) then
- print(Target data not found)
- else
- print(Target data found)
- endif
- endprocedure // Search
26An Iterative Ordered Search
- procedure Search ( cur isoftype in ptr toa Node,
- target isoftype in Num )
-
- loop
- exitif((cur NIL) OR (cur.data gt target))
- cur lt- cur.next
- endloop
- if((cur NIL) OR (cur.data gt target)) then
- print(Target data not found)
- else
- print(Target data found)
- endif
- endprocedure // Search
27Tracing the Search
- Now lets call the un-ordered version of the
module - algorithm Example
- . . .
- Search(head, 4)
- . . .
- endalgorithm // Example
7
22
head
8
4
34
28procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
29procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
30procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
31procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
32procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
33procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
34procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
35procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
36procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
37procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
38procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
39procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
40procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
41procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
42procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
43procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
44procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
Target data found
cur
target 4
7
22
head
8
4
34
45procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data target))
cur lt- cur.next endloop if(cur NIL) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
cur
target 4
7
22
head
8
4
34
46Tracing the Ordered Search
- Now lets call the ordered version of the module
- algorithm Example
- . . .
- Search(head, 9)
- . . .
- endalgorithm // Example
4
7
head
8
22
34
47procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data gt target))
cur lt- cur.next endloop if((cur NIL) OR
(cur.data gt target)) then print(Target data
not found) else print(Target data
found) endif endprocedure // Search
cur
target 9
4
7
head
8
22
34
48procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data gt target))
cur lt- cur.next endloop if((cur NIL) OR
(cur.data gt target)) then print(Target data
not found) else print(Target data
found) endif endprocedure // Search
cur
target 9
4
7
head
8
22
34
49procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data gt target))
cur lt- cur.next endloop if((cur NIL) OR
(cur.data gt target)) then print(Target data
not found) else print(Target data
found) endif endprocedure // Search
cur
target 9
4
7
head
8
22
34
50procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data gt target))
cur lt- cur.next endloop if((cur NIL) OR
(cur.data gt target)) then print(Target data
not found) else print(Target data
found) endif endprocedure // Search
cur
target 9
4
7
head
8
22
34
51procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data gt target))
cur lt- cur.next endloop if((cur NIL) OR
(cur.data gt target)) then print(Target data
not found) else print(Target data
found) endif endprocedure // Search
cur
target 9
4
7
head
8
22
34
52procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data gt target))
cur lt- cur.next endloop if((cur NIL) OR
(cur.data gt target)) then print(Target data
not found) else print(Target data
found) endif endprocedure // Search
cur
target 9
4
7
head
8
22
34
53procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data gt target))
cur lt- cur.next endloop if((cur NIL) OR
(cur.data gt target)) then print(Target data
not found) else print(Target data
found) endif endprocedure // Search
Target data not found
cur
target 9
4
7
head
8
22
34
54procedure Search ( cur isoftype in ptr toa
Node, target isoftype in Num ) loop
exitif((cur NIL) OR (cur.data gt target))
cur lt- cur.next endloop if((cur NIL) OR
(cur.data gt target)) then print(Target data
not found) else print(Target data
found) endif endprocedure // Search
cur
target 9
4
7
head
8
22
34
55Searching an Array
- 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)
56Un-Ordered Iterative Array Search
procedure Search(my_array isoftype in
NumArrayType, target isoftype in
Num) i isoftype Num i lt- 1 loop
exitif((i gt MAX) OR (my_arrayi target)) i
lt- i 1 endloop if(i gt MAX) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
57Calling the Module
- algorithm Example2
- MAX is 6
- . . .
- Search(MyNumArray, 13)
- . . .
- endalgorithm // Example2
58procedure Search(my_array isoftype in
NumArrayType, target isoftype in
Num) i isoftype Num i lt- 1 loop
exitif((i gt MAX) OR (my_arrayi target)) i
lt- i 1 endloop if(i gt 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
59procedure Search(my_array isoftype in
NumArrayType, target isoftype in
Num) i isoftype Num i lt- 1 loop
exitif((i gt MAX) OR (my_arrayi target)) i
lt- i 1 endloop if(i gt 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
60procedure Search(my_array isoftype in
NumArrayType, target isoftype in
Num) i isoftype Num i lt- 1 loop
exitif((i gt MAX) OR (my_arrayi target)) i
lt- i 1 endloop if(i gt 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
61procedure Search(my_array isoftype in
NumArrayType, target isoftype in
Num) i isoftype Num i lt- 1 loop
exitif((i gt MAX) OR (my_arrayi target)) i
lt- i 1 endloop if(i gt 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
62procedure Search(my_array isoftype in
NumArrayType, target isoftype in
Num) i isoftype Num i lt- 1 loop
exitif((i gt MAX) OR (my_arrayi target)) i
lt- i 1 endloop if(i gt 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
63procedure Search(my_array isoftype in
NumArrayType, target isoftype in
Num) i isoftype Num i lt- 1 loop
exitif((i gt MAX) OR (my_arrayi target)) i
lt- i 1 endloop if(i gt 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
64procedure Search(my_array isoftype in
NumArrayType, target isoftype in
Num) i isoftype Num i lt- 1 loop
exitif((i gt MAX) OR (my_arrayi target)) i
lt- i 1 endloop if(i gt 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
65procedure Search(my_array isoftype in
NumArrayType, target isoftype in
Num) i isoftype Num i lt- 1 loop
exitif((i gt MAX) OR (my_arrayi target)) i
lt- i 1 endloop if(i gt MAX) then
print(Target data not found) else
print(Target data found) endif endprocedure
// Search
Target data found
my_array
7
12
5
22
13
32
target 13
1
2
3
4
5
6
66procedure Search(my_array isoftype in
NumArrayType, target isoftype in
Num) i isoftype Num i lt- 1 loop
exitif((i gt MAX) OR (my_arrayi target)) i
lt- i 1 endloop if(i gt 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
67Binary Tree Searches
Page 130
14
9
42
56
28
3
68Searching With Binary Trees
Have to visit every node in the binary tree.
3 types of traversals
Preorder
Inorder
Postorder
Check Node Go left Go right
Go left Check Node Go right
Go left Go right Check Node
69Pre-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 ???
70Search 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
71LB
cur
Find 14
72LB
Find 14
cur
73LB
Find 14
74LB
Find 14
22
75LB
Find 14
22
76LB
Find 14...Found!!!
22
14
77Search on a Binary Tree
LB
- 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 weve found the 14 do we search the right
subtree?
78Summary
- 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)
79Questions?
80Searching a Binary Search Tree (BST)
81The Scenario
- Weve got a Binary Search Tree and we want to
determine if an element is in the collection.
14
Greater Than 14
Less Than 14
82Cutting 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 theright!
Greater Than 14
Less Than 14
83The Binary Search Algorithm
- if at NIL // not found
- DO NOT FOUND WORK
- elseif ( match ) then // found
- DO FOUND WORK
- elseif ( value to match lt current value )
- recurse left // must be to left
- else
- recurse right // must be to right
84The 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 gt target)
- Search(cur.left, target)
- else
- Search(cur.right, target)
- endif
- endprocedure // Search
85. . Search(head, 35) Search(head, 87) . .
head
42
47
23
35
86. . Search(head, 35) Search(head, 87) . .
head
42
47
23
35
87. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
cur
42
47
23
35
target 35
88. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
cur
42
47
23
35
target 35
89. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
cur
42
47
23
35
target 35
90. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
cur
42
47
23
35
target 35
91. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
cur
47
23
35
target 35
92. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
cur
47
23
35
target 35
93. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
cur
47
23
35
target 35
94. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
cur
47
23
35
target 35
95. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
47
23
35
cur
target 35
96. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
47
23
35
cur
target 35
97. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
47
23
35
cur
Target Found
98. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
47
23
35
cur
99. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
cur
47
23
35
100. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
cur
42
47
23
35
101. . Search(head, 35) Search(head, 87) . .
head
42
47
23
35
102. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
cur
42
47
23
35
target 87
103. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
cur
42
47
23
35
target 87
104. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
cur
42
47
23
35
target 87
105. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
cur
42
47
23
35
target 87
106. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
47
23
35
target 87
107. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
47
23
35
target 87
108. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
47
23
35
target 87
109. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
47
23
35
target 87
110. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
47
23
35
target 87
111. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
47
23
35
Not Found
112. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
47
23
35
113. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
42
47
23
35
114. . Search(head, 35) Search(head, 87) . .
head
Procedure Search( cur iot in Ptr toa Node,
target iot in num) if(cur NIL) then
print(Not Found) elseif(cur.data target)
print(Target Found) elseif(cur.data gt
target) Search(cur.left, target) else
Search(cur.right, target) endif endprocedure //
Search
cur
42
47
23
35
115. . Search(head, 35) Search(head, 87) . .
head
42
47
23
35
116Summary
- 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 wasnt in
the binary search tree.
117Questions?
118Binary Search on a Sorted Array
119The 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
120A 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
121Binary 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.
122The 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
123The 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
124The 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
125The 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
126The Binary Search Algorithm
- Return found or not found (true or false), so it
should be a function. - Well use recursion
- We must pass the entire array into the module
each pass, so set bounds (search space) via index
bounds (parameters) - Well need a first and last
127The 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 lt element at middle then
- Look to the left
- else
- Look to the right
128Looking Left
- Use indices first and last to keep track of
where we are looking - Move left by setting last middle 1
L
F
L
M
129Looking Right
- Use indices first and last to keep track of
where we are looking - Move right by setting first middle 1
F
F
L
M
130Binary Search Example Found
F
L
M
Looking for 42
131Binary Search Example Found
F
L
M
Looking for 42
132Binary Search Example Found
F
M
L
42 found in 3 comparisons
133Binary Search Example Not Found
F
L
M
Looking for 89
134Binary Search Example Not Found
F
L
M
Looking for 89
135Binary Search Example Not Found
F
L
M
Looking for 89
136Binary Search Example Not Found
F
L
89 not found 3 comparisons
137- Function Find returnsa boolean (A isoftype in
ArrayType, first, last, to_find isoftype in num) - // contract (Pre, Post, Purpose) here
- middle isoftype num
- middle lt- (first last) div 2
- if (first gt last) then
- Find returns false
- elseif (Amiddle to_find) then
- Find returns true
- elseif (to_find lt Amiddle) then
- Find returns Find(A, first, middle1,
to_find) - else
- Find returns Find(A, middle1, last, to_find)
- endfunction
138Summary
- 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 isnt in the array when our
first and last indices cross
139Questions?
140Data Structure Conversionand Helper Modules
141The 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. - Youll need to convert the data from one
structure to the other without altering the data
itself.
142An Example List to Tree
143Purpose of Structure Conversion
- Given the input data arranged in one format
- Keep the original data andstructure intact
- Return the same data, but arrangedin a different
order or within a different data structure
144Conversion 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.
145Steps 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.
146An Example List to Tree
15
11
42
9
10
60
77
3
147Initialize the New Structure
15
11
42
Head
9
10
60
77
3
148Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
60
77
3
149Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
60
77
3
150Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
42
60
77
3
151Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
42
60
9
77
3
152Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
42
60
9
77
10
3
153Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
42
60
9
60
77
10
3
154Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
42
60
9
60
77
10
77
3
155Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
42
60
9
60
77
10
3
77
3
Slide 156