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

Slides:



Advertisements
Similar presentations
Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree.
Advertisements

Linked List: Traversal Insertion Deletion. Linked List Traversal LB.
Advanced Data Structures
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Announcements NP-Complete Problems Decidable vs. Undecidable Problems Review.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
David Stotts Computer Science Department UNC Chapel Hill.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.
Announcements Review problems Review Outline. Online Survey
Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.
Review Records Dynamic Memory and Pointers Introduction to Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
Review Problems. What is the Big O? i
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Graphs Arrays Iteration Combining Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
Reading data into sorted list Want to suck text file in and produce sorted list of the contents: Option 1 : read directly into array based list, sort afterwards.
Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
CSE 373 Data Structures Lecture 7
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Chapter 12 – Data Structures
COMP 53 – Week Fourteen Trees.
Sorting.
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Trees.
Tree.
Trees Tree nomenclature Implementation strategies Traversals
Lecture 22 Binary Search Trees Chapter 10 of textbook
Cse 373 April 14th – TreEs pt 2.
Week 11 - Friday CS221.
Data Structure Interview
ITEC 2620M Introduction to Data Structures
Simple Dynamic Data (Linked Lists)
i206: Lecture 13: Recursion, continued Trees
Chapter 20: Binary Trees.
Data Structures and Database Applications Binary Trees in C#
TREES General trees Binary trees Binary search trees AVL trees
Chapter 21: Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Chapter 12: Binary Search Trees
Binary Tree Traversals
CS6045: Advanced Algorithms
Lecture 16 Bubble Sort Merge Sort.
Announcements Prelim 1 on Tuesday! A4 will be posted today
CSE 1002 Fundamentals of Software Development 2 More Linked Structures
CSC 143 Java Trees.
Chapter 20: Binary Trees.
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Binary Trees.
Binary Tree Implementation And Applications
Binary Search Trees CS 580U Fall 17.
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
8.2 Tree Traversals Chapter 8 - Trees.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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

Traversals of Linked Lists

The Scenario We need to visit each element in the linked list. At each element, we do some work. We’ll stop when we reach the end of the list. Examples: Printing all elements Updating/changing the data of all elements

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

Visiting Every Element head 48 17 142 // Regardless of whether we use iteration or recursion, we continue until we reach the end (NIL).

Don’t De-reference NIL We must make sure we don’t de-reference (follow) the NIL pointer. To do this, always test to see if the pointer is NIL. If so, we can’t use the ‘^’ operator head // Can’t follow head! (i.e. head^ doesn’t work!)

Testing for NIL Always test for NIL at the beginning of your loop. Your exitif statement must appear at the top Always test for NIL at the beginning of your recursive module. Your terminating condition must appear at the top of your module

Iterative Traversal Template procedure Traverse (cur iot in Ptr toa Node) // Purpose: call Do_Something on each element // Precondition: none // Postcondition: Do_Something on each element loop exitif( cur = NIL ) Do_Something( cur^.data ) cur <- cur^.next endloop endprocedure // Traverse

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List Called via Print_List(list_head) cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 42 cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 42 cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 42 cur list_head 4 17 42

An Iterative Traversal Example procedure Print_List(cur iot in Ptr toa Node) loop exitif (cur = NIL) print(cur^.data) cur <- cur^.next endloop endprocedure //Print_List 4 17 42 cur list_head 4 17 42

Recursive Traversal Template procedure Traverse (cur iot in Ptr toa Node) // Purpose: call Do_Something on each element // Precondition: none // Postcondition: Do_Something on each element if (cur <> NIL) then Do_Something( cur^.data ) Traverse( cur^.next ) endif endprocedure // Traverse

A Recursive Traversal Example procedure Print_List(cur iot in Ptr toa Node) if (cur <> NIL) then print(cur^.data) Print_List(cur^.next) endif endprocedure //Print_List Called via Print_List(list_head) cur list_head 4 17 42 You already know how to do this...

A Loophole in Parameter Protection If a pointer is an in parameter, the pointer cannot be changed. But, anything to which the pointer points may be changed. Thus, giving access to a list means giving ability to make changes to it; you cannot protect against this.

An Example procedure DestroyList (cur iot in Ptr toa Node) if (cur <> nil) then cur^.data <- -1 DestroyList(cur^.next) endif endprocedure // DestroyList head -1 -1 48 17 -1 142 //

Another Example // head 48 48 17 142 procedure DestroyList (cur iot in Ptr toa Node) cur^.next <- NIL endprocedure // DestroyList head 48 48 17 142 //

Summary Traversals involve visiting every element Recursion or iteration Stop when you reach NIL Make sure to not de-reference NIL Be aware that passing a pointer to a module via an IN parameter allows the module to modify the entire list.

Questions?

Traversing a Binary Search Tree (BST) Pre-Order

Outline of Pre-Order Traversal Three principle steps: Do work (Current) Traverse Left Traverse Right Work can be anything Separate work from traversal

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

Pre-Order Traversal Procedure procedure Pre_Order(cur iot in Ptr toa Tree_Node) // Purpose: perform pre-order traversal, call // DoWhatever for each node // Preconditions: cur points to a binary tree // Postcondition: DoWhatever will be performed // on each tree node in “pre-order” order if( cur <> NIL ) then Do_Whatever( cur^.data ) Pre_Order( cur^.left_child ) Pre_Order( cur^.right_child ) endif endprocedure // Pre_Order

22 9 67 3 14 36 94 44 97 1 7 Proc PreOrderPrint(pointer) pointer NOT NIL? print(data) PreOrderPrint(left child) PreOrderPrint(right child) root P 22 L R 9 67 3 14 36 94 44 97 1 7

Questions?

Traversing a Binary Search Tree (BST) Post-Order

Outline of Post-Order Traversal Three principle steps: Traverse Left Traverse Right Do work (Current) Work can be anything Separate work from traversal

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

Post-Order Traversal Procedure procedure Post_Order(cur iot in Ptr toa Tree_Node) // Purpose: perform post-order traversal, call // DoWhatever for each node // Preconditions: cur points to a binary tree // Postcondition: DoWhatever will be performed // on each tree node in “post-order” order if( cur <> NIL ) then Post_Order( cur^.left_child ) Post_Order( cur^.right_child ) Do_Whatever( cur^.data ) endif endprocedure // Post_Order

22 9 67 3 14 36 94 44 97 1 7 Proc PostOrderPrint(pointer) pointer NOT NIL? PostOrderPrint(left child) PostOrderPrint(right child) print(data) root L 22 R P 9 67 3 14 36 94 44 97 1 7

Questions?

Miscellaneous BST Traversals

Miscellaneous Traversals Defined Traversals In-order L C R Pre-order C L R Post-order L R C Other Possibilities: R C L C R L R L C

22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P 22 R L 9 67 3 14 36 94 44 97 1 7

22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P 22 R L 9 67 3 14 36 94 44 97 1 7

22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R L 9 67 Output: 22 3 14 36 94 44 97 1 7

22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L Output: 22 9 67 3 14 36 94 44 97 1 7

22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L P Output: 22 67 9 67 3 14 36 94 44 97 1 7

22 67 9 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L P 67 Output: 22 67 9 R 3 14 36 94 44 97 1 7

22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L P 9 67 Output: 22 67 94 R 3 14 P 36 94 44 97 1 7

22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L P Output: 22 67 94 9 67 R 3 14 P 36 94 R 44 97 1 7

22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L P Output: 22 67 94 97 9 67 R 3 14 P 36 94 R P 44 97 1 7

22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L P Output: 22 67 94 97 9 67 R 3 14 P 36 94 R P 44 97 1 7 R

22 9 67 3 14 36 94 44 97 1 7 Proc MiscTraverse(pointer) pointer NOT NIL? print(data) MiscTraverse(right child) MiscTraverse(left child) root P P 22 R R L L P P Output: 22 67 94 97 36 44 9 14 3 7 1 9 67 R R L L P 3 P 14 P P 36 94 R R R R L L L L P P P P 44 97 1 7 R R R R L L L L

Questions?

Depth First vs. Breadth First Traversal

Depth vs. Breadth First Traversals Depth First Traversals Go down (deep) In-, Pre-, Post-order Breadth First Traversals Go across (shallow) Require a queue to help

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

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

Depth First Traversal (In-Order) Procedure DFT (current isoftype in Ptr toa Node) // In order DFT if(current <> NIL) then DFT(current^.left) print(current^.data) DFT(Current^.right) endif endprocedure

Depth First Traversal (Pre-Order) Procedure DFT (current isoftype in Ptr toa Node) // In order DFT if(current <> NIL) then DFT(current^.left) print(current^.data) DFT(Current^.right) endif endprocedure Move this here to make a Preorder DFT

Depth First Traversal (Post-Order) Procedure DFT (current isoftype in Ptr toa Node) // In order DFT if(current <> NIL) then DFT(current^.left) print(current^.data) DFT(Current^.right) endif endprocedure Move this here to make a Postorder DFT

We have already seen these! Proc DFT(pointer) pointer NOT NIL? DFT(left child) print(data) DFT(right child) root 22 9 67 We have already seen these! 3 14 36 94 44 97 1 7

Breadth-First Traversal Requires a queue to maintain which nodes to visit next. Enqueue the root pointer Loop until no elements in the queue Dequeue(current) Enqueue current’s left and right children Do work at current

Breadth-First Traversal LB Breadth-First Traversal Procedure BFT(root isoftype in Ptr toa Node) Q isoftype Queue Initialize(Q) temp isoftype Ptr toa Node OK isoftype Boolean enqueue(Q, root) // continued

Breadth-First Traversal LB Breadth-First Traversal loop dequeue(temp, OK, Q) exitif(NOT OK) if(temp^.left <> NIL) then enqueue(temp^.left, Q)) endif if(temp^.right <> NIL) then enqueue(temp^.right, Q) print(temp^.data) // processing node endloop endprocedure

22 9 67 Q: 3 14 36 94 aNode: 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(data) endloop root 22 9 67 Q: 3 14 36 94 aNode: 44 97 1 7

22 9 67 Q:22 3 14 36 94 aNode: 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(data) endloop root 22 9 67 Q:22 3 14 36 94 aNode: 44 97 1 7

22 9 67 Q:22 3 14 36 94 aNode: 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(data) endloop root 22 9 67 Q:22 3 14 36 94 aNode: 44 97 1 7

22 9 67 Q: 3 14 36 94 aNode:22 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q: 3 14 36 94 aNode:22 44 97 1 7

22 9 67 Q:9 67 3 14 36 94 aNode:22 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:9 67 3 14 36 94 aNode:22 44 97 1 7

22 9 67 Q:9 67 3 14 36 94 aNode:22 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:9 67 3 14 36 94 aNode:22 44 97 1 7

22 9 67 Q:9 67 3 14 36 94 aNode:22 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:9 67 3 14 36 94 aNode:22 44 97 1 7

22 9 67 Q:67 3 14 36 94 aNode:9 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:67 3 14 36 94 aNode:9 44 97 1 7

22 9 67 Q:67 3 14 3 14 36 94 aNode:9 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:67 3 14 3 14 36 94 aNode:9 44 97 1 7

22 9 67 Q:67 3 14 3 14 36 94 aNode:9 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:67 3 14 3 14 36 94 aNode:9 44 97 1 7

22 9 67 Q:67 3 14 3 14 36 94 aNode:9 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:67 3 14 3 14 36 94 aNode:9 44 97 1 7

22 9 67 Q:3 14 3 14 36 94 aNode:67 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:3 14 3 14 36 94 aNode:67 44 97 1 7

22 9 67 Q:3 14 36 94 3 14 36 94 aNode:67 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:3 14 36 94 3 14 36 94 aNode:67 44 97 1 7

22 9 67 Q:3 14 36 94 3 14 36 94 aNode:67 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:3 14 36 94 3 14 36 94 aNode:67 44 97 1 7

22 9 67 Q:3 14 36 94 3 14 36 94 aNode:67 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:3 14 36 94 3 14 36 94 aNode:67 44 97 1 7

22 9 67 Q:14 36 94 3 14 36 94 aNode:3 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:14 36 94 3 14 36 94 aNode:3 44 97 1 7

22 9 67 Q:14 36 94 1 7 3 14 36 94 aNode:3 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:14 36 94 1 7 3 14 36 94 aNode:3 44 97 1 7

22 9 67 Q:14 36 94 1 7 3 14 36 94 aNode:3 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:14 36 94 1 7 3 14 36 94 aNode:3 44 97 1 7

22 9 67 Q:14 36 94 1 7 3 14 36 94 aNode:3 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:14 36 94 1 7 3 14 36 94 aNode:3 44 97 1 7

22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7

22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7

22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7

22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:36 94 1 7 3 14 36 94 aNode:14 44 97 1 7

22 9 67 Q:94 1 7 3 14 36 94 aNode:36 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:94 1 7 3 14 36 94 aNode:36 44 97 1 7

22 9 67 Q:94 1 7 44 3 14 36 94 aNode:36 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:94 1 7 44 3 14 36 94 aNode:36 44 97 1 7

22 9 67 Q:94 1 7 44 3 14 36 94 aNode:36 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:94 1 7 44 3 14 36 94 aNode:36 44 97 1 7

22 9 67 Q:94 1 7 44 3 14 36 94 aNode:36 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:94 1 7 44 3 14 36 94 aNode:36 44 97 1 7

22 9 67 Q:1 7 44 3 14 36 94 aNode:94 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:1 7 44 3 14 36 94 aNode:94 44 97 1 7

22 9 67 Q:1 7 44 97 3 14 36 94 aNode:94 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:1 7 44 97 3 14 36 94 aNode:94 44 97 1 7

22 9 67 Q:1 7 44 97 3 14 36 94 aNode:94 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:1 7 44 97 3 14 36 94 aNode:94 44 97 1 7

22 9 67 Q:1 7 44 97 3 14 36 94 aNode:94 44 97 1 7 Enqueue(root) loop exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:1 7 44 97 3 14 36 94 aNode:94 44 97 1 7

22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7

22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7

22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7

22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:7 44 97 3 14 36 94 aNode:1 44 97 1 7

22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7

22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7

22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7

22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:44 97 3 14 36 94 aNode:7 44 97 1 7

22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7

22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7

22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7

22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q:97 3 14 36 94 aNode:44 44 97 1 7

22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7

22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7

22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7

22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7 Enqueue(root) loop root exitif Q empty dequeue(aNode) enqueue(children) print(aNode.data) endloop root 22 9 67 Q: 3 14 36 94 aNode:97 44 97 1 7

Queuestions?

Traversals of Arrays

The Scenario We need to visit each element in the array. At each element, we do some work. We’ll stop when we reach the end of the array (when we reach MAX). Examples: Printing all elements Updating/changing the data of all elements

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

Recursive Array Traversal Template procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Purpose: call Do_Something on each element // Precondition: i = 1 on first call // Postcondition: Do_Something on each element if (i <= MAX) then Do_Something(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure

Recursive Array Traversal Example procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 12 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 12 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 i = 2 12 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 i = 2 12 43 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 i = 2 12 43 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 i = 1 12 43 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 i = 1 12 43 11 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 i = 1 12 43 11 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 i = 1 12 43 11 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 i = 1 12 43 11 9 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 i = 1 12 43 11 9 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 5 i = 1 12 43 11 9 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 5 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 5 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5

i = 2 i = 3 i = 4 i = 5 i = 6 i = 1 12 43 11 9 98 NumArray 12 43 11 9 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 5 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 6 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 5 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 4 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 3 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 2 i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5

procedure RecursiveTraverse (i iot in num, NumArray iot in NumArrayType) // Pre: i = 1 on first call if (i <= MAX) then print(NumArray[i]) RecursiveTraverse (i+1, NumArray) endif endprocedure i = 1 12 43 11 9 98 NumArray 12 43 11 9 98 1 2 3 4 5

Iterative Traversal Template procedure Traverse (my_array iot in/out ArrayType) // Purpose: call Do_Something on each element // Precondition: none // Postcondition: Do_Something on each element i isoftype Num i <- 1 loop exitif( i > MAX ) Do_Something( my_array[i] ) i <- i + 1 endloop endprocedure // Traverse

An Iterative Traversal Example Traverse the array and fill in every element so that the ith element contains the value i. 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

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

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

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 i

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 i 1

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 i 1

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 i

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 1 2 3 4 5 6 7 8 9 10 i 1

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 1 2 3 4 5 6 7 8 9 10 i 2

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 1 2 3 4 5 6 7 8 9 10 i 2

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 1 2 3 4 5 6 7 8 9 10 i 2

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 1 2 3 4 5 6 7 8 9 10 i 2

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 1 2 3 4 5 6 7 8 9 10 i 2

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 1 2 3 4 5 6 7 8 9 10 i 3

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 1 2 3 4 5 6 7 8 9 10 i 3

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 1 2 3 4 5 6 7 8 9 10 i 3

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 1 2 3 4 5 6 7 8 9 10 i 3

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 1 2 3 4 5 6 7 8 9 10 i 3

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 1 2 3 4 5 6 7 8 9 10 i 4

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 1 2 3 4 5 6 7 8 9 10 i 4

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 1 2 3 4 5 6 7 8 9 10 i 4

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 i 5

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10 i 6

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 10 i 7

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 10 i 8

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 10 i 9

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 i 9

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 i 10

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 i 10

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 i 10

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 i 10

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 i 10

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 i 11

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 i 11

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 i 11

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 i 11

MAX is 10 Vec_max definesa Array[1..MAX] of Num data isoftype Vec_max i isoftype Num i <- 1 loop exitif(i > MAX) data[i] <- i i <- i + 1 endloop data 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 i 11

A More Complex Problem Write a module which returns an array with the following characteristics: Every odd element has the first half of the alphabet in ascending order & every even element has the second half of the alphabet in descending order. A[1] = ‘A’ A[2] = ‘Z’ A[3] = ‘B’ A[4] = ‘Y’ A[5] = ‘C’ A[6] = ‘X’ . A[23] = ‘L’ A[24] = ‘O’ A[25] = ‘M’ A[26] = ‘N’

A A 1 2 3 4 5 . . . 23 24 25 26 Z B Y C L O M N Every odd element has the first half of the alphabet in ascending order & every even element has the second half of the alphabet in descending order.

A First Try ALPHAS is 26 A isoftype CharArrayType i isoftype Num up isoftype char i <- 1 up <- ‘A’ loop exitif( i > ALPHAS ) A[i] <- up up <- next_character(up) i <- i + 2 endloop

A 1 2 3 4 5 . . . 23 24 25 26 A B C L M Every odd element has the first half of the alphabet in ascending order & every even element has the second half of the alphabet in descending order.

ALPHAS is 26 A isoftype Array_Type i isoftype Num up, down isoftype char i <- 1 up <- ‘A’ down <- ‘Z’ loop exitif( i > ALPHAS ) A[i] <- up A[i + 1] <- down up <- next_character(up) down <- previous_character(down) i <- i + 2 endloop

A A 1 2 3 4 5 . . . 23 24 25 26 Z B Y C L O M N Every odd element has the first half of the alphabet in ascending order & every even element has the second half of the alphabet in descending order.

A 2-D Iterative Traversal Example We have a 2-D array We want to traverse the array and set each element to the product of it’s row position and column position (i.e. the element at position [i,j] should contain the value i*j) 1 2 3 4 1 2 3 1 2 3 4 i 2 4 6 8 3 6 9 12 j

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

1 Data: 2 3 1 2 3 4 MAX_ROWS is 3 MAX_COLS is 4 Row_Type definesa Array[1..MAX_COLS] of Num Matrix definesa Array[1..MAX_ROWS] of Row_Type Data isoftype Matrix Data: 1 2 3 1 2 3 4

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

1 Data: 2 3 Row: Col: 1 2 3 4 MAX_ROWS is 3 MAX_COLS is 4 Row_Type definesa Array[1..MAX_COLS] of Num Matrix definesa Array[1..MAX_ROWS] of Row_Type Data isoftype Matrix Row, Col isoftype Num Data: 1 2 3 Row: Col: 1 2 3 4

Data: 1 2 3 Row: 1 Col: 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 Data: 1 2 3 Row: 1 Col: 1 2 3 4

Data: 1 2 3 Row: 1 Col: 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 Data: 1 2 3 Row: 1 Col: 1 2 3 4

Data: 1 2 3 Row: 1 Col: 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 Data: 1 2 3 Row: 1 Col: 1 2 3 4

Data: 1 2 3 Row: 1 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 Data: 1 2 3 Row: 1 Col: 1 1 2 3 4

Data: 1 2 3 Row: 1 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 Data: 1 2 3 Row: 1 Col: 1 1 2 3 4

Data: 1 2 3 Row: 1 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 Data: 1 2 3 Row: 1 Col: 1 1 2 3 4

1 Data: 1 2 3 Row: 1 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 1 Data: 1 2 3 Row: 1 Col: 1 1 2 3 4

1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4

1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4

1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4

2 1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 2 1 Data: 1 2 3 Row: 1 Col: 2 1 2 3 4

2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4

2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4

2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4

3 2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 3 2 1 Data: 1 2 3 Row: 1 Col: 3 1 2 3 4

3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4

3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4

3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4

4 3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 1 Col: 4 1 2 3 4

4 3 2 1 Data: 1 2 3 Row: 1 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 1 Col: 5 1 2 3 4

4 3 2 1 Data: 1 2 3 Row: 1 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 1 Col: 5 1 2 3 4

4 3 2 1 Data: 1 2 3 Row: 1 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 1 Col: 5 1 2 3 4

4 3 2 1 Data: 1 2 3 Row: 2 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 5 1 2 3 4

4 3 2 1 Data: 1 2 3 Row: 2 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 5 1 2 3 4

4 3 2 1 Data: 1 2 3 Row: 2 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 5 1 2 3 4

4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 What would happen if we did this outside first loop? 4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4

4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4

4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4

4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 1 1 2 3 4

4 3 2 1 Data: 1 2 3 Row: 2 Col: 2 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 Data: 1 2 3 Row: 2 Col: 2 1 2 3 4

4 3 2 1 6 Data: 1 2 3 Row: 2 Col: 3 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 6 Data: 1 2 3 Row: 2 Col: 3 1 2 3 4

4 3 2 1 8 6 Data: 1 2 3 Row: 2 Col: 4 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 Data: 1 2 3 Row: 2 Col: 4 1 2 3 4

4 3 2 1 8 6 Data: 1 2 3 Row: 3 Col: 1 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 Data: 1 2 3 Row: 3 Col: 1 1 2 3 4

4 3 2 1 8 6 Data: 1 2 3 Row: 3 Col: 2 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 Data: 1 2 3 Row: 3 Col: 2 1 2 3 4

4 3 2 1 8 6 9 Data: 1 2 3 Row: 3 Col: 3 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 9 Data: 1 2 3 Row: 3 Col: 3 1 2 3 4

4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 3 Col: 4 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 3 Col: 4 1 2 3 4

4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 3 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 3 Col: 5 1 2 3 4

4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 3 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 3 Col: 5 1 2 3 4

4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 4 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 4 Col: 5 1 2 3 4

4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 4 Col: 5 1 2 3 4 Row <- 1 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 4 Col: 5 1 2 3 4

and continue… 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 4 Col: 5 1 2 3 4 loop exitif(Row > MAX_ROWS) Col <- 1 exitif(Col > MAX_COLS) Data[Row][Col] <- Row * Col Col <- Col + 1 endloop Row <- Row + 1 MAX_ROWS is 3 MAX_COLS is 4 and continue… 4 3 2 1 8 6 12 9 Data: 1 2 3 Row: 4 Col: 5 1 2 3 4

Summary Traversals involve visiting every element. Recursion or iteration Do some work at each element When doing iterative traversals on arrays Use a loop for each dimension of the array Embed loops Typically perform work in innermost loop

Questions?