2.35k likes | 2.52k Views
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.
E N D
Lecture 14 Traversals of Linked ListsPreorder BST TraversalPostorder BST TraversalMiscellaneous BST TraversalsDepth First vs Breadth FirstArray Traversals
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
Traversals • A traversal visits every element in a collection. • Two forms: • Recursive • Iterative (linear structures)
Visiting Every Element // head 48 17 142 • Regardless of whether we use iteration or recursion, we continue until we reach the end (NIL).
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!)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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...
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.
-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
48 Another Example procedure DestroyList (cur iot in Ptr toa Node) cur^.next <- NIL endprocedure // DestroyList // head 48 17 142
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.
Outline of Pre-Order Traversal • Three principle steps: • Do work (Current) • Traverse Left • Traverse Right • Work can be anything • Separate work from traversal
Traverse the tree “Pre-order”: • Visit the current and do work • Visit the tree’s left sub-tree • Visit right sub-tree
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
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
Outline of Post-Order Traversal • Three principle steps: • Traverse Left • Traverse Right • Do work (Current) • Work can be anything • Separate work from traversal
Traverse the tree “Post order”: • Visit the tree’s left sub-tree • Visit right sub-tree • Visit the current and do work
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
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
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
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
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
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
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
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
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
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