 Download Presentation Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversi

Loading in 2 Seconds...

# Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversi - PowerPoint PPT Presentation

Lecture 15. Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules . LB. Plan of Attack. Simple searching just involves traversing a data structure until the data element is found. I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation ## Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversi

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
1. Lecture 15 Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversionand Helper Modules

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

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

4. LB procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) okToExit isoftype Boolean okToExit <- FALSE loop exitif(okToExit) if(cur = NIL OR cur^.data > target) then okToExit <- TRUE print(“Target data not found”) elseif(cur^.data = target) then okToExit = TRUE print(“Found target”) // Could transfer data to param here else cur <- cur^.next endif endloop endprocedure // Search

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

6. Searching via Traversals

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

8. Another Situation • I have the same student database. • I need to view Alice Jones’ grades and update them. • Again, I need to search the database. • This time, I need 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: Head \\ 5 35 42 12 12 Found!

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

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

16. Search Examples Arrays Linked Lists Binary Trees

17. Search Example on a List

18. An Iterative Search 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 inNum ) 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 head 7 22 8 4 34

28. procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- 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

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 cur target = 4 head 7 22 8 4 34

30. procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- 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

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 cur target = 4 head 7 22 8 4 34

32. procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- 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

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 cur target = 4 head 7 22 8 4 34

34. procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- 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

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 cur target = 4 head 7 22 8 4 34

36. procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- 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

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 cur target = 4 head 7 22 8 4 34

38. procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- 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

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 cur target = 4 head 7 22 8 4 34

40. procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- 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

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 cur target = 4 head 7 22 8 4 34

42. procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- 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

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 cur target = 4 head 7 22 8 4 34

44. procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search Target data found cur target = 4 head 7 22 8 4 34

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 cur target = 4 head 7 22 8 4 34

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

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 cur target = 9 head 4 7 8 22 34

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 cur target = 9 head 4 7 8 22 34

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 cur target = 9 head 4 7 8 22 34

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 cur target = 9 head 4 7 8 22 34