1.67k likes | 1.69k Views
Lecture 15. Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules. LB. Plan of Attack. Simple searching just involves traversing a data structure until the data element is found.
E N D
Lecture 15 Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversionand Helper Modules
LB Plan of Attack • Simple searching just involves traversing a data structure until the data element is found. • The only twist is that if the collection is ordered (like a linked lists) you can stop if you don’t find an element and you pass the point where it should be located. • So we’re going to skip numerous slides which essentially review material we’ve already covered. • So we’re going to focus on Binary Search and Data Structure Conversion • But first the twist...
Page 122 procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head 7 22 8 4 34
LB procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) okToExit isoftype Boolean okToExit <- FALSE loop exitif(okToExit) if(cur = NIL OR cur^.data > target) then okToExit <- TRUE print(“Target data not found”) elseif(cur^.data = target) then okToExit = TRUE print(“Found target”) // Could transfer data to param here else cur <- cur^.next endif endloop endprocedure // Search
LB Same logic can be applied to Trees Skipping stuff starting on Page 120!
An Example • Imagine we have a database of students • I want to know if Bob Smith is in my class. • I want to search my database and have the module print out“IN THE CLASS” or “NOT IN THE CLASS.”
Another Situation • I have the same student database. • I need to view Alice Jones’ grades and update them. • Again, I need to search the database. • This time, I need Alice’s information returned to me so I can view and modify them (i.e. need access).
The Scenario • We have some collection(stored in an array, tree, or list) • May be sorted or not • May be empty or not • We want the determine if a particular element is in the collection or not. • We need to search for the element.
Searching • Given the collection and an element to find… • Determine whether the “target” element was found in the collection • Print a message • Return a value (an index or pointer, etc.) • Don’t modify the collection in the search!
Key Fields • Often, our collections will hold complex structures (i.e. have many items of information in each). • It is common to organize our collection using one “part” (or field) • Name • Student Number • This is called the “key field” • Then we can search on the key field.
A Simple Search • A search traverses the collection until • The desired element is found • Or the collection is exhausted • If the collection is ordered, I might not have to look at all elements • I can stop looking when I know the element cannot be in the collection.
Searching in an Unordered Collection • Let’s determine if the value 12 is in the collection: Head \\ 5 35 42 12 12 Found!
Searching in an Unordered Collection • Let’s determine if the value 13 is in the collection: Head \\ 5 35 42 12 13 Not Found!
Searching in an Ordered Collection • Let’s determine if the value 13 is in the collection: Head \\ 42 5 12 35 13 Not Found!
Search Examples Arrays Linked Lists Binary Trees
An Iterative Search Let’s build a module to search a list of numbers using iteration. We’ll print whether the number was found or not. Steps: • Traverse until we find a match or reach the end. • Print the results.
An Iterative Un-Ordered Search procedure Search ( ??? ) loop ??? exitif( ??? ) ??? endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype inNum ) loop ??? exitif( ??? ) ??? endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif( cur = NIL ) cur <- cur^.next endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL)OR (cur^.data = target)) cur <- cur^.next endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( cur = NIL ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( cur = NIL ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
What’s Different for An Iterative Ordered Search? procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( cur = NIL ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
An Iterative Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
Tracing the Search Now let’s call the un-ordered version of the module: algorithm Example . . . Search(head, 4) . . . endalgorithm // Example head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search Target data found cur target = 4 head 7 22 8 4 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 4 head 7 22 8 4 34
Tracing the Ordered Search Now let’s call the ordered version of the module: algorithm Example . . . Search(head, 9) . . . endalgorithm // Example head 4 7 8 22 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 9 head 4 7 8 22 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 9 head 4 7 8 22 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 9 head 4 7 8 22 34
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search cur target = 9 head 4 7 8 22 34