Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules - PowerPoint PPT Presentation

About This Presentation
Title:

Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules

Description:

Changes each quarter ... Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion – PowerPoint PPT presentation

Number of Views:532
Avg rating:3.0/5.0
Slides: 168
Provided by: JonA79
Category:

less

Transcript and Presenter's Notes

Title: Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules


1
Searching via Traversals Searching a Binary
Search Tree (BST) Binary Search on a Sorted
Array Data Structure Conversionand Helper
Modules
Lecture 15
2
Plan 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...

3
Page 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
4
LB
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
5
Same logic can be applied to Trees
LB
Skipping stuff starting on Page 120!
6
Searching via Traversals
7
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.

8
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 Alices information returned to
    me so I can view and modify them (i.e. need
    access).

9
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.

10
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.)
  • Dont modify the collection in the search!

11
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.

12
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.

13
Searching in an Unordered Collection
  • Lets determine if the value 12 is in the
    collection

Head
\\
5
35
42
12
12 Found!
14
Searching in an Unordered Collection
  • Lets determine if the value 13 is in the
    collection

Head
\\
5
35
42
12
13 Not Found!
15
Searching in an Ordered Collection
  • Lets determine if the value 13 is in the
    collection

Head
\\
42
5
12
35
13 Not Found!
16
Search Examples
Arrays
Linked Lists
Binary Trees
17
Search Example on a List
18
An 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.

19
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

20
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

21
An 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

22
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 lt- cur.next
  • endloop
  • if( ??? ) then
  • print(Target data not found)
  • else
  • print(Target data found)
  • endif
  • endprocedure // Search

23
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 lt- cur.next
  • endloop
  • if( cur NIL ) then
  • print(Target data not found)
  • else
  • print(Target data found)
  • endif
  • endprocedure // Search

24
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 lt- cur.next
  • endloop
  • if( cur NIL ) then
  • print(Target data not found)
  • else
  • print(Target data found)
  • endif
  • endprocedure // Search

25
Whats 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

26
An 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

27
Tracing 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
28
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
cur
target 4
7
22
head
8
4
34
29
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
cur
target 4
7
22
head
8
4
34
30
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
cur
target 4
7
22
head
8
4
34
31
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
cur
target 4
7
22
head
8
4
34
32
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
cur
target 4
7
22
head
8
4
34
33
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
cur
target 4
7
22
head
8
4
34
34
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
cur
target 4
7
22
head
8
4
34
35
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
cur
target 4
7
22
head
8
4
34
36
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
cur
target 4
7
22
head
8
4
34
37
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
cur
target 4
7
22
head
8
4
34
38
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
cur
target 4
7
22
head
8
4
34
39
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
cur
target 4
7
22
head
8
4
34
40
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
cur
target 4
7
22
head
8
4
34
41
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
cur
target 4
7
22
head
8
4
34
42
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
cur
target 4
7
22
head
8
4
34
43
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
cur
target 4
7
22
head
8
4
34
44
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
Target data found
cur
target 4
7
22
head
8
4
34
45
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
cur
target 4
7
22
head
8
4
34
46
Tracing 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
47
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
cur
target 9
4
7
head
8
22
34
48
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
cur
target 9
4
7
head
8
22
34
49
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
cur
target 9
4
7
head
8
22
34
50
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
cur
target 9
4
7
head
8
22
34
51
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
cur
target 9
4
7
head
8
22
34
52
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
cur
target 9
4
7
head
8
22
34
53
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
Target data not found
cur
target 9
4
7
head
8
22
34
54
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
cur
target 9
4
7
head
8
22
34
55
Searching 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)

56
Un-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
57
Calling the Module
  • algorithm Example2
  • MAX is 6
  • . . .
  • Search(MyNumArray, 13)
  • . . .
  • endalgorithm // Example2

58
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
my_array
7
12
5
22
13
32
target 13
1
2
3
4
5
6
59
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
my_array
7
12
5
22
13
32
target 13
1
2
3
4
5
6
60
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
my_array
7
12
5
22
13
32
target 13
1
2
3
4
5
6
61
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
my_array
7
12
5
22
13
32
target 13
1
2
3
4
5
6
62
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
my_array
7
12
5
22
13
32
target 13
1
2
3
4
5
6
63
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
my_array
7
12
5
22
13
32
target 13
1
2
3
4
5
6
64
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
my_array
7
12
5
22
13
32
target 13
1
2
3
4
5
6
65
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
Target data found
my_array
7
12
5
22
13
32
target 13
1
2
3
4
5
6
66
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
my_array
7
12
5
22
13
32
target 13
1
2
3
4
5
6
67
Binary Tree Searches
Page 130
14
9
42
56
28
3
68
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 right
Go left Check Node Go right
Go left Go right Check Node
69
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 ???
70
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

71
LB
cur
Find 14
72
LB
Find 14
cur
73
LB
Find 14
74
LB
Find 14
22
75
LB
Find 14
22
76
LB
Find 14...Found!!!
22
14
77
Search 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?
78
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)

79
Questions?
80
Searching a Binary Search Tree (BST)
81
The 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
82
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 theright!
Greater Than 14
Less Than 14
83
The 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

84
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 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
116
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 wasnt in
    the binary search tree.

117
Questions?
118
Binary Search on a Sorted Array
119
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

120
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

121
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.

122
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
123
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
124
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
125
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
126
The 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

127
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 lt element at middle then
  • Look to the left
  • else
  • Look to the right

128
Looking 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
129
Looking 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
130
Binary Search Example Found
F
L
M
Looking for 42
131
Binary Search Example Found
F
L
M
Looking for 42
132
Binary Search Example Found
F
M
L
42 found in 3 comparisons
133
Binary Search Example Not Found
F
L
M
Looking for 89
134
Binary Search Example Not Found
F
L
M
Looking for 89
135
Binary Search Example Not Found
F
L
M
Looking for 89
136
Binary 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

138
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 isnt in the array when our
    first and last indices cross

139
Questions?
140
Data Structure Conversionand Helper Modules
141
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.
  • Youll need to convert the data from one
    structure to the other without altering the data
    itself.

142
An Example List to Tree
143
Purpose 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

144
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.

145
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.

146
An Example List to Tree
15
11
42
9
10
60
77
3
147
Initialize the New Structure
15
11
42
Head
9
10
60
77
3
148
Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
60
77
3
149
Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
60
77
3
150
Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
42
60
77
3
151
Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
42
60
9
77
3
152
Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
42
60
9
77
10
3
153
Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
42
60
9
60
77
10
3
154
Traverse the Original Structure and Insert
intothe New Structure
15
11
42
Head
9
15
10
11
42
60
9
60
77
10
77
3
155
Traverse 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
Write a Comment
User Comments (0)
About PowerShow.com