Searching via Traversals Searching a Binary Search Tree
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. • 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 ) ok. To. Exit isoftype Boolean ok. To. Exit <- FALSE loop exitif(ok. To. Exit) if(cur = NIL OR cur^. data > target) then ok. To. Exit <- TRUE print(“Target data not found”) elseif(cur^. data = target) then ok. To. Exit = 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!
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: Head 35 42 12 Found! 12 5 \
Searching in an Unordered Collection • Let’s determine if the value 13 is in the collection: Head 35 42 12 13 Not Found! 5 \
Searching in an Ordered Collection • Let’s determine if the value 13 is in the collection: Head 5 12 35 13 Not Found! 42 \
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 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 = 4 head cur 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 = 4 head cur 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 = 4 head cur 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 data (cur^. data Target found = 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
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 target = 9 head cur 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 target = 9 head cur 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
procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur =Target NIL) OR (cur^. data not found>= 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
Searching an Array 12 42 35 101 77 5 • 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 Num. Array. Type, 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 Example 2 MAX is 6. . . Search(My. Num. Array, 13). . . endalgorithm // Example 2
procedure Search(my_array isoftype in Num. Array. Type, 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 7 12 5 22 13 32 1 2 3 4 5 6
procedure Search(my_array isoftype in Num. Array. Type, 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 7 12 5 22 13 32 1 2 3 4 5 6
procedure Search(my_array isoftype in Num. Array. Type, 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 7 12 5 22 13 32 1 2 3 4 5 6
procedure Search(my_array isoftype in Num. Array. Type, 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 7 12 5 22 13 32 1 2 3 4 5 6
procedure Search(my_array isoftype in Num. Array. Type, 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 7 12 5 22 13 32 1 2 3 4 5 6
procedure Search(my_array isoftype in Num. Array. Type, 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 7 12 5 22 13 32 1 2 3 4 5 6
procedure Search(my_array isoftype in Num. Array. Type, 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 7 12 5 22 13 32 1 2 3 4 5 6
procedure Search(my_array isoftype in Num. Array. Type, target isoftype in Num) i isoftype Num i <- 1 Target data found 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 7 12 5 22 13 32 1 2 3 4 5 6
procedure Search(my_array isoftype in Num. Array. Type, 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 7 12 5 22 13 32 1 2 3 4 5 6
Page 130 Binary Tree Searches 14 9 3 42 28 56
Searching With Binary Trees Have to visit every node in the binary tree. 3 types of traversals: Preorder Check Node Inorder Go left Postorder Go left Check Node 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, to. Find iot in Num) if (cur = NIL) then BTSearch returns false elseif (cur^. data = to. Find) then BTSearch returns true else BTSearch returns BTSearch(cur^. left, to. Find) OR BTSearch(cur^. right, to. Find) endif endfunction // BTSearch
LB cur Find 14 94 36 3 22 14 67 9
LB Find 14 94 cur 36 3 22 14 67 9
LB Find 14 94 36 3 cur 22 14 67 9
LB Find 14 94 36 3 cur 22 14 67 9
LB Find 14 94 36 3 cur 22 14 67 9
LB Find 14. . . Found!!! 94 3 22 36 cur 14 67 9
LB Search on a Binary Tree function BTSearch returnsa Boolean (cur iot in ptr toa Node, to. Find iot in Num) if (cur = NIL) then BTSearch returns false elseif (cur^. data = to. Find) then BTSearch returns true else BTSearch returns BTSearch(cur^. left, to. Find) OR BTSearch(cur^. right, to. Find) endif endfunction // BTSearch 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 Looking for 42… It must be to the right! Less Than 14 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
. . Search(head, 35) Search(head, 87). . head 42 47 23 35
. . Search(head, 35) Search(head, 87). . head 42 47 23 35
. Procedure Search(. cur iot in Ptr toa Node, Search(head, 35)num) target iot in Search(head, 87)then if(cur = NIL). 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 = 35 head cur 42 47 23 35
. Procedure Search(. cur iot in Ptr toa Node, Search(head, 35)num) target iot in Search(head, 87)then if(cur = NIL). 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 = 35 head cur 42 47 23 35
. Procedure Search(. cur iot in Ptr toa Node, Search(head, 35)num) target iot in Search(head, 87)then if(cur = NIL). 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 = 35 head cur 42 47 23 35
. Procedure Search(. cur iot in Ptr toa Node, Search(head, 35)num) target iot in Search(head, 87)then if(cur = NIL). 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 = 35 head cur 42 47 23 35
. Procedure Search(. Procedure cur iot in Ptr toa Node, cur iot in 35) Ptr toa Node, Search(head, target iot in num) Search(head, 87)then 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 = 35 head 42 cur 47 23 35
. Procedure Search(. Procedure cur iot in Ptr toa Node, cur iot in 35) Ptr toa Node, Search(head, target iot in num) Search(head, 87)then if(cur = NIL) then. if(cur 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 = 35 head 42 cur 47 23 35
. Procedure Search(. Procedure cur iot in Ptr toa Node, cur iot in 35) Ptr toa Node, Search(head, target iot in num) Search(head, 87)then if(cur = NIL) then. if(cur 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 = 35 head 42 cur 47 23 35
. Procedure Search(. Procedure cur iot in Ptr toa Node, cur iot in 35) Ptr toa Node, Search(head, target iot in num) Search(head, 87)then if(cur = NIL) then. if(cur 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 = 35 head 42 cur 47 23 35
. Procedure Search(. Procedure cur iot in Search( Ptr toa Node, cur iot in Ptr toa Node, Search(head, 35) target iotin in num) cur iot Ptr toa Node, target iot in num) Search(head, if(cur target = NIL) iot 87) in then num) if(cur = NIL) then. print(“Not if(cur = NIL) Found”) then print(“Not Found”). elseif(cur^. data print(“Not Found”) = target) elseif(cur^. data = target) print(“Target Found”) elseif(cur^. data > target) Search(cur^. left, elseif(cur^. data target) > target) Search(cur^. left, target) else Search(cur^. right, target) endif endprocedure endif // Search endprocedure // Search head 42 35 cur target = 35 47 23
. Procedure Search(. Procedure cur iot in Search( Ptr toa Node, cur iot in Ptr toa Node, Search(head, 35) target iotin in num) cur iot Ptr toa Node, target iot in num) Search(head, if(cur target = NIL) iot 87) in then num) if(cur = NIL) then. print(“Not if(cur = NIL) Found”) then print(“Not Found”). elseif(cur^. data print(“Not Found”) = target) elseif(cur^. data = target) print(“Target Found”) elseif(cur^. data > target) Search(cur^. left, elseif(cur^. data target) > target) Search(cur^. left, target) else Search(cur^. right, target) endif endprocedure endif // Search endprocedure // Search head 42 35 cur target = 35 47 23
. Procedure Search(. Procedure cur iot in Search( Ptr toa Node, cur iot in Ptr toa Node, Search(head, 35) target iotin in num) cur iot Ptr toa Node, target iot in num) Search(head, if(cur target = NIL) iot 87) in then num) if(cur = NIL) then. print(“Not if(cur = NIL) Found”) then print(“Not Found”). elseif(cur^. data print(“Not Found”) = target) elseif(cur^. data = target) print(“Target Found”) elseif(cur^. data > target) Search(cur^. left, elseif(cur^. data target) > target) Search(cur^. left, target) else Search(cur^. right, target) endif endprocedure endif // Search endprocedure // Search head 42 35 cur Target Found 47 23
. Procedure Search(. Procedure cur iot in Search( Ptr toa Node, cur iot in Ptr toa Node, Search(head, 35) target iotin in num) cur iot Ptr toa Node, target iot in num) Search(head, if(cur target = NIL) iot 87) in then num) if(cur = NIL) then. print(“Not if(cur = NIL) Found”) then print(“Not Found”). elseif(cur^. data print(“Not Found”) = target) elseif(cur^. data = target) print(“Target Found”) elseif(cur^. data > target) Search(cur^. left, elseif(cur^. data target) > target) Search(cur^. left, target) else Search(cur^. right, target) endif endprocedure endif // Search endprocedure // Search head 42 47 23 35 cur
. Procedure Search(. Procedure cur iot in Ptr toa Node, cur iot in 35) Ptr toa Node, Search(head, target iot in num) Search(head, 87)then if(cur = NIL) then. if(cur 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 cur 47 23 35
. Procedure Search(. cur iot in Ptr toa Node, Search(head, 35)num) target iot in Search(head, 87)then if(cur = NIL). 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 cur 42 47 23 35
. . Search(head, 35) Search(head, 87). . head 42 47 23 35
. Procedure Search(. cur iot in Ptr toa Node, Search(head, 35)num) target iot in Search(head, 87)then if(cur = NIL). 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 head cur 42 47 23 35
. Procedure Search(. cur iot in Ptr toa Node, Search(head, 35)num) target iot in Search(head, 87)then if(cur = NIL). 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 head cur 42 47 23 35
. Procedure Search(. cur iot in Ptr toa Node, Search(head, 35)num) target iot in Search(head, 87)then if(cur = NIL). 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 head cur 42 47 23 35
. Procedure Search(. cur iot in Ptr toa Node, Search(head, 35)num) target iot in Search(head, 87)then if(cur = NIL). 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 head cur 42 47 23 35
. Procedure Search(. Procedure cur iot in Ptr toa Node, cur iot in 35) Ptr toa Node, Search(head, target iot in num) Search(head, 87)then 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 head 42 cur 47 23 35
. Procedure Search(. Procedure cur iot in Ptr toa Node, cur iot in 35) Ptr toa Node, Search(head, target iot in num) Search(head, 87)then if(cur = NIL) then. if(cur 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 head 42 cur 47 23 35
. Procedure Search(. Procedure cur iot in Ptr toa Node, cur iot in 35) Ptr toa Node, Search(head, target iot in num) Search(head, 87)then if(cur = NIL) then. if(cur 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 head 42 cur 47 23 35
. Procedure Search(. Procedure cur iot in Ptr toa Node, cur iot in 35) Ptr toa Node, Search(head, target iot in num) Search(head, 87)then if(cur = NIL) then. if(cur 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 head 42 cur 47 23 35
. Procedure Search(. Procedure cur iot in Search( Ptr toa Node, cur iot in Ptr toa Node, Search(head, 35) target iotin in num) cur iot Ptr toa Node, target iot in num) Search(head, if(cur target = NIL) iot 87) in then num) if(cur = NIL) then. print(“Not if(cur = NIL) Found”) then print(“Not Found”). elseif(cur^. data print(“Not Found”) = target) elseif(cur^. data = target) print(“Target Found”) elseif(cur^. data > target) Search(cur^. left, elseif(cur^. data target) > target) Search(cur^. left, target) else Search(cur^. right, target) endif endprocedure endif // Search endprocedure // Search target = 87 head 42 47 23 cur 35
. Procedure Search(. Procedure cur iot in Search( Ptr toa Node, cur iot in Ptr toa Node, Search(head, 35) target iotin in num) cur iot Ptr toa Node, target iot in num) Search(head, if(cur target = NIL) iot 87) in then num) if(cur = NIL) then. print(“Not if(cur = NIL) Found”) then print(“Not Found”). elseif(cur^. data print(“Not Found”) = target) elseif(cur^. data = target) print(“Target Found”) elseif(cur^. data > target) Search(cur^. left, elseif(cur^. data target) > target) Search(cur^. left, target) else Search(cur^. right, target) endif endprocedure endif // Search endprocedure // Search Not Found head 42 47 23 cur 35
. Procedure Search(. Procedure cur iot in Search( Ptr toa Node, cur iot in Ptr toa Node, Search(head, 35) target iotin in num) cur iot Ptr toa Node, target iot in num) Search(head, if(cur target = NIL) iot 87) in then num) if(cur = NIL) then. print(“Not if(cur = NIL) Found”) then print(“Not Found”). elseif(cur^. data print(“Not Found”) = target) elseif(cur^. data = target) print(“Target Found”) elseif(cur^. data > target) Search(cur^. left, elseif(cur^. data target) > target) Search(cur^. left, target) else Search(cur^. right, target) endif endprocedure endif // Search endprocedure // Search head 42 47 23 cur 35
. Procedure Search(. Procedure cur iot in Ptr toa Node, cur iot in 35) Ptr toa Node, Search(head, target iot in num) Search(head, 87)then if(cur = NIL) then. if(cur 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 cur 47 23 35
. Procedure Search(. cur iot in Ptr toa Node, Search(head, 35)num) target iot in Search(head, 87)then if(cur = NIL). 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 cur 42 47 23 35
. . Search(head, 35) Search(head, 87). . head 42 47 23 35
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 7 12 42 59 71 86 104 212
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 1 7 9 12 33 42 59 76 81 84 91 92 93 99
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
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
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
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 – 1 7 F 12 42 59 L M 71 86 104 212 L
Looking Right • Use indices “first” and “last” to keep track of where we are looking • Move right by setting first = middle + 1 7 F 12 42 59 71 M F 86 104 212 L
Binary Search Example – Found 7 F 12 42 59 71 M Looking for 42 86 104 212 L
Binary Search Example – Found 7 12 42 F M L 59 71 Looking for 42 86 104 212
Binary Search Example – Found 7 12 42 59 71 86 104 F M L 42 found – in 3 comparisons 212
Binary Search Example – Not Found 7 F 12 42 59 71 M Looking for 89 86 104 212 L
Binary Search Example – Not Found 7 12 42 59 71 F Looking for 89 86 M 104 212 L
Binary Search Example – Not Found 7 12 42 59 71 Looking for 89 86 104 212 F M L
Binary Search Example – Not Found 7 12 42 59 71 86 104 L F 89 not found – 3 comparisons 212
Function Find returnsa boolean (A isoftype in Array. Type, 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 to a Sorted 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.
15 11 42 9 10 60 77 3 An Example: List to Tree
15 11 42 9 10 60 77 3 Initialize the New Structure Head
15 11 42 9 10 60 77 3 Traverse the Original Structure and Insert into the New Structure Head 15
15 11 42 Traverse the Original Structure and Insert into the New Structure Head 9 10 60 77 3 15 11
15 11 42 Traverse the Original Structure and Insert into the New Structure Head 9 10 60 77 3 15 11 42
15 11 Traverse the Original Structure and Insert into the New Structure 42 Head 9 15 10 60 77 3 11 9 42
15 11 Traverse the Original Structure and Insert into the New Structure 42 Head 9 15 10 60 77 3 11 9 10 42
15 11 Traverse the Original Structure and Insert into the New Structure 42 Head 9 15 10 60 77 3 11 9 42 60 10
15 11 Traverse the Original Structure and Insert into the New Structure 42 Head 9 15 10 60 77 3 11 9 42 60 10 77
15 11 Traverse the Original Structure and Insert into the New Structure 42 Head 9 15 10 11 60 77 3 9 3 42 60 10 77
15 11 Done Traversing Now Return the New Structure 42 Head 9 15 10 11 60 77 3 9 3 42 60 10 77
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 Original Structure New Structure Convert
The Conversion Helper Modules Original Structure New Structure Convert Initialize 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 List. To. Tree // some delcarations Tree. Ptr isoftype Ptr toa Tree_Node List. Ptr isoftype Ptr toa List_Node // build the list work here Tree. Ptr <- NIL Tree_from_List( List. Ptr, Tree. Ptr ) // now the tree is built endalgorithm // List. To. Tree 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 Better. List. To. Tree. Ptr isoftype Ptr toa Tree_Node List. Ptr isoftype Ptr toa List_Node // build the list work is here Nice_Tree_from_List( List. Ptr, Tree. Ptr ) // now the tree is built endalgorithm // Better. List. To. Tree
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?
- Slides: 167