Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 14 Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array Traversals.

Similar presentations


Presentation on theme: "Lecture 14 Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array Traversals."— Presentation transcript:

1 Lecture 14 Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array 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 4 17 42

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 4 17 42

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 4 17 42

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 4 17 42

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 4 17 42

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 4 17 42

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 4 17 42

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 4 17 42

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 4 17 42

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 4 17 42

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 cur list_head 4 17 42

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 cur list_head 4 17 42

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 cur list_head 4 17 42

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 cur list_head 4 17 42

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 4 17 42 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 means giving ability to make changes to it; you cannot protect against this.

26 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 //

27 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 //

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

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

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 R L R L C

44 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

45 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

46 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

47 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

48 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

49 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

50 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: R 3 14 P 36 94 44 97 1 7

51 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: 9 67 R 3 14 P 36 94 R 44 97 1 7

52 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: 9 67 R 3 14 P 36 94 R P 44 97 1 7

53 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: 9 67 R 3 14 P 36 94 R P 44 97 1 7 R

54 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: 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

55 Questions?

56 Depth First vs. Breadth First Traversal

57 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

58 Depth-First Traversal
root 22 9 67 3 14 36 94 44 97 1 7

59 Breadth-First Traversal
root 22 9 67 3 14 36 94 44 97 1 7

60 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

61 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

62 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

63 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

64 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

65 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

66 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

67 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

68 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

69 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

70 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

71 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

72 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

73 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

74 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

75 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: 3 14 36 94 aNode:9 44 97 1 7

76 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: 3 14 36 94 aNode:9 44 97 1 7

77 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: 3 14 36 94 aNode:9 44 97 1 7

78 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

79 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 aNode:67 44 97 1 7

80 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 aNode:67 44 97 1 7

81 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 aNode:67 44 97 1 7

82 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: 3 14 36 94 aNode:3 44 97 1 7

83 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: 3 14 36 94 aNode:3 44 97 1 7

84 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: 3 14 36 94 aNode:3 44 97 1 7

85 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: 3 14 36 94 aNode:3 44 97 1 7

86 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: 3 14 36 94 aNode:14 44 97 1 7

87 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: 3 14 36 94 aNode:14 44 97 1 7

88 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: 3 14 36 94 aNode:14 44 97 1 7

89 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: 3 14 36 94 aNode:14 44 97 1 7

90 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

91 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: 3 14 36 94 aNode:36 44 97 1 7

92 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: 3 14 36 94 aNode:36 44 97 1 7

93 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: 3 14 36 94 aNode:36 44 97 1 7

94 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

95 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: 3 14 36 94 aNode:94 44 97 1 7

96 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: 3 14 36 94 aNode:94 44 97 1 7

97 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: 3 14 36 94 aNode:94 44 97 1 7

98 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: 3 14 36 94 aNode:1 44 97 1 7

99 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: 3 14 36 94 aNode:1 44 97 1 7

100 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: 3 14 36 94 aNode:1 44 97 1 7

101 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: 3 14 36 94 aNode:1 44 97 1 7

102 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

103 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

104 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

105 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

106 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

107 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

108 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

109 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

110 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

111 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

112 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

113 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

114 Queuestions?

115 Traversals of Arrays

116 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

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

118 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

119 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

120 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

121 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

122 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

123 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

124 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 NumArray 12 43 11 9 98

125 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 NumArray 12 43 11 9 98

126 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 NumArray 12 43 11 9 98

127 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 NumArray 12 43 11 9 98

128 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 NumArray 12 43 11 9 98

129 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 NumArray 12 43 11 9 98

130 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 NumArray 12 43 11 9 98

131 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 NumArray 12 43 11 9 98

132 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 NumArray 12 43 11 9 98

133 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 NumArray 12 43 11 9 98

134 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 NumArray 12 43 11 9 98

135 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 NumArray 12 43 11 9 98

136 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 NumArray 12 43 11 9 98

137 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 NumArray 12 43 11 9 98

138 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 NumArray 12 43 11 9 98

139 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 NumArray 12 43 11 9 98

140 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

141 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

142 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

143 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

144 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

145 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

146 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 i

147 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 i 1

148 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 i 1

149 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 i

150 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 i 1

151 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 i 2

152 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 i 2

153 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 i 2

154 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 i 2

155 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 i 2

156 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 i 3

157 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 i 3

158 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 i 3

159 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 i 3

160 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 i 3

161 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 i 4

162 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 i 4

163 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 i 4

164 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 i 5

165 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 i 6

166 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 i 7

167 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 i 8

168 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 i 9

169 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 i 9

170 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 i 10

171 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 i 10

172 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 i 10

173 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 i 10

174 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 10

175 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 11

176 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 11

177 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 11

178 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 11

179 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 11

180 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’

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

182 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

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

184 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

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

186 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 1 2 3 4 i 2 4 6 8 3 6 9 12 j

187 MAX_COLS is 4 Row_Type definesa Array[1..MAX_COLS] of Num Sample isoftype Row_type Sample:

188 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

189 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

190 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:

191 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:

192 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:

193 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:

194 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

195 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

196 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

197 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

198 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

199 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

200 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

201 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

202 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

203 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

204 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

205 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

206 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

207 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

208 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

209 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

210 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

211 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

212 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

213 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

214 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

215 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

216 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

217 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

218 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

219 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

220 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

221 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

222 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

223 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

224 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

225 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

226 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

227 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

228 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

229 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

230 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

231 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

232 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

233 Questions?

234


Download ppt "Lecture 14 Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array Traversals."

Similar presentations


Ads by Google