Lecture 14

Preview:

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Traversals of Linked ListsPreorder BST Traversal

Postorder BST TraversalMiscellaneous BST Traversals

Depth First vs Breadth FirstArray 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

• Regardless of whether we use iteration or recursion, we continue until we reach the end (NIL).

48 17 142head //

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 endloopendprocedure // Traverse

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

Called via Print_List(list_head)

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

4

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

4

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

4

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

4

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

4 17

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

4 17

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

4 17

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

4 17

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

4 17 42

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

4 17 42

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

4 17 42

cur

An Iterative Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloopendprocedure //Print_List

4 17

list_head

42

4 17 42

cur

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 ) endifendprocedure // Traverse

A Recursive Traversal Exampleprocedure Print_List(cur iot in Ptr toa Node) if (cur <> NIL) then print(cur^.data) Print_List(cur^.next) endifendprocedure //Print_List

4 17

list_head

42

Called via Print_List(list_head)

cur

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) endifendprocedure // DestroyList

48 17 142head //-1 -1 -1

Another Example

procedure DestroyList (cur iot in Ptr toa Node) cur^.next <- NILendprocedure // DestroyList

48 17 142head //48

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

Proc PreOrderPrint(pointer) pointer NOT NIL? print(data) PreOrderPrint(left child) PreOrderPrint(right child)

22

root

67

363 14

447

94

971

9

PLR

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

Proc PostOrderPrint(pointer) pointer NOT NIL? PostOrderPrint(left child) PostOrderPrint(right child) print(data)

22

root

67

363 14

447

94

971

9

LRP

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

Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child)

22

root

67

363 14

447

94

971

9

P

LR

22

root

67

363 14

447

94

971

9

Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child)

P

LR

22

root

67

363 14

447

94

971

9

P

Output: 22

Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child)

P

LR

22

root

67

363 14

447

94

971

9

P

Output: 22

Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child)

P

LR R

22

root

67

363 14

447

94

971

9

P

Output: 22 67

Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child)

P

LR R

P

22

root

67

363 14

447

94

971

9

P

Output: 22 67

Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child)

P

LR R

PR

22

root

67

363 14

447

94

971

9

P

Output: 22 67 94

Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child)

P

LR R

PR

P

22

root

67

363 14

447

94

971

9

P

Output: 22 67 94

Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child)

P

LR R

PR

PR

22

root

67

363 14

447

94

971

9

P

Output: 22 67 94 97

Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child)

P

LR R

PR

PR

P

22

root

67

363 14

447

94

971

9

P

Output: 22 67 94 97

Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child)

P

LR R

PR

PR

PR

22

root

67

363 14

447

94

971

9

P

Output: 22 67 94 97 36 44 9 14 3 7 1

Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child)

P

LR R

PR

PR

PRL

L

L

PR

PRL

L

L

PR

PRL

L

PR

PRL

L

PRL

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

22

root

67

363 14

447

94

971

9

Breadth-First Traversal

22

root

67

363 14

447

94

971

9

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

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

Move this hereto make aPreorder 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) endifendprocedure

Move this hereto make aPostorder DFT

Proc DFT(pointer) pointer NOT NIL? DFT(left child) print(data) DFT(right child)

22

root

67

363 14

447

94

971

9

Breadth-First Traversal

Requires a queue to maintain which nodes to visit next.

Enqueue the root pointerLoop until no elements in the queue

Dequeue(current)Enqueue current’s left and right childrenDo work at current

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

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) endif print(temp^.data) // processing node endloopendprocedure

LB

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(data)endloop

22

root

67

363 14

447

94

971

9

Q:

aNode:

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(data)endloop

22

root

67

363 14

447

94

971

9

Q:22

aNode:

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(data)endloop

22

root

67

363 14

447

94

971

9

Q:22

aNode:

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:

aNode:22

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:9 67

aNode:22

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:9 67

aNode:22

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:9 67

aNode:22

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:67

aNode:9

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:67 3 14

aNode:9

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:67 3 14

aNode:9

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:67 3 14

aNode:9

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:3 14

aNode:67

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:3 14 36 94

aNode:67

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:3 14 36 94

aNode:67

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:3 14 36 94

aNode:67

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:14 36 94

aNode:3

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:14 36 94 1 7

aNode:3

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:14 36 94 1 7

aNode:3

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:14 36 94 1 7

aNode:3

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:36 94 1 7

aNode:14

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:36 94 1 7

aNode:14

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:36 94 1 7

aNode:14

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:36 94 1 7

aNode:14

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:94 1 7

aNode:36

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:94 1 7 44

aNode:36

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:94 1 7 44

aNode:36

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:94 1 7 44

aNode:36

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:1 7 44

aNode:94

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:1 7 44 97

aNode:94

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:1 7 44 97

aNode:94

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:1 7 44 97

aNode:94

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:7 44 97

aNode:1

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:7 44 97

aNode:1

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:7 44 97

aNode:1

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:7 44 97

aNode:1

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:44 97

aNode:7

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:44 97

aNode:7

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:44 97

aNode:7

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:44 97

aNode:7

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:97

aNode:44

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:97

aNode:44

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:97

aNode:44

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:97

aNode:44

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:

aNode:97

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:

aNode:97

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:

aNode:97

Enqueue(root)loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data)endloop

22

root

67

363 14

447

94

971

9

Q:

aNode:97

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

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43 11

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43 11

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43 11

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

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

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

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

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

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

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

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

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43 11 9 98

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

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

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43 11 9 98

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

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

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43 11 9 98

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

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

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

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

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

i = 6

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43 11 9 98

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

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

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43 11 9 98

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

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43 11 9 98

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

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43 11 9 98

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

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

i = 1

12 43 11 9 98NumArray

1 2 3 4 5

12 43 11 9 98

Iterative Traversal Templateprocedure 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 endloopendprocedure // Traverse

An Iterative Traversal ExampleTraverse the array and fill in every elementso that the ith element contains the value i.

1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

data1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

data

i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

data

1i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

data

1i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

data

1i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 data

1i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 data

2i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 data

2i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 data

2i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 data

2i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 data

2i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 data

3i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 data

3i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 data

3i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 data

3i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 data

3i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 data

4i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 data

4i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 data

4i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 data

5i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 data

6i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 data

7i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 data

8i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 data

9i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 9 data

9i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 9 data

10i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 9 data

10i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 9 data

10i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 9 data

10i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 9 10data

10i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 9 10data

11i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 9 10data

11i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 9 10data

11i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 9 10data

11i

1 2 3 4 5 6 7 8 9 10

MAX is 10Vec_max definesa Array[1..MAX] of Numdata isoftype Vec_maxi isoftype Numi <- 1loop exitif(i > MAX) data[i] <- i i <- i + 1endloop

1 2 3 4 5 6 7 8 9 10data

11i

1 2 3 4 5 6 7 8 9 10

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’

1 2 3 4 5. . .

23 24 25 26A 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

A First TryALPHAS is 26A isoftype CharArrayTypei isoftype Numup isoftype chari <- 1up <- ‘A’loopexitif( i > ALPHAS )A[i] <- upup <- next_character(up)i <- i + 2

endloop

1 2 3 4 5. . .

23 24 25 26A 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.

A

ALPHAS is 26A isoftype Array_Typei isoftype Numup, down isoftype chari <- 1up <- ‘A’down <- ‘Z’loopexitif( i > ALPHAS )A[i] <- upA[i + 1] <- downup <- next_character(up)down <- previous_character(down)i <- i + 2

endloop

1 2 3 4 5. . .

23 24 25 26A 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

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

123

i

j

1 2 3 42 4 6 83 6 9 12

MAX_COLS is 4Row_Type definesa Array[1..MAX_COLS] of Num

Sample isoftype Row_type

Sample:1 2 3 4

MAX_ROWS is 3MAX_COLS is 4Row_Type definesa Array[1..MAX_COLS] of NumMatrix definesa Array[1..MAX_ROWS] of Row_TypeData isoftype Matrix

Data:

1 2 3 4

1

2

3

MAX_ROWS is 3MAX_COLS is 4Row_Type definesa Array[1..MAX_COLS] of NumMatrix definesa Array[1..MAX_ROWS] of Row_TypeData isoftype MatrixRow, Col isoftype NumRow <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

MAX_ROWS is 3MAX_COLS is 4Row_Type definesa Array[1..MAX_COLS] of NumMatrix definesa Array[1..MAX_ROWS] of Row_TypeData isoftype MatrixRow, Col isoftype Num

Data:

Row: Col:

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

Data:

Row: 1Col:

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

Data:

Row: 1Col:

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

Data:

Row: 1Col:

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

Data:

Row: 1Col: 1

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

Data:

Row: 1Col: 1

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

Data:

Row: 1Col: 1

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

1

Data:

Row: 1Col: 1

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

1

Data:

Row: 1Col: 2

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

1

Data:

Row: 1Col: 2

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

1

Data:

Row: 1Col: 2

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

2 1

Data:

Row: 1Col: 2

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

2 1

Data:

Row: 1Col: 3

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

2 1

Data:

Row: 1Col: 3

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

2 1

Data:

Row: 1Col: 3

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

3 2 1

Data:

Row: 1Col: 3

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

3 2 1

Data:

Row: 1Col: 4

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

3 2 1

Data:

Row: 1Col: 4

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

3 2 1

Data:

Row: 1Col: 4

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

Data:

Row: 1Col: 4

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

Data:

Row: 1Col: 5

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

Data:

Row: 1Col: 5

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

Data:

Row: 1Col: 5

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

Data:

Row: 2Col: 5

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

Data:

Row: 2Col: 5

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

Data:

Row: 2Col: 5

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

Data:

Row: 2Col: 1

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

What would happenif we did this outside

first loop?

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

Data:

Row: 2Col: 1

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

Data:

Row: 2Col: 1

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

2

Data:

Row: 2Col: 1

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

4 2

Data:

Row: 2Col: 2

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

6 4 2

Data:

Row: 2Col: 3

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

8 6 4 2

Data:

Row: 2Col: 4

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

8 6 4 2

3

Data:

Row: 3Col: 1

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

8 6 4 2

6 3

Data:

Row: 3Col: 2

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

8 6 4 2

9 6 3

Data:

Row: 3Col: 3

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

8 6 4 2

12 9 6 3

Data:

Row: 3Col: 4

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

8 6 4 2

12 9 6 3

Data:

Row: 3Col: 5

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

8 6 4 2

12 9 6 3

Data:

Row: 3Col: 5

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

8 6 4 2

12 9 6 3

Data:

Row: 4Col: 5

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

8 6 4 2

12 9 6 3

Data:

Row: 4Col: 5

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

Row <- 1loop exitif(Row > MAX_ROWS) Col <- 1 loop exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1endloop

4 3 2 1

8 6 4 2

12 9 6 3

Data:

Row: 4Col: 5

MAX_ROWS is 3MAX_COLS is 4

1 2 3 4

1

2

3

and continue…

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?