234
Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array Traversals

Lecture 14

  • Upload
    chiku

  • View
    27

  • Download
    0

Embed Size (px)

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

Page 1: Lecture 14

Traversals of Linked ListsPreorder BST Traversal

Postorder BST TraversalMiscellaneous BST Traversals

Depth First vs Breadth FirstArray Traversals

Page 2: Lecture 14

Traversals of Linked Lists

Page 3: Lecture 14

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

Page 4: Lecture 14

Traversals

• A traversal visits every element in a collection.

• Two forms:– Recursive– Iterative (linear structures)

Page 5: Lecture 14

Visiting Every Element

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

48 17 142head //

Page 6: Lecture 14

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

Page 7: Lecture 14

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

Page 8: Lecture 14

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

Page 9: Lecture 14

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

Page 10: Lecture 14

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

Page 11: Lecture 14

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

Page 12: Lecture 14

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

Page 13: Lecture 14

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

Page 14: Lecture 14

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

Page 15: Lecture 14

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

Page 16: Lecture 14

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

Page 17: Lecture 14

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

Page 18: Lecture 14

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

Page 19: Lecture 14

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

Page 20: Lecture 14

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

Page 21: Lecture 14

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

Page 22: Lecture 14

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

Page 23: Lecture 14

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

Page 24: Lecture 14

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

Page 25: Lecture 14

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.

Page 26: Lecture 14

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

Page 27: Lecture 14

Another Example

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

48 17 142head //48

Page 28: Lecture 14

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.

Page 29: Lecture 14

Questions?

Page 30: Lecture 14

Traversing a Binary Search Tree (BST)Pre-Order

Page 31: Lecture 14

Outline of Pre-Order Traversal

• Three principle steps:– Do work (Current)– Traverse Left– Traverse Right

• Work can be anything• Separate work from traversal

Page 32: Lecture 14

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

Page 33: Lecture 14

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

Page 34: Lecture 14

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

22

root

67

363 14

447

94

971

9

PLR

Page 35: Lecture 14

Questions?

Page 36: Lecture 14

Traversing a Binary Search Tree (BST)Post-Order

Page 37: Lecture 14

Outline of Post-Order Traversal

• Three principle steps:– Traverse Left– Traverse Right– Do work (Current)

• Work can be anything• Separate work from traversal

Page 38: Lecture 14

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

Page 39: Lecture 14

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

Page 40: Lecture 14

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

22

root

67

363 14

447

94

971

9

LRP

Page 41: Lecture 14

Questions?

Page 42: Lecture 14

Miscellaneous BST Traversals

Page 43: Lecture 14

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

Page 44: Lecture 14

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

Page 45: Lecture 14

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

Page 46: Lecture 14

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

Page 47: Lecture 14

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

Page 48: Lecture 14

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

Page 49: Lecture 14

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

Page 50: Lecture 14

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

Page 51: Lecture 14

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

Page 52: Lecture 14

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

Page 53: Lecture 14

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

Page 54: Lecture 14

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

Page 55: Lecture 14

Questions?

Page 56: Lecture 14

Depth First vs. Breadth First Traversal

Page 57: Lecture 14

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

Page 58: Lecture 14

Depth-First Traversal

22

root

67

363 14

447

94

971

9

Page 59: Lecture 14

Breadth-First Traversal

22

root

67

363 14

447

94

971

9

Page 60: Lecture 14

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

Page 61: Lecture 14

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

Page 62: Lecture 14

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

Page 63: Lecture 14

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

22

root

67

363 14

447

94

971

9

Page 64: Lecture 14

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

Page 65: Lecture 14

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

Page 66: Lecture 14

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

Page 67: Lecture 14

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

22

root

67

363 14

447

94

971

9

Q:

aNode:

Page 68: Lecture 14

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:

Page 69: Lecture 14

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:

Page 70: Lecture 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:

aNode:22

Page 71: Lecture 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:9 67

aNode:22

Page 72: Lecture 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:9 67

aNode:22

Page 73: Lecture 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:9 67

aNode:22

Page 74: Lecture 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:67

aNode:9

Page 75: Lecture 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:67 3 14

aNode:9

Page 76: Lecture 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:67 3 14

aNode:9

Page 77: Lecture 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:67 3 14

aNode:9

Page 78: Lecture 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:3 14

aNode:67

Page 79: Lecture 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:3 14 36 94

aNode:67

Page 80: Lecture 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:3 14 36 94

aNode:67

Page 81: Lecture 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:3 14 36 94

aNode:67

Page 82: Lecture 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:14 36 94

aNode:3

Page 83: Lecture 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:14 36 94 1 7

aNode:3

Page 84: Lecture 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:14 36 94 1 7

aNode:3

Page 85: Lecture 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:14 36 94 1 7

aNode:3

Page 86: Lecture 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

Page 87: Lecture 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

Page 88: Lecture 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

Page 89: Lecture 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

Page 90: Lecture 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

Page 91: Lecture 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 44

aNode:36

Page 92: Lecture 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 44

aNode:36

Page 93: Lecture 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 44

aNode:36

Page 94: Lecture 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:1 7 44

aNode:94

Page 95: Lecture 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:1 7 44 97

aNode:94

Page 96: Lecture 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:1 7 44 97

aNode:94

Page 97: Lecture 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:1 7 44 97

aNode:94

Page 98: Lecture 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:7 44 97

aNode:1

Page 99: Lecture 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:7 44 97

aNode:1

Page 100: Lecture 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:7 44 97

aNode:1

Page 101: Lecture 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:7 44 97

aNode:1

Page 102: Lecture 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:44 97

aNode:7

Page 103: Lecture 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:44 97

aNode:7

Page 104: Lecture 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:44 97

aNode:7

Page 105: Lecture 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:44 97

aNode:7

Page 106: Lecture 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:97

aNode:44

Page 107: Lecture 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:97

aNode:44

Page 108: Lecture 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:97

aNode:44

Page 109: Lecture 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:97

aNode:44

Page 110: Lecture 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:

aNode:97

Page 111: Lecture 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:

aNode:97

Page 112: Lecture 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:

aNode:97

Page 113: Lecture 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:

aNode:97

Page 114: Lecture 14

Queuestions?

Page 115: Lecture 14

Traversals of Arrays

Page 116: Lecture 14

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

Page 117: Lecture 14

Traversals

• A traversal visits every element in a collection.

• Two forms:– Recursive– Iterative (linear structures)

Page 118: Lecture 14

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

Page 119: Lecture 14

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

Page 120: Lecture 14

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

Page 121: Lecture 14

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

Page 122: Lecture 14

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

Page 123: Lecture 14

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

Page 124: Lecture 14

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

Page 125: Lecture 14

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

Page 126: Lecture 14

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

Page 127: Lecture 14

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

Page 128: Lecture 14

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

Page 129: Lecture 14

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

Page 130: Lecture 14

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

Page 131: Lecture 14

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

Page 132: Lecture 14

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

Page 133: Lecture 14

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

Page 134: Lecture 14

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

Page 135: Lecture 14

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

Page 136: Lecture 14

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

Page 137: Lecture 14

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

Page 138: Lecture 14

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

Page 139: Lecture 14

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

Page 140: Lecture 14

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

Page 141: Lecture 14

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

Page 142: Lecture 14

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

Page 143: Lecture 14

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

Page 144: Lecture 14

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

Page 145: Lecture 14

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

Page 146: Lecture 14

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

Page 147: Lecture 14

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

Page 148: Lecture 14

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

Page 149: Lecture 14

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

Page 150: Lecture 14

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

Page 151: Lecture 14

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

Page 152: Lecture 14

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

Page 153: Lecture 14

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

Page 154: Lecture 14

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

Page 155: Lecture 14

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

Page 156: Lecture 14

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

Page 157: Lecture 14

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

Page 158: Lecture 14

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

Page 159: Lecture 14

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

Page 160: Lecture 14

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

Page 161: Lecture 14

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

Page 162: Lecture 14

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

Page 163: Lecture 14

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

Page 164: Lecture 14

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

Page 165: Lecture 14

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

Page 166: Lecture 14

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

Page 167: Lecture 14

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

Page 168: Lecture 14

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

Page 169: Lecture 14

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

Page 170: Lecture 14

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

Page 171: Lecture 14

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

Page 172: Lecture 14

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

Page 173: Lecture 14

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

Page 174: Lecture 14

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

Page 175: Lecture 14

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

Page 176: Lecture 14

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

Page 177: Lecture 14

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

Page 178: Lecture 14

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

Page 179: Lecture 14

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

Page 180: Lecture 14

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’

Page 181: Lecture 14

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

Page 182: Lecture 14

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

Page 183: Lecture 14

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

Page 184: Lecture 14

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

Page 185: Lecture 14

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

Page 186: Lecture 14

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

Page 187: Lecture 14

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

Sample isoftype Row_type

Sample:1 2 3 4

Page 188: Lecture 14

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

Page 189: Lecture 14

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

Page 190: Lecture 14

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

Page 191: Lecture 14

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

Page 192: Lecture 14

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

Page 193: Lecture 14

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

Page 194: Lecture 14

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

Page 195: Lecture 14

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

Page 196: Lecture 14

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

Page 197: Lecture 14

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

Page 198: Lecture 14

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

Page 199: Lecture 14

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

Page 200: Lecture 14

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

Page 201: Lecture 14

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

Page 202: Lecture 14

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

Page 203: Lecture 14

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

Page 204: Lecture 14

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

Page 205: Lecture 14

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

Page 206: Lecture 14

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

Page 207: Lecture 14

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

Page 208: Lecture 14

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

Page 209: Lecture 14

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

Page 210: Lecture 14

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

Page 211: Lecture 14

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

Page 212: Lecture 14

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

Page 213: Lecture 14

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

Page 214: Lecture 14

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

Page 215: Lecture 14

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

Page 216: Lecture 14

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?

Page 217: Lecture 14

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

Page 218: Lecture 14

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

Page 219: Lecture 14

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

Page 220: Lecture 14

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

Page 221: Lecture 14

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

Page 222: Lecture 14

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

Page 223: Lecture 14

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

Page 224: Lecture 14

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

Page 225: Lecture 14

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

Page 226: Lecture 14

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

Page 227: Lecture 14

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

Page 228: Lecture 14

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

Page 229: Lecture 14

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

Page 230: Lecture 14

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

Page 231: Lecture 14

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…

Page 232: Lecture 14

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

Page 233: Lecture 14

Questions?

Page 234: Lecture 14