Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules
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... LB
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 Page 122
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! LB
Searching via Traversals
An Example Imagine we have a database of students I want to know if Bob Smith is in my class. I want to search my database and have the module print out “IN THE CLASS” or “NOT IN THE CLASS.”
Another Situation I have the same student database. I need to view Alice Jones’ grades and update them. Again, I need to search the database. This time, I need Alice’s information returned to me so I can view and modify them (i.e. need access).
The Scenario We have some collection (stored in an array, tree, or list) –May be sorted or not –May be empty or not We want the determine if a particular element is in the collection or not. We need to search for the element.
Searching Given the collection and an element to find… Determine whether the “target” element was found in the collection –Print a message –Return a value (an index or pointer, etc.) Don’t modify the collection in the search!
Key Fields Often, our collections will hold complex structures (i.e. have many items of information in each). It is common to organize our collection using one “part” (or field) –Name –Student Number This is called the “key field” Then we can search on the key field.
A Simple Search A search traverses the collection until –The desired element is found –Or the collection is exhausted If the collection is ordered, I might not have to look at all elements –I can stop looking when I know the element cannot be in the collection.
Searching in an Unordered Collection Let’s determine if the value 12 is in the collection: \\ 12 Found! Head
Searching in an Unordered Collection Let’s determine if the value 13 is in the collection: \\ 13 Not Found! Head
Searching in an Ordered Collection Let’s determine if the value 13 is in the collection: \\ 13 Not Found! Head
Search Examples Arrays Linked Lists Binary Trees
Search Example on a List
An Iterative Search Let’s build a module to search a list of numbers using iteration. We’ll print whether the number was found or not. Steps: –Traverse until we find a match or reach the end. –Print the results.
An Iterative Un-Ordered Search procedure Search ( ??? ) loop ??? exitif( ??? ) ??? endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop ??? exitif( ??? ) ??? endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif( cur = NIL ) cur <- cur^.next endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( cur = NIL ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( cur = NIL ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
What’s Different for An Iterative Ordered Search? procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( cur = NIL ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
An Iterative Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
Tracing the Search Now let’s call the un-ordered version of the module: algorithm Example... Search(head, 4)... endalgorithm // Example head
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur
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 target = 4 cur Target data found
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 target = 4 cur
Tracing the Ordered Search Now let’s call the ordered version of the module: algorithm Example... Search(head, 9)... endalgorithm // Example head
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 target = 9 cur head
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 target = 9 cur head
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 target = 9 cur head
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 target = 9 cur head
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 target = 9 cur head
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 target = 9 cur head
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 target = 9 cur head Target data not found
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 target = 9 cur head
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)
Un-Ordered Iterative Array Search procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search
Calling the Module algorithm Example2 MAX is 6... Search(MyNumArray, 13)... endalgorithm // Example2
procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13
procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13
procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13
procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13
procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13
procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13
procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13
procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13 Target data found
procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13
Binary Tree Searches Page 130
Searching With Binary Trees Have to visit every node in the binary tree. 3 types of traversals: PreorderInorderPostorder Check Node Go left Go right Go left Check Node Go right Go left Go right Check Node
Pre-Order Search Traversal Algorithm As soon as we get to a node, check to see if we have a match Otherwise, look for the element in the left sub-tree Otherwise, look for the element in the right sub-tree 14 Left ??? Right ???
Search on a Binary Tree function BTSearch returnsa Boolean (cur iot in ptr toa Node, toFind iot in Num) if (cur = NIL) then BTSearch returns false elseif (cur^.data = toFind) then BTSearch returns true else BTSearch returns BTSearch(cur^.left, toFind) OR BTSearch(cur^.right, toFind) endif endfunction // BTSearch
94 cur Find 14 LB
94 cur Find 14 LB
Find 14 cur LB
Find 14 cur LB
Find 14 cur LB
Find 14...Found!!! cur LB
Search on a Binary Tree function BTSearch returnsa Boolean (cur iot in ptr toa Node, toFind iot in Num) if (cur = NIL) then BTSearch returns false elseif (cur^.data = toFind) then BTSearch returns true else BTSearch returns BTSearch(cur^.left, toFind) OR BTSearch(cur^.right, toFind) endif endfunction // BTSearch LB Once we’ve found the 14 do we search the right subtree?
Summary Searches involve visiting elements in a collection until –A match is found –Or until all possible locations are exhausted With sorted collections, we may be able to “stop early” Once we determine if the element is in the collection, then we do some work: –Print the results –Return some information (pointer, index)
Questions?
Searching a Binary Search Tree (BST)
The Scenario We’ve got a Binary Search Tree and we want to determine if an element is in the collection. 14 Less Than 14 Greater Than 14
Cutting the Work in Half In searching for a match, we can ignore half of the tree at each comparison. 14 Less Than 14 Looking for 42… It must be to the right! Greater Than 14
The Binary Search Algorithm if at NIL // not found DO NOT FOUND WORK elseif ( match ) then // found DO FOUND WORK elseif ( value to match < current value ) recurse left // must be to left else recurse right // must be to right
The Binary Search for a BST procedure Search(cur iot in Ptr toa Node, target isoftype in Num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search
head 42. Search(head, 35) Search(head, 87)
head 42. Search(head, 35) Search(head, 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 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 > 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 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 > 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 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 > 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Target Found cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 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 > 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 cur
. Search(head, 35) Search(head, 87). head
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 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 > 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > 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 > 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Not Found cur
. 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 > 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 > 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. 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 > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur
. Search(head, 35) Search(head, 87). head
Summary We can cut the work in half after each comparison. Recurse in one direction – left or right. When we reach NIL, then we no the value wasn’t in the binary search tree.
Questions?
Binary Search on a Sorted Array
The Scenario We have a sorted array We want to determine if a particular element is in the array –Once found, print or return (index, boolean, etc.) –If not found, indicate the element is not in the collection
A Better Search Of course we could use our simpler search and traverse the array But we can use the fact that the array is sorted to our advantage This will allow us to reduce the number of comparisons
Binary Search Requires a sorted array or a binary search tree. Cuts the “search space” in half each time. Keeps cutting the search space in half until the target is found or has exhausted the all possible locations. Inappropriate for linked lists because linked lists lack random access.
The Algorithm look at “middle” element if no match then look left or right
The Algorithm look at “middle” element if no match then look left or right
The Algorithm look at “middle” element if no match then look left or right
The Algorithm look at “middle” element if no match then look left or right
The Binary Search Algorithm Return found or not found (true or false), so it should be a function. We’ll use recursion We must pass the entire array into the module each pass, so set bounds (search space) via index bounds (parameters) –We’ll need a first and last
The Binary Search Algorithm calculate middle position if (first and last have “crossed”) then DO NOT FOUND WORK elseif (element at middle = to_find) then DO FOUND WORK elseif to_find < element at middle then Look to the left else Look to the right
Looking Left Use indices “first” and “last” to keep track of where we are looking Move left by setting last = middle – FLM L
Looking Right Use indices “first” and “last” to keep track of where we are looking Move right by setting first = middle FLM F
Binary Search Example – Found Looking for 42 FLM
Binary Search Example – Found Looking for 42 FLM
Binary Search Example – Found found – in 3 comparisons F L M
Binary Search Example – Not Found Looking for 89 FLM
Binary Search Example – Not Found Looking for 89 FLM
Binary Search Example – Not Found Looking for 89 FL M
Binary Search Example – Not Found not found – 3 comparisons FL
Function Find returnsa boolean (A isoftype in ArrayType, first, last, to_find isoftype in num) // contract (Pre, Post, Purpose) here middle isoftype num middle <- (first + last) div 2 if (first > last) then Find returns false elseif (A[middle] = to_find) then Find returns true elseif (to_find < A[middle]) then Find returns Find(A, first, middle–1, to_find) else Find returns Find(A, middle+1, last, to_find) endfunction
Summary Binary search reduces the work by half at each comparison With the array, we need to keep track of which “part” is currently “visible” We know the element isn’t in the array when our first and last indices “cross”
Questions?
Data Structure Conversion and Helper Modules
The Scenario Imagine you have a collection of data stored in a linked list. But now you need to store this data in a binary search tree. You’ll need to convert the data from one structure to the other without altering the data itself.
An Example: List to Tree
Purpose of Structure Conversion Given the input data arranged in one format: Keep the original data and structure intact Return the same data, but arranged in a different order or within a different data structure
Conversion Examples Linked List to a Binary Search Tree Binary Search Tree to a Sorted Linked List Array to a Linked List Array to a Binary Search Tree Any others are also valid, but going from a dynamic structure to a static structure may run out of allocated space.
Steps Involved Initialize the new structure Traverse the original structure –At each element, insert the current element into the new structure Return the new structure Note that the original structure and data is unchanged.
An Example: List to Tree
Initialize the New Structure Head
Traverse the Original Structure and Insert into the New Structure Head
Traverse the Original Structure and Insert into the New Structure Head
Traverse the Original Structure and Insert into the New Structure Head
Traverse the Original Structure and Insert into the New Structure Head
Traverse the Original Structure and Insert into the New Structure Head
Traverse the Original Structure and Insert into the New Structure Head
Traverse the Original Structure and Insert into the New Structure Head
Traverse the Original Structure and Insert into the New Structure Head
Done Traversing Now Return the New Structure Head
Needed Modules A module to initialize the new structure A module to traverse the initial structure A module to insert an element into the new structure But all of this should be transparent to the user!
Helper Modules In order to correctly perform some tasks, helper modules are often necessary. Helper (or wrapper) modules are other modules that help in the completion of a task. They are often useful in hiding details.
The Conversion Helper Modules Convert Original Structure New Structure
The Conversion Helper Modules Convert Initialize Original Structure New Structure Traverse Original Insert into New
A First Try at the Conversion Module procedure Tree_from_List(cur isoftype in Ptr toa List_Node, head isoftype in/out Ptr toa Tree_Node ) // Pre: head initialized to NIL if( cur <> NIL ) then BST_Insert(head, cur^.data ) Tree_from_List(cur^.next, head ) endif endprocedure
A First Try at the Algorithm algorithm ListToTree // some delcarations TreePtr isoftype Ptr toa Tree_Node ListPtr isoftype Ptr toa List_Node // build the list work here TreePtr <- NIL Tree_from_List( ListPtr, TreePtr ) // now the tree is built endalgorithm // ListToTree The user is required to initialize the list pointer. It is better to wrap the recursive call into a user interface program as follows…
A Better Main Algorithm The initialization should be hidden inside the conversion module itself. algorithm BetterListToTree TreePtr isoftype Ptr toa Tree_Node ListPtr isoftype Ptr toa List_Node // build the list work is here Nice_Tree_from_List( ListPtr, TreePtr ) // now the tree is built endalgorithm // BetterListToTree
The “Wrapper” Module procedure Nice_Tree_from_List( cur isoftype in Ptr toa List_Node, head isoftype out Ptr toa Tree_Node ) // perform the initialization head <- NIL // build the list Tree_From_List( cur, head ) endprocedure
Summary Keep the original structure unchanged Initialize the new structure Traverse the old structure –Insert each element into the new structure Helper (wrapper) modules –Help hide details –Properly abstract tasks into separate modules
Questions?