Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules."— Presentation transcript:

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

2 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

3 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 722 8434 head Page 122

4 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

5 Same logic can be applied to Trees Skipping stuff starting on Page 120! LB

6 Searching via Traversals

7 An Example Imagine we have a database of students I want to know if Bob Smith is in my class. I want to search my database and have the module print out “IN THE CLASS” or “NOT IN THE CLASS.”

8 Another Situation I have the same student database. I need to view Alice Jones’ grades and update them. Again, I need to search the database. This time, I need Alice’s information returned to me so I can view and modify them (i.e. need access).

9 The Scenario We have some collection (stored in an array, tree, or list) –May be sorted or not –May be empty or not We want the determine if a particular element is in the collection or not. We need to search for the element.

10 Searching Given the collection and an element to find… Determine whether the “target” element was found in the collection –Print a message –Return a value (an index or pointer, etc.) Don’t modify the collection in the search!

11 Key Fields Often, our collections will hold complex structures (i.e. have many items of information in each). It is common to organize our collection using one “part” (or field) –Name –Student Number This is called the “key field” Then we can search on the key field.

12 A Simple Search A search traverses the collection until –The desired element is found –Or the collection is exhausted If the collection is ordered, I might not have to look at all elements –I can stop looking when I know the element cannot be in the collection.

13 Searching in an Unordered Collection Let’s determine if the value 12 is in the collection: 3542 12 5 \\ 12 Found! Head

14 Searching in an Unordered Collection Let’s determine if the value 13 is in the collection: 3542 12 5 \\ 13 Not Found! Head

15 Searching in an Ordered Collection Let’s determine if the value 13 is in the collection: 512 35 42 \\ 13 Not Found! Head

16 Search Examples Arrays Linked Lists Binary Trees

17 Search Example on a List

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

19 An Iterative Un-Ordered Search procedure Search ( ??? ) loop ??? exitif( ??? ) ??? endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

20 An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop ??? exitif( ??? ) ??? endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

21 An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif( cur = NIL ) cur <- cur^.next endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

22 An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

23 An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( cur = NIL ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

24 An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( cur = NIL ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

25 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

26 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

27 Tracing the Search Now let’s call the un-ordered version of the module: algorithm Example... Search(head, 4)... endalgorithm // Example 722 8434 head

28 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 722 8434 head target = 4 cur

29 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 722 8434 head target = 4 cur

30 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 722 8434 head target = 4 cur

31 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 722 8434 head target = 4 cur

32 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 722 8434 head target = 4 cur

33 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 722 8434 head target = 4 cur

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 722 8434 head target = 4 cur

35 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 722 8434 head target = 4 cur

36 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 722 8434 head target = 4 cur

37 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 722 8434 head target = 4 cur

38 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 722 8434 head target = 4 cur

39 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 722 8434 head target = 4 cur

40 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 722 8434 head target = 4 cur

41 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 722 8434 head target = 4 cur

42 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 722 8434 head target = 4 cur

43 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 722 8434 head target = 4 cur

44 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 722 8434 head target = 4 cur Target data found

45 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 722 8434 head target = 4 cur

46 Tracing the Ordered Search Now let’s call the ordered version of the module: algorithm Example... Search(head, 9)... endalgorithm // Example 47 82234 head

47 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 47 82234 head

48 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 47 82234 head

49 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 47 82234 head

50 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 47 82234 head

51 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 47 82234 head

52 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 47 82234 head

53 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 47 82234 head Target data not found

54 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 47 82234 head

55 Searching an Array Same principles apply: –Examine cells until we find a match or exhaust the possibilities –Then indicate whether the item was in the collection (print or return information) 12 42 35 101 77 5

56 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

57 Calling the Module algorithm Example2 MAX is 6... Search(MyNumArray, 13)... endalgorithm // Example2

58 procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 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 7125221332 123456 target = 13

59 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 7125221332 123456 target = 13

60 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 7125221332 123456 target = 13

61 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 7125221332 123456 target = 13

62 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 7125221332 123456 target = 13

63 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 7125221332 123456 target = 13

64 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 7125221332 123456 target = 13

65 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 7125221332 123456 target = 13 Target data found

66 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 7125221332 123456 target = 13

67 Binary Tree Searches 14 9 28 42 3 56 Page 130

68 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

69 Pre-Order Search Traversal Algorithm As soon as we get to a node, check to see if we have a match Otherwise, look for the element in the left sub-tree Otherwise, look for the element in the right sub-tree 14 Left ??? Right ???

70 Search on a Binary Tree function BTSearch returnsa Boolean (cur iot in ptr toa Node, toFind iot in Num) if (cur = NIL) then BTSearch returns false elseif (cur^.data = toFind) then BTSearch returns true else BTSearch returns BTSearch(cur^.left, toFind) OR BTSearch(cur^.right, toFind) endif endfunction // BTSearch

71 94 cur 3667221493 Find 14 LB

72 94 cur 3667221493 Find 14 LB

73 943667221493 Find 14 cur LB

74 943667 22 1493 Find 14 cur LB

75 943667 22 1493 Find 14 cur LB

76 943667 2214 93 Find 14...Found!!! cur LB

77 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?

78 Summary Searches involve visiting elements in a collection until –A match is found –Or until all possible locations are exhausted With sorted collections, we may be able to “stop early” Once we determine if the element is in the collection, then we do some work: –Print the results –Return some information (pointer, index)

79 Questions?

80 Searching a Binary Search Tree (BST)

81 The Scenario 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

82 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

83 The Binary Search Algorithm if at NIL // not found DO NOT FOUND WORK elseif ( match ) then // found DO FOUND WORK elseif ( value to match < current value ) recurse left // must be to left else recurse right // must be to right

84 The Binary Search for a BST procedure Search(cur iot in Ptr toa Node, target isoftype in Num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search

85 head 42. Search(head, 35) Search(head, 87). 23 35 47

86 head 42. Search(head, 35) Search(head, 87). 23 35 47

87 . Search(head, 35) Search(head, 87). head 42 23 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 47 target = 35 cur

88 . Search(head, 35) Search(head, 87). head 42 23 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 47 target = 35 cur

89 . Search(head, 35) Search(head, 87). head 42 23 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 47 target = 35 cur

90 . Search(head, 35) Search(head, 87). head 42 23 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 47 target = 35 cur

91 . Search(head, 35) Search(head, 87). head 42 23 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 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

92 . Search(head, 35) Search(head, 87). head 42 23 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 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

93 . Search(head, 35) Search(head, 87). head 42 23 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 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

94 . Search(head, 35) Search(head, 87). head 42 23 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 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

95 . Search(head, 35) Search(head, 87). head 42 23 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 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

96 . Search(head, 35) Search(head, 87). head 42 23 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 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

97 . Search(head, 35) Search(head, 87). head 42 23 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 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

98 . Search(head, 35) Search(head, 87). head 42 23 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 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

99 . Search(head, 35) Search(head, 87). head 42 23 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 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

100 . Search(head, 35) Search(head, 87). head 42 23 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 47 cur

101 . Search(head, 35) Search(head, 87). head 42 23 35 47

102 . Search(head, 35) Search(head, 87). head 42 23 35 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 target = 87 cur

103 . Search(head, 35) Search(head, 87). head 42 23 35 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 target = 87 cur

104 . Search(head, 35) Search(head, 87). head 42 23 35 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 target = 87 cur

105 . Search(head, 35) Search(head, 87). head 42 23 35 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 target = 87 cur

106 . Search(head, 35) Search(head, 87). head 42 23 35 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 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

107 . Search(head, 35) Search(head, 87). head 42 23 35 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 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

108 . Search(head, 35) Search(head, 87). head 42 23 35 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 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

109 . Search(head, 35) Search(head, 87). head 42 23 35 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 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

110 . Search(head, 35) Search(head, 87). head 42 23 35 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 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

111 . Search(head, 35) Search(head, 87). head 42 23 35 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 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

112 . Search(head, 35) Search(head, 87). head 42 23 35 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 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

113 . Search(head, 35) Search(head, 87). head 42 23 35 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

114 . Search(head, 35) Search(head, 87). head 42 23 35 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

115 . Search(head, 35) Search(head, 87). head 42 23 35 47

116 Summary We can cut the work in half after each comparison. Recurse in one direction – left or right. When we reach NIL, then we no the value wasn’t in the binary search tree.

117 Questions?

118 Binary Search on a Sorted Array

119 The Scenario We have a sorted array We want to determine if a particular element is in the array –Once found, print or return (index, boolean, etc.) –If not found, indicate the element is not in the collection 71242597186104212

120 A Better Search Of course we could use our simpler search and traverse the array But we can use the fact that the array is sorted to our advantage This will allow us to reduce the number of comparisons

121 Binary Search Requires a sorted array or a binary search tree. Cuts the “search space” in half each time. Keeps cutting the search space in half until the target is found or has exhausted the all possible locations. Inappropriate for linked lists because linked lists lack random access.

122 The Algorithm look at “middle” element if no match then look left or right 1 7 9 12 33 42 59 76 81 84 91 92 93 99

123 The Algorithm look at “middle” element if no match then look left or right 1 7 9 12 33 42 59 76 81 84 91 92 93 99

124 The Algorithm look at “middle” element if no match then look left or right 1 7 9 12 33 42 59 76 81 84 91 92 93 99

125 The Algorithm look at “middle” element if no match then look left or right 1 7 9 12 33 42 59 76 81 84 91 92 93 99

126 The Binary Search Algorithm Return found or not found (true or false), so it should be a function. 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

127 The Binary Search Algorithm calculate middle position if (first and last have “crossed”) then DO NOT FOUND WORK elseif (element at middle = to_find) then DO FOUND WORK elseif to_find < element at middle then Look to the left else Look to the right

128 Looking Left Use indices “first” and “last” to keep track of where we are looking Move left by setting last = middle – 1 71242597186104212 FLM L

129 Looking Right Use indices “first” and “last” to keep track of where we are looking Move right by setting first = middle + 1 71242597186104212 FLM F

130 Binary Search Example – Found 71242597186104212 Looking for 42 FLM

131 Binary Search Example – Found 71242597186104212 Looking for 42 FLM

132 Binary Search Example – Found 71242597186104212 42 found – in 3 comparisons F L M

133 Binary Search Example – Not Found 71242597186104212 Looking for 89 FLM

134 Binary Search Example – Not Found 71242597186104212 Looking for 89 FLM

135 Binary Search Example – Not Found 71242597186104212 Looking for 89 FL M

136 Binary Search Example – Not Found 71242597186104212 89 not found – 3 comparisons FL

137 Function Find returnsa boolean (A isoftype in ArrayType, first, last, to_find isoftype in num) // contract (Pre, Post, Purpose) here middle isoftype num middle <- (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

138 Summary Binary search reduces the work by half at each comparison With the array, we need to keep track of which “part” is currently “visible” We know the element isn’t in the array when our first and last indices “cross”

139 Questions?

140 Data Structure Conversion and Helper Modules

141 The Scenario Imagine you have a collection of data stored in a linked list. But now you need to store this data in a binary search tree. You’ll need to convert the data from one structure to the other without altering the data itself.

142 An Example: List to Tree

143 Purpose of Structure Conversion Given the input data arranged in one format: Keep the original data and structure intact Return the same data, but arranged in a different order or within a different data structure

144 Conversion Examples Linked List to a Binary Search Tree Binary Search Tree to a Sorted Linked List Array to a Linked List Array to a Binary Search Tree Any others are also valid, but going from a dynamic structure to a static structure may run out of allocated space.

145 Steps Involved Initialize the new structure Traverse the original structure –At each element, insert the current element into the new structure Return the new structure Note that the original structure and data is unchanged.

146 An Example: List to Tree 9 10 60 77 3 42 11 15

147 Initialize the New Structure Head 9 10 60 77 3 42 11 15

148 Traverse the Original Structure and Insert into the New Structure Head 15 9 10 60 77 3 42 11 15

149 Traverse the Original Structure and Insert into the New Structure Head 15 9 10 60 77 3 42 11 15 11

150 Traverse the Original Structure and Insert into the New Structure Head 15 9 10 60 77 3 42 11 15 11 42

151 Traverse the Original Structure and Insert into the New Structure Head 15 9 10 60 77 3 42 11 15 11 42 9

152 Traverse the Original Structure and Insert into the New Structure Head 15 9 10 60 77 3 42 11 15 11 42 9 10

153 Traverse the Original Structure and Insert into the New Structure Head 15 9 10 60 77 3 42 11 15 11 42 9 10 60

154 Traverse the Original Structure and Insert into the New Structure Head 15 9 10 60 77 3 42 11 15 11 42 9 10 60 77

155 Traverse the Original Structure and Insert into the New Structure Head 15 9 10 60 77 3 42 11 15 11 42 9 10 60 77 3

156 Done Traversing Now Return the New Structure Head 15 9 10 60 77 3 42 11 15 11 42 9 10 60 77 3

157 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!

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

159 The Conversion Helper Modules Convert Original Structure New Structure

160 The Conversion Helper Modules Convert Initialize Original Structure New Structure Traverse Original Insert into New

161 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

162 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…

163 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

164 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

165 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

166 Questions?

167


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

Similar presentations


Ads by Google