Title: Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array Traversals
1Traversals of Linked ListsPreorder BST
TraversalPostorder BST TraversalMiscellaneous
BST TraversalsDepth First vs Breadth FirstArray
Traversals
Lecture 14
2Traversals of Linked Lists
3The Scenario
- We need to visit each element in the linked list.
- At each element, we do some work.
- Well stop when we reach the end of the list.
- Examples
- Printing all elements
- Updating/changing the data of all elements
4Traversals
- A traversal visits every element in a collection.
- Two forms
- Recursive
- Iterative (linear structures)
5Visiting Every Element
//
head
48
17
142
- Regardless of whether we use iteration or
recursion, we continue until we reach the end
(NIL).
6Dont De-reference NIL
- We must make sure we dont de-reference (follow)
the NIL pointer. - To do this, always test to see if the pointer is
NIL. - If so, we cant use the operator
head
//
Cant follow head! (i.e. head doesnt work!)
7Testing 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
8Iterative 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 lt- cur.next
- endloop
- endprocedure // Traverse
9An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
Called via Print_List(list_head)
cur
list_head
17
42
4
10An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
cur
list_head
17
42
4
11An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
4
cur
list_head
17
42
4
12An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
4
cur
list_head
17
42
4
13An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
4
cur
list_head
17
42
4
14An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
4
cur
list_head
17
42
4
15An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
4 17
cur
list_head
17
42
4
16An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
4 17
cur
list_head
17
42
4
17An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
4 17
cur
list_head
17
42
4
18An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
4 17
cur
list_head
17
42
4
19An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
4 17 42
cur
list_head
17
42
4
20An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
4 17 42
cur
list_head
17
42
4
21An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
4 17 42
cur
list_head
17
42
4
22An Iterative Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- loop
- exitif (cur NIL)
- print(cur.data)
- cur lt- cur.next
- endloop
- endprocedure //Print_List
4 17 42
cur
list_head
17
42
4
23Recursive 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 ltgt NIL) then
- Do_Something( cur.data )
- Traverse( cur.next )
- endif
- endprocedure // Traverse
24A Recursive Traversal Example
- procedure Print_List(cur iot in Ptr toa Node)
- if (cur ltgt NIL) then
- print(cur.data)
- Print_List(cur.next)
- endif
- endprocedure //Print_List
Called via Print_List(list_head)
cur
list_head
17
42
4
You already know how to do this...
25A 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.
26An Example
- procedure DestroyList (cur iot in Ptr toa Node)
- if (cur ltgt nil) then
- cur.data lt- -1
- DestroyList(cur.next)
- endif
- endprocedure // DestroyList
//
head
48
17
142
27Another Example
- procedure DestroyList (cur iot in Ptr toa Node)
- cur.next lt- NIL
- endprocedure // DestroyList
//
head
48
17
142
28Summary
- 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.
29Questions?
30Traversing a Binary Search Tree (BST)Pre-Order
31Outline of Pre-Order Traversal
- Three principle steps
- Do work (Current)
- Traverse Left
- Traverse Right
- Work can be anything
- Separate work from traversal
32- Traverse the tree Pre-order
- Visit the current and do work
- Visit the trees left sub-tree
- Visit right sub-tree
33Pre-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 ltgt NIL ) then
- Do_Whatever( cur.data )
- Pre_Order( cur.left_child )
- Pre_Order( cur.right_child )
- endif
- endprocedure // Pre_Order
34Proc PreOrderPrint(pointer) pointer NOT NIL?
print(data) PreOrderPrint(left child)
PreOrderPrint(right child)
root
P
L
R
35Questions?
36Traversing a Binary Search Tree (BST)Post-Order
37Outline of Post-Order Traversal
- Three principle steps
- Traverse Left
- Traverse Right
- Do work (Current)
- Work can be anything
- Separate work from traversal
38- Traverse the tree Post order
- Visit the trees left sub-tree
- Visit right sub-tree
- Visit the current and do work
39Post-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 ltgt NIL ) then
- Post_Order( cur.left_child )
- Post_Order( cur.right_child )
- Do_Whatever( cur.data )
- endif
- endprocedure // Post_Order
40Proc PostOrderPrint(pointer) pointer NOT NIL?
PostOrderPrint(left child) PostOrderPrint(right
child) print(data)
root
L
R
P
41Questions?
42Miscellaneous BST Traversals
43Miscellaneous 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
44Proc MiscTraverse(pointer) pointer NOT NIL?
print(data) MiscTraverse(right child)
MiscTraverse(left child)
root
P
R
L
45Proc MiscTraverse(pointer) pointer NOT NIL?
print(data) MiscTraverse(right child)
MiscTraverse(left child)
root
P
R
L
46Proc MiscTraverse(pointer) pointer NOT NIL?
print(data) MiscTraverse(right child)
MiscTraverse(left child)
root
P
P
R
L
Output 22
47Proc MiscTraverse(pointer) pointer NOT NIL?
print(data) MiscTraverse(right child)
MiscTraverse(left child)
root
P
P
R
R
L
Output 22
48Proc MiscTraverse(pointer) pointer NOT NIL?
print(data) MiscTraverse(right child)
MiscTraverse(left child)
root
P
P
R
R
L
P
Output 22 67
49Proc MiscTraverse(pointer) pointer NOT NIL?
print(data) MiscTraverse(right child)
MiscTraverse(left child)
root
P
P
R
R
L
P
Output 22 67
R
50Proc MiscTraverse(pointer) pointer NOT NIL?
print(data) MiscTraverse(right child)
MiscTraverse(left child)
root
P
P
R
R
L
P
Output 22 67 94
R
P
51Proc MiscTraverse(pointer) pointer NOT NIL?
print(data) MiscTraverse(right child)
MiscTraverse(left child)
root
P
P
R
R
L
P
Output 22 67 94
R
P
R
52Proc MiscTraverse(pointer) pointer NOT NIL?
print(data) MiscTraverse(right child)
MiscTraverse(left child)
root
P
P
R
R
L
P
Output 22 67 94 97
R
P
R
P
53Proc MiscTraverse(pointer) pointer NOT NIL?
print(data) MiscTraverse(right child)
MiscTraverse(left child)
root
P
P
R
R
L
P
Output 22 67 94 97
R
P
R
P
R
54Proc MiscTraverse(pointer) pointer NOT NIL?
print(data) MiscTraverse(right child)
MiscTraverse(left child)
root
P
P
R
R
L
L
P
P
Output 22 67 94 97 36 44 9 14 3 7 1
R
R
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
55Questions?
56Depth First vs. Breadth First Traversal
57Depth 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
58Depth-First Traversal
root
59Breadth-First Traversal
root
60Depth First Traversal (In-Order)
- Procedure DFT
- (current isoftype in Ptr toa Node)
- // In order DFT
- if(current ltgt NIL) then
- DFT(current.left)
- print(current.data)
- DFT(Current.right)
- endif
- endprocedure
61Depth First Traversal (Pre-Order)
- Procedure DFT
- (current isoftype in Ptr toa Node)
- // In order DFT
- if(current ltgt NIL) then
- DFT(current.left)
- print(current.data)
- DFT(Current.right)
- endif
- endprocedure
Move this here to make a Preorder DFT
62Depth First Traversal (Post-Order)
- Procedure DFT
- (current isoftype in Ptr toa Node)
- // In order DFT
- if(current ltgt NIL) then
- DFT(current.left)
- print(current.data)
- DFT(Current.right)
- endif
- endprocedure
Move this here to make a Postorder DFT
63Proc DFT(pointer) pointer NOT NIL? DFT(left
child) print(data) DFT(right child)
root
We have already seen these!
64Breadth-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 currents left and right children
- Do work at current
65Breadth-First Traversal
LB
- 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
66Breadth-First Traversal
LB
- loop
- dequeue(temp, OK, Q)
- exitif(NOT OK)
- if(temp.left ltgt NIL) then
- enqueue(temp.left, Q))
- endif
- if(temp.right ltgt NIL) then
- enqueue(temp.right, Q)
- endif
- print(temp.data) // processing node
- endloop
- endprocedure
67- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(data)
- endloop
root
Q
aNode
68- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(data)
- endloop
root
Q22
aNode
69- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(data)
- endloop
root
Q22
aNode
70- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q
aNode22
71- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q9 67
aNode22
72- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q9 67
aNode22
73- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q9 67
aNode22
74- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q67
aNode9
75- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q67 3 14
aNode9
76- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q67 3 14
aNode9
77- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q67 3 14
aNode9
78- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q3 14
aNode67
79- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q3 14 36 94
aNode67
80- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q3 14 36 94
aNode67
81- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q3 14 36 94
aNode67
82- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q14 36 94
aNode3
83- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q14 36 94 1 7
aNode3
84- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q14 36 94 1 7
aNode3
85- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q14 36 94 1 7
aNode3
86- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q36 94 1 7
aNode14
87- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q36 94 1 7
aNode14
88- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q36 94 1 7
aNode14
89- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q36 94 1 7
aNode14
90- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q94 1 7
aNode36
91- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q94 1 7 44
aNode36
92- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q94 1 7 44
aNode36
93- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q94 1 7 44
aNode36
94- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q1 7 44
aNode94
95- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q1 7 44 97
aNode94
96- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q1 7 44 97
aNode94
97- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q1 7 44 97
aNode94
98- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q7 44 97
aNode1
99- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q7 44 97
aNode1
100- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q7 44 97
aNode1
101- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q7 44 97
aNode1
102- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q44 97
aNode7
103- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q44 97
aNode7
104- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q44 97
aNode7
105- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q44 97
aNode7
106- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q97
aNode44
107- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q97
aNode44
108- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q97
aNode44
109- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q97
aNode44
110- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q
aNode97
111- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q
aNode97
112- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q
aNode97
113- Enqueue(root)
- loop
- exitif Q empty
- dequeue(aNode)
- enqueue(children)
- print(aNode.data)
- endloop
root
Q
aNode97
114Queuestions?
115Traversals of Arrays
116The Scenario
- We need to visit each element in the array.
- At each element, we do some work.
- Well stop when we reach the end of the array
(when we reach MAX). - Examples
- Printing all elements
- Updating/changing the data of all elements
117Traversals
- A traversal visits every element in a collection.
- Two forms
- Recursive
- Iterative (linear structures)
118Recursive 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 lt MAX) then
- Do_Something(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
119Recursive Array Traversal Example
- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
120- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
NumArray
12
43
11
9
98
1 2 3 4 5
121- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12
12
43
11
9
98
NumArray
1 2 3 4 5
122- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12
12
43
11
9
98
NumArray
1 2 3 4 5
123- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) // Pre i 1 on
first call if (i lt MAX) then
print(NumArrayi) RecursiveTraverse (i1,
NumArray) endif endprocedure
i 1
i 2
12
12
43
11
9
98
NumArray
1 2 3 4 5
124- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) // Pre i 1 on
first call if (i lt MAX) then
print(NumArrayi) RecursiveTraverse (i1,
NumArray) endif endprocedure
i 1
i 2
12 43
12
43
11
9
98
NumArray
1 2 3 4 5
125- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) // Pre i 1 on
first call if (i lt MAX) then
print(NumArrayi) RecursiveTraverse (i1,
NumArray) endif endprocedure
i 1
i 2
12 43
12
43
11
9
98
NumArray
1 2 3 4 5
126- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43
12
43
11
9
98
NumArray
1 2 3 4 5
127- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11
12
43
11
9
98
NumArray
1 2 3 4 5
128- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11
12
43
11
9
98
NumArray
1 2 3 4 5
129- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11
12
43
11
9
98
NumArray
1 2 3 4 5
130- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11 9
12
43
11
9
98
NumArray
1 2 3 4 5
131- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11 9
12
43
11
9
98
NumArray
1 2 3 4 5
132- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11 9
12
43
11
9
98
NumArray
1 2 3 4 5
133- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11 9 98
12
43
11
9
98
NumArray
1 2 3 4 5
134- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11 9 98
12
43
11
9
98
NumArray
1 2 3 4 5
135- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11 9 98
12
43
11
9
98
NumArray
1 2 3 4 5
136- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11 9 98
12
43
11
9
98
NumArray
1 2 3 4 5
137- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11 9 98
12
43
11
9
98
NumArray
1 2 3 4 5
138- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11 9 98
12
43
11
9
98
NumArray
1 2 3 4 5
139- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11 9 98
12
43
11
9
98
NumArray
1 2 3 4 5
140- procedure RecursiveTraverse (i iot in num,
NumArray iot in NumArrayType) - // Pre i 1 on first call
- if (i lt MAX) then
- print(NumArrayi)
- RecursiveTraverse (i1, NumArray)
- endif
- endprocedure
i 1
12 43 11 9 98
12
43
11
9
98
NumArray
1 2 3 4 5
141Iterative 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 lt- 1
- loop
- exitif( i gt MAX )
- Do_Something( my_arrayi )
- i lt- i 1
- endloop
- endprocedure // Traverse
142An Iterative Traversal Example
Traverse the array and fill in every elementso
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
143MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
144MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
145MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
146MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
i
147MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
1
i
148MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
1
i
149MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
1
i
150MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
1
i
151MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
2
i
152MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
2
i
153MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
2
i
154MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
2
i
155MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
2
i
156MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
3
i
157MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
3
i
158MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
3
i
159MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
3
i
160MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
3
i
161MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
4
i
162MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
4
i
163MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
4
i
164MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
5
i
165MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
6
i
166MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
7
i
167MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
8
i
168MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
9
i
169MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
9
i
170MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
10
i
171MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
10
i
172MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
10
i
173MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
10
i
174MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
10
i
175MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
11
i
176MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
11
i
177MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
11
i
178MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
11
i
179MAX is 10 Vec_max definesa Array1..MAX of
Num data isoftype Vec_max i isoftype Num i lt-
1 loop exitif(i gt MAX) datai lt- i i lt- i
1 endloop
data
1 2 3 4 5 6 7 8 9
10
11
i
180A 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.
A1 A A2 Z A3 B A4 Y A5
C A6 X . . . A23 L A24
O A25 M A26 N
181A
A
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.
182A First Try
- ALPHAS is 26
- A isoftype CharArrayType
- i isoftype Num
- up isoftype char
- i lt- 1
- up lt- A
- loop
- exitif( i gt ALPHAS )
- Ai lt- up
- up lt- next_character(up)
- i lt- i 2
- endloop
183A
A
B
C
L
M
Every odd element has the first half of the
alphabet in ascending order every even element
has the second half of the alphabet in descending
order.
184- ALPHAS is 26
- A isoftype Array_Type
- i isoftype Num
- up, down isoftype char
- i lt- 1
- up lt- A
- down lt- Z
- loop
- exitif( i gt ALPHAS )
- Ai lt- up
- Ai 1 lt- down
- up lt- next_character(up)
- down lt- previous_character(down)
- i lt- i 2
- endloop
185A
A
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.
186A 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 its row position and
column position - (i.e. the element at position i,j should
contain the value ij)
1
2
3
4
2
i
4
6
8
3
6
9
12
j
187MAX_COLS is 4 Row_Type definesa
Array1..MAX_COLS of Num Sample isoftype
Row_type
Sample
1 2 3 4
188MAX_ROWS is 3 MAX_COLS is 4 Row_Type definesa
Array1..MAX_COLS of Num Matrix definesa
Array1..MAX_ROWS of Row_Type Data isoftype
Matrix
1 2 3
1 2 3 4
189MAX_ROWS is 3 MAX_COLS is 4 Row_Type definesa
Array1..MAX_COLS of Num Matrix definesa
Array1..MAX_ROWS of Row_Type Data isoftype
Matrix Row, Col isoftype Num Row lt- 1 loop
exitif(Row gt MAX_ROWS) Col lt- 1 loop
exitif(Col gt MAX_COLS) DataRowCol lt- Row
Col Col lt- Col 1 endloop Row lt- Row
1 endloop
190MAX_ROWS is 3 MAX_COLS is 4 Row_Type definesa
Array1..MAX_COLS of Num Matrix definesa
Array1..MAX_ROWS of Row_Type Data isoftype
Matrix Row, Col isoftype Num
1 2 3
1 2 3 4
191Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
192Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
193Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
194Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
195Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
196Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
197Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
198Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
199Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
200Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
201Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
202Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
203Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
204Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
205Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
206Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col lt- Col 1
endloop Row lt- Row 1 endloop
MAX_ROWS is 3 MAX_COLS is 4
1 2 3
1 2 3 4
207Row lt- 1 loop exitif(Row gt MAX_ROWS) Col lt-
1 loop exitif(Col gt MAX_COLS)
DataRowCol lt- Row Col Col l