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. 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 4 17 42
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 4 17 42
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 4 17 42
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 4 17 42
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 4 17 42
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 4 17 42
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 4 17 42
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 4 17 42
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 4 17 42
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 4 17 42
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 4 17 42
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 4 17 42
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 4 17 42
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 4 17 42
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 4 17 42 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 means giving ability to make changes to it; you cannot protect against this.
An Example procedure DestroyList (cur iot in Ptr toa Node) if (cur <> nil) then cur^.data <- -1 DestroyList(cur^.next) endif endprocedure // DestroyList head -1 -1 48 17 -1 142 //
Another Example // head 48 48 17 142 procedure DestroyList (cur iot in Ptr toa Node) cur^.next <- NIL endprocedure // DestroyList head 48 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.
Questions?
Traversing a Binary Search Tree (BST) Pre-Order
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 9 67 3 14 36 94 44 97 1 7 Proc PreOrderPrint(pointer) pointer NOT NIL? print(data) PreOrderPrint(left child) PreOrderPrint(right child) root P 22 L R 9 67 3 14 36 94 44 97 1 7
Questions?
Traversing a Binary Search Tree (BST) Post-Order
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 9 67 3 14 36 94 44 97 1 7 Proc PostOrderPrint(pointer) pointer NOT NIL? PostOrderPrint(left child) PostOrderPrint(right child) print(data) root L 22 R P 9 67 3 14 36 94 44 97 1 7
Questions?
Miscellaneous BST Traversals
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 R L R L C
22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P 22 R L 9 67 3 14 36 94 44 97 1 7
22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P 22 R L 9 67 3 14 36 94 44 97 1 7
22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R L 9 67 Output: 22 3 14 36 94 44 97 1 7
22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L Output: 22 9 67 3 14 36 94 44 97 1 7
22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L P Output: 22 67 9 67 3 14 36 94 44 97 1 7
22 67 9 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L P 67 Output: 22 67 9 R 3 14 36 94 44 97 1 7
22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L P 9 67 Output: 22 67 94 R 3 14 P 36 94 44 97 1 7
22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L P Output: 22 67 94 9 67 R 3 14 P 36 94 R 44 97 1 7
22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L P Output: 22 67 94 97 9 67 R 3 14 P 36 94 R P 44 97 1 7
22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L P Output: 22 67 94 97 9 67 R 3 14 P 36 94 R P 44 97 1 7 R
22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L L P P Output: 22 67 94 97 36 44 9 14 3 7 1 9 67 R R L L P 3 P 14 P P 36 94 R R R R L L L L P P P P 44 97 1 7 R R R R L L L L
Questions?
Depth First vs. Breadth First Traversal
Depth vs. Breadth First Traversals Depth First Traversals Go down (deep) In-, Pre-, Post-order Breadth First Traversals Go across (shallow) Require a queue to help
Depth-First Traversal root 22 9 67 3 14 36 94 44 97 1 7
Breadth-First Traversal root 22 9 67 3 14 36 94 44 97 1 7
Depth First Traversal (In-Order) Procedure DFT (current isoftype in Ptr toa Node) // In order DFT if(current <> NIL) then DFT(current^.left) print(current^.data) DFT(Current^.right) endif endprocedure
Depth First Traversal (Pre-Order) Procedure DFT (current isoftype in Ptr toa Node) // In order DFT if(current <> NIL) then DFT(current^.left) print(current^.data) DFT(Current^.right) endif endprocedure Move this here to make a Preorder DFT
Depth First Traversal (Post-Order) Procedure DFT (current isoftype in Ptr toa Node) // In order DFT if(current <> NIL) then DFT(current^.left) print(current^.data) DFT(Current^.right) endif endprocedure Move this here to make a Postorder DFT
We have already seen these! Proc DFT(pointer) pointer NOT NIL? DFT(left child) print(data) DFT(right child) root 22 9 67 We have already seen these! 3 14 36 94 44 97 1 7
Breadth-First Traversal Requires a queue to maintain which nodes to visit next. Enqueue the root pointer Loop until no elements in the queue Dequeue(current) Enqueue current’s left and right children Do work at current
Breadth-First Traversal LB Breadth-First Traversal Procedure BFT(root isoftype in Ptr toa Node) Q isoftype Queue Initialize(Q) temp isoftype Ptr toa Node OK isoftype Boolean enqueue(Q, root) // continued
Breadth-First Traversal LB Breadth-First Traversal loop dequeue(temp, OK, Q) exitif(NOT OK) if(temp^.left <> NIL) then enqueue(temp^.left, Q)) endif if(temp^.right <> NIL) then enqueue(temp^.right, Q) print(temp^.data) // processing node endloop endprocedure
22 9 67 Q: 3 14 36 94 aNode: 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(data) endloop root 22 9 67 Q: 3 14 36 94 aNode: 44 97 1 7
22 9 67 Q:22 3 14 36 94 aNode: 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(data) endloop root 22 9 67 Q:22 3 14 36 94 aNode: 44 97 1 7
22 9 67 Q:22 3 14 36 94 aNode: 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(data) endloop root 22 9 67 Q:22 3 14 36 94 aNode: 44 97 1 7
22 9 67 Q: 3 14 36 94 aNode:22 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q: 3 14 36 94 aNode:22 44 97 1 7
22 9 67 Q:9 67 3 14 36 94 aNode:22 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:9 67 3 14 36 94 aNode:22 44 97 1 7
22 9 67 Q:9 67 3 14 36 94 aNode:22 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:9 67 3 14 36 94 aNode:22 44 97 1 7
22 9 67 Q:9 67 3 14 36 94 aNode:22 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:9 67 3 14 36 94 aNode:22 44 97 1 7
22 9 67 Q:67 3 14 36 94 aNode:9 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:67 3 14 36 94 aNode:9 44 97 1 7
22 9 67 Q:67 3 14 3 14 36 94 aNode:9 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:67 3 14 3 14 36 94 aNode:9 44 97 1 7
22 9 67 Q:67 3 14 3 14 36 94 aNode:9 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:67 3 14 3 14 36 94 aNode:9 44 97 1 7
22 9 67 Q:67 3 14 3 14 36 94 aNode:9 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:67 3 14 3 14 36 94 aNode:9 44 97 1 7
22 9 67 Q:3 14 3 14 36 94 aNode:67 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:3 14 3 14 36 94 aNode:67 44 97 1 7
22 9 67 Q:3 14 36 94 3 14 36 94 aNode:67 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:3 14 36 94 3 14 36 94 aNode:67 44 97 1 7
22 9 67 Q:3 14 36 94 3 14 36 94 aNode:67 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:3 14 36 94 3 14 36 94 aNode:67 44 97 1 7
22 9 67 Q:3 14 36 94 3 14 36 94 aNode:67 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:3 14 36 94 3 14 36 94 aNode:67 44 97 1 7
22 9 67 Q:14 36 94 3 14 36 94 aNode:3 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:14 36 94 3 14 36 94 aNode:3 44 97 1 7
22 9 67 Q:14 36 94 1 7 3 14 36 94 aNode:3 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:14 36 94 1 7 3 14 36 94 aNode:3 44 97 1 7
22 9 67 Q:14 36 94 1 7 3 14 36 94 aNode:3 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:14 36 94 1 7 3 14 36 94 aNode:3 44 97 1 7
22 9 67 Q:14 36 94 1 7 3 14 36 94 aNode:3 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:14 36 94 1 7 3 14 36 94 aNode:3 44 97 1 7
22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7
22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7
22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7
22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7
22 9 67 Q:94 1 7 3 14 36 94 aNode:36 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:94 1 7 3 14 36 94 aNode:36 44 97 1 7
22 9 67 Q:94 1 7 44 3 14 36 94 aNode:36 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:94 1 7 44 3 14 36 94 aNode:36 44 97 1 7
22 9 67 Q:94 1 7 44 3 14 36 94 aNode:36 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:94 1 7 44 3 14 36 94 aNode:36 44 97 1 7
22 9 67 Q:94 1 7 44 3 14 36 94 aNode:36 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:94 1 7 44 3 14 36 94 aNode:36 44 97 1 7
22 9 67 Q:1 7 44 3 14 36 94 aNode:94 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:1 7 44 3 14 36 94 aNode:94 44 97 1 7
22 9 67 Q:1 7 44 97 3 14 36 94 aNode:94 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:1 7 44 97 3 14 36 94 aNode:94 44 97 1 7
22 9 67 Q:1 7 44 97 3 14 36 94 aNode:94 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:1 7 44 97 3 14 36 94 aNode:94 44 97 1 7
22 9 67 Q:1 7 44 97 3 14 36 94 aNode:94 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:1 7 44 97 3 14 36 94 aNode:94 44 97 1 7
22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7
22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7
22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7
22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7
22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7
22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7
22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7
22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7
22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7
22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7
22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7
22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7
22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7
22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7
22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7
22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7
Queuestions?
Traversals of Arrays
The Scenario We need to visit each element in the array. At each element, we do some work. We’ll stop when we reach the end of the array (when we reach MAX). 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)
Recursive Array Traversal Template procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Purpose: call Do_Something on each element // Precondition: i = 1 on first call // Postcondition: Do_Something on each element if (i <= MAX) then Do_Something(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure
Recursive Array Traversal Example procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 12 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 12 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 i = 2 12 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 i = 2 12 43 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 i = 2 12 43 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 i = 1 12 43 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 i = 1 12 43 11 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 i = 1 12 43 11 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 i = 1 12 43 11 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 i = 1 12 43 11 9 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 i = 1 12 43 11 9 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 5 i = 1 12 43 11 9 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 5 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 5 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5
i = 2 i = 3 i = 4 i = 5 i = 6 i = 1 12 43 11 9 98 NumArray 12 43 11 9 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 5 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 6 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 5 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5
procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5
Iterative Traversal Template procedure Traverse (my_array iot in/out ArrayType) // Purpose: call Do_Something on each element // Precondition: none // Postcondition: Do_Something on each element i isoftype Num i <- 1 loop exitif( i > MAX ) Do_Something( my_array[i] ) i <- i + 1 endloop endprocedure // Traverse
An Iterative Traversal Example Traverse the array and fill in every element so that the ith element contains the value i. 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 i
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 i 1
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 i 1
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 i
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 1 2 3 4 5 6 7 8 9 10 i 1
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 1 2 3 4 5 6 7 8 9 10 i 2
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 1 2 3 4 5 6 7 8 9 10 i 2
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 1 2 3 4 5 6 7 8 9 10 i 2
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 1 2 3 4 5 6 7 8 9 10 i 2
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 1 2 3 4 5 6 7 8 9 10 i 2
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 1 2 3 4 5 6 7 8 9 10 i 3
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 1 2 3 4 5 6 7 8 9 10 i 3
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 1 2 3 4 5 6 7 8 9 10 i 3
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 1 2 3 4 5 6 7 8 9 10 i 3
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 1 2 3 4 5 6 7 8 9 10 i 3
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 1 2 3 4 5 6 7 8 9 10 i 4
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 1 2 3 4 5 6 7 8 9 10 i 4
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 1 2 3 4 5 6 7 8 9 10 i 4
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 i 5
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10 i 6
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 10 i 7
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 10 i 8
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 10 i 9
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 i 9
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 i 10
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 i 10
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 i 10
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 i 10
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 i 10
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 i 11
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 i 11
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 i 11
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 i 11
MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 i 11
A More Complex Problem Write a module which returns an array with the following characteristics: Every odd element has the first half of the alphabet in ascending order & every even element has the second half of the alphabet in descending order. A[1] = ‘A’ A[2] = ‘Z’ A[3] = ‘B’ A[4] = ‘Y’ A[5] = ‘C’ A[6] = ‘X’ . A[23] = ‘L’ A[24] = ‘O’ A[25] = ‘M’ A[26] = ‘N’
A A 1 2 3 4 5 . . . 23 24 25 26 Z B Y C L O M N Every odd element has the first half of the alphabet in ascending order & every even element has the second half of the alphabet in descending order.
A First Try ALPHAS is 26 A isoftype CharArrayType i isoftype Num up isoftype char i <- 1 up <- ‘A’ loop exitif( i > ALPHAS ) A[i] <- up up <- next_character(up) i <- i + 2 endloop
A 1 2 3 4 5 . . . 23 24 25 26 A B C L M Every odd element has the first half of the alphabet in ascending order & every even element has the second half of the alphabet in descending order.
ALPHAS is 26 A isoftype Array_Type i isoftype Num up, down isoftype char i <- 1 up <- ‘A’ down <- ‘Z’ loop exitif( i > ALPHAS ) A[i] <- up A[i + 1] <- down up <- next_character(up) down <- previous_character(down) i <- i + 2 endloop
A A 1 2 3 4 5 . . . 23 24 25 26 Z B Y C L O M N Every odd element has the first half of the alphabet in ascending order & every even element has the second half of the alphabet in descending order.
A 2-D Iterative Traversal Example We have a 2-D array We want to traverse the array and set each element to the product of it’s row position and column position (i.e. the element at position [i,j] should contain the value i*j) 1 2 3 4 1 2 3 1 2 3 4 i 2 4 6 8 3 6 9 12 j
MAX_COLS is 4 Row_Type definesa Array[1..MAX_COLS] of Num Sample isoftype Row_type Sample: 1 2 3 4
1 Data: 2 3 1 2 3 4 MAX_ROWS is 3 MAX_COLS is 4 Row_Type definesa Array[1..MAX_COLS] of Num Matrix definesa Array[1..MAX_ROWS] of Row_Type Data isoftype Matrix Data: 1 2 3 1 2 3 4
MAX_ROWS is 3 MAX_COLS is 4 Row_Type definesa Array[1..MAX_COLS] of Num Matrix definesa Array[1..MAX_ROWS] of Row_Type Data isoftype Matrix Row, Col isoftype Num Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1
1 Data: 2 3 Row: Col: 1 2 3 4 MAX_ROWS is 3 MAX_COLS is 4 Row_Type definesa Array[1..MAX_COLS] of Num Matrix definesa Array[1..MAX_ROWS] of Row_Type Data isoftype Matrix Row, Col isoftype Num Data: 1 2 3 Row: Col: 1 2 3 4
Data: 1 2 3 Row: 1 Col: 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 Data: 1 2 3 Row: 1 Col: 1 2 3 4
Data: 1 2 3 Row: 1 Col: 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 Data: 1 2 3 Row: 1 Col: 1 2 3 4
Data: 1 2 3 Row: 1 Col: 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 Data: 1 2 3 Row: 1 Col: 1 2 3 4
Data: 1 2 3 Row: 1 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 Data: 1 2 3 Row: 1 Col: 1 1 2 3 4
Data: 1 2 3 Row: 1 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 Data: 1 2 3 Row: 1 Col: 1 1 2 3 4
Data: 1 2 3 Row: 1 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 Data: 1 2 3 Row: 1 Col: 1 1 2 3 4
1 Data: 1 2 3 Row: 1 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 1 Data: 1 2 3 Row: 1 Col: 1 1 2 3 4
1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4
1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4
1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4
2 1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 2 1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4
2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4
2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4
2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4
3 2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 3 2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4
3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4
3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4
3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4
4 3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4
4 3 2 1 Data: 1 2 3 Row: 1 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 1 Col: 5 1 2 3 4
4 3 2 1 Data: 1 2 3 Row: 1 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 1 Col: 5 1 2 3 4
4 3 2 1 Data: 1 2 3 Row: 1 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 1 Col: 5 1 2 3 4
4 3 2 1 Data: 1 2 3 Row: 2 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 5 1 2 3 4
4 3 2 1 Data: 1 2 3 Row: 2 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 5 1 2 3 4
4 3 2 1 Data: 1 2 3 Row: 2 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 5 1 2 3 4
4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 What would happen if we did this outside first loop? 4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4
4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4
4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4
4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4
4 3 2 1 Data: 1 2 3 Row: 2 Col: 2 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 2 1 2 3 4
4 3 2 1 6 Data: 1 2 3 Row: 2 Col: 3 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 6 Data: 1 2 3 Row: 2 Col: 3 1 2 3 4
4 3 2 1 8 6 Data: 1 2 3 Row: 2 Col: 4 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 Data: 1 2 3 Row: 2 Col: 4 1 2 3 4
4 3 2 1 8 6 Data: 1 2 3 Row: 3 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 Data: 1 2 3 Row: 3 Col: 1 1 2 3 4
4 3 2 1 8 6 Data: 1 2 3 Row: 3 Col: 2 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 Data: 1 2 3 Row: 3 Col: 2 1 2 3 4
4 3 2 1 8 6 9 Data: 1 2 3 Row: 3 Col: 3 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 9 Data: 1 2 3 Row: 3 Col: 3 1 2 3 4
4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 3 Col: 4 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 3 Col: 4 1 2 3 4
4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 3 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 3 Col: 5 1 2 3 4
4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 3 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 3 Col: 5 1 2 3 4
4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 4 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 4 Col: 5 1 2 3 4
4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 4 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 4 Col: 5 1 2 3 4
and continue… 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 4 Col: 5 1 2 3 4 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 and continue… 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 4 Col: 5 1 2 3 4
Summary Traversals involve visiting every element. Recursion or iteration Do some work at each element When doing iterative traversals on arrays Use a loop for each dimension of the array Embed loops Typically perform work in innermost loop
Questions?