1 / 234

Lecture 14

Lecture 14. Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array Traversals. Traversals of Linked Lists. The Scenario. We need to visit each element in the linked list. At each element, we do some work.

chiku
Download Presentation

Lecture 14

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 14 Traversals of Linked ListsPreorder BST TraversalPostorder BST TraversalMiscellaneous BST TraversalsDepth First vs Breadth FirstArray Traversals

  2. Traversals of Linked Lists

  3. The Scenario • We need to visit each element in the linked list. • At each element, we do some work. • We’ll stop when we reach the end of the list. • Examples: • Printing all elements • Updating/changing the data of all elements

  4. Traversals • A traversal visits every element in a collection. • Two forms: • Recursive • Iterative (linear structures)

  5. Visiting Every Element // head 48 17 142 • Regardless of whether we use iteration or recursion, we continue until we reach the end (NIL).

  6. Don’t De-reference NIL • We must make sure we don’t de-reference (follow) the NIL pointer. • To do this, always test to see if the pointer is NIL. • If so, we can’t use the ‘^’ operator head // Can’t follow head! (i.e. head^ doesn’t work!)

  7. Testing for NIL • Always test for NIL at the beginning of your loop. • Your exitif statement must appear at the top • Always test for NIL at the beginning of your recursive module. • Your terminating condition must appear at the top of your module

  8. Iterative Traversal Template procedure Traverse (cur iot in Ptr toa Node) // Purpose: call Do_Something on each element // Precondition: none // Postcondition: Do_Something on each element loop exitif( cur = NIL ) Do_Something( cur^.data ) cur <- cur^.next endloop endprocedure // Traverse

  9. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List Called via Print_List(list_head) cur list_head 17 42 4

  10. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List cur list_head 17 42 4

  11. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 cur list_head 17 42 4

  12. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 cur list_head 17 42 4

  13. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 cur list_head 17 42 4

  14. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 cur list_head 17 42 4

  15. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 cur list_head 17 42 4

  16. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 cur list_head 17 42 4

  17. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 cur list_head 17 42 4

  18. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 cur list_head 17 42 4

  19. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 42 cur list_head 17 42 4

  20. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 42 cur list_head 17 42 4

  21. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 42 cur list_head 17 42 4

  22. An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 42 cur list_head 17 42 4

  23. Recursive Traversal Template procedure Traverse (cur iot in Ptr toa Node) // Purpose: call Do_Something on each element // Precondition: none // Postcondition: Do_Something on each element if (cur <> NIL) then Do_Something( cur^.data ) Traverse( cur^.next ) endif endprocedure // Traverse

  24. A Recursive Traversal Example procedure Print_List(cur iot in Ptr toa Node) if (cur <> NIL) then print(cur^.data) Print_List(cur^.next) endif endprocedure //Print_List Called via Print_List(list_head) cur list_head 17 42 4 You already know how to do this...

  25. A Loophole in Parameter Protection • If a pointer is an in parameter, the pointer cannot be changed. • But, anything to which the pointer points may be changed. • Thus, giving access to a list meansgiving ability to make changes to it; you cannot protect against this.

  26. -1 -1 -1 An Example procedure DestroyList (cur iot in Ptr toa Node) if (cur <> nil) then cur^.data <- -1 DestroyList(cur^.next) endif endprocedure // DestroyList // head 48 17 142

  27. 48 Another Example procedure DestroyList (cur iot in Ptr toa Node) cur^.next <- NIL endprocedure // DestroyList // head 48 17 142

  28. Summary • Traversals involve visiting every element • Recursion or iteration • Stop when you reach NIL • Make sure to not de-reference NIL • Be aware that passing a pointer to a module via an IN parameter allows the module to modify the entire list.

  29. Questions?

  30. Traversing a Binary Search Tree (BST)Pre-Order

  31. Outline of Pre-Order Traversal • Three principle steps: • Do work (Current) • Traverse Left • Traverse Right • Work can be anything • Separate work from traversal

  32. Traverse the tree “Pre-order”: • Visit the current and do work • Visit the tree’s left sub-tree • Visit right sub-tree

  33. Pre-Order Traversal Procedure procedure Pre_Order(cur iot in Ptr toa Tree_Node) • // Purpose: perform pre-order traversal, call • // DoWhatever for each node • // Preconditions: cur points to a binary tree • // Postcondition: DoWhatever will be performed • // on each tree node in “pre-order” order • if( cur <> NIL ) then • Do_Whatever( cur^.data ) • Pre_Order( cur^.left_child ) • Pre_Order( cur^.right_child ) • endif • endprocedure // Pre_Order

  34. 22 67 36 3 14 44 7 94 97 1 9 Proc PreOrderPrint(pointer) pointer NOT NIL? print(data) PreOrderPrint(left child) PreOrderPrint(right child) root P L R

  35. Questions?

  36. Traversing a Binary Search Tree (BST)Post-Order

  37. Outline of Post-Order Traversal • Three principle steps: • Traverse Left • Traverse Right • Do work (Current) • Work can be anything • Separate work from traversal

  38. Traverse the tree “Post order”: • Visit the tree’s left sub-tree • Visit right sub-tree • Visit the current and do work

  39. Post-Order Traversal Procedure procedure Post_Order(cur iot in Ptr toa Tree_Node) • // Purpose: perform post-order traversal, call • // DoWhatever for each node • // Preconditions: cur points to a binary tree • // Postcondition: DoWhatever will be performed • // on each tree node in “post-order” order • if( cur <> NIL ) then • Post_Order( cur^.left_child ) • Post_Order( cur^.right_child ) • Do_Whatever( cur^.data ) • endif • endprocedure // Post_Order

  40. 22 67 36 3 14 44 7 94 97 1 9 Proc PostOrderPrint(pointer) pointer NOT NIL? PostOrderPrint(left child) PostOrderPrint(right child) print(data) root L R P

  41. Questions?

  42. Miscellaneous BST Traversals

  43. Miscellaneous Traversals • Defined Traversals • In-order L C R • Pre-order C L R • Post-order L R C • Other Possibilities: • R C L • C RL • RL C

  44. 22 67 36 3 14 44 7 94 97 1 9 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P R L

  45. 22 67 36 3 14 44 7 94 97 1 9 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P R L

  46. 22 14 67 3 94 36 7 97 1 9 44 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P R L Output: 22

  47. 22 14 36 3 97 67 7 94 1 9 44 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P R R L Output: 22

  48. 22 14 36 3 97 67 7 94 1 9 44 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P R R L P Output: 22 67

  49. 22 67 36 3 1 14 7 94 97 9 44 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P R R L P Output: 22 67 R

  50. 22 67 36 3 1 14 7 94 97 9 44 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P R R L P Output: 22 67 94 R P

More Related