0% found this document useful (0 votes)
7 views55 pages

PreOrder Traversal

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views55 pages

PreOrder Traversal

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PRE-ORDER

TRAVERSAL
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
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
L PreOrderPrint(left child) 22
R PreOrderPrint(right child)

9 67

3 14 36 94

1 7 44 97
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
L PreOrderPrint(left child) 22
R PreOrderPrint(right child)

Output: 9 67

3 14 36 94

1 7 44 97
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child)
R PreOrderPrint(right child)

Output: 22 9 67

3 14 36 94

1 7 44 97
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child)
L
R PreOrderPrint(right child)

Output: 22 9 67

3 14 36 94

1 7 44 97
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 P 9 67

3 14 36 94

1 7 44 97
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child)
L
R PreOrderPrint(right child)

Output: 22 9 P 9 67
L

3 14 36 94

1 7 44 97
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child)
L
R PreOrderPrint(right child)

Output: 22 9 3 P 9 67
L

P 3 14 36 94

1 7 44 97
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child)
L
R PreOrderPrint(right child)

Output: 22 9 3 P 9 67
L

P 3 14 36 94
L

1 7 44 97
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child)
L
R PreOrderPrint(right child)

Output: 22 9 3 1 P 9 67
L

P 3 14 36 94
L

P 1 7 44 97
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 3 1 P 9 67
L

P 3 14 36 94
L

P 1 7 44 97
L
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 3 1 P 9 67
L

P 3 14 36 94
L

P 1 7 44 97
L
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 3 1 P 9 67
L

P 3 14 36 94
L

P 1 7 44 97
L
R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 3 1 P 9 67
L

P 3 14 36 94
L

P 1 7 44 97
L
R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 3 1 P 9 67
L

P 3 14 36 94
L
R

P 1 7 44 97
L
R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 3 1 7 P 9 67
L

P 3 14 36 94
L
R

P 1 P 7 44 97
L
R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 3 1 7 P 9 67
L

P 3 14 36 94
L
R

P 1 P 7 44 97
L L
R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 3 1 7 P 9 67
L

P 3 14 36 94
L
R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 3 1 7 P 9 67
L

P 3 14 36 94
L
R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 3 1 7 P 9 67
L

P 3 14 36 94
L
R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 3 1 7 P 9 67
L
R

P 3 14 36 94
L
R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child) L
R PreOrderPrint(right child)

Output: 22 9 3 1 7 14 P 9 67
L
R

P 3 P 14 36 94
L
R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child)
L
R PreOrderPrint(right child)

Output: 22 9 3 1 7 14 P 9 67
L
R

P 3 P 14 36 94
L L
R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child)
L
R PreOrderPrint(right child)

Output: 22 9 3 1 7 14 P 9 67
L
R

P 3 P 14 36 94
L L
R R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child)
L
R PreOrderPrint(right child)

Output: 22 9 3 1 7 14 P 9 67
L
R

P 3 P 14 36 94
L L
R R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child)
L
R PreOrderPrint(right child)

Output: 22 9 3 1 7 14 P 9 67
L
R

P 3 P 14 36 94
L L
R R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data)
P 22
L PreOrderPrint(left child)
L
R PreOrderPrint(right child)

Output: 22 9 3 1 7 14 P 9 67
L
R

P 3 P 14 36 94
L L
R R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 P 9 67
L
R

P 3 P 14 36 94
L L
R R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
L
R

P 3 P 14 36 94
L L
R R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
L L
R

P 3 P 14 36 94
L L
R R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 L L
R

P 3 P 14 P 36 94
L L
R R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 L L
R

P 3 P 14 P 36 94
L L L
R R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 L L
R

P 3 P 14 P 36 94
L L L
R R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 L L
R

P 3 P 14 P 36 94
L L L
R R R

P 1 P 7 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 44 L L
R

P 3 P 14 P 36 94
L L L
R R R

P 1 P 7 P 44 97
L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 44 L L
R

P 3 P 14 P 36 94
L L L
R R R

P 1 P 7 P 44 97
L L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 44 L L
R

P 3 P 14 P 36 94
L L L
R R R

P 1 P 7 P 44 97
L L L
R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 44 L L
R

P 3 P 14 P 36 94
L L L
R R R

P 1 P 7 P 44 97
L L L
R R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 44 L L
R

P 3 P 14 P 36 94
L L L
R R R

P 1 P 7 P 44 97
L L L
R R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 44 L L
R

P 3 P 14 P 36 94
L L L
R R R

P 1 P 7 P 44 97
L L L
R R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 44 L L
R R

P 3 P 14 P 36 94
L L L
R R R

P 1 P 7 P 44 97
L L L
R R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 44 94 L L
R R

P 3 P 14 P 36 P 94
L L L
R R R

P 1 P 7 P 44 97
L L L
R R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 44 94 L L
R R

P 3 P 14 P 36 P 94
L L L L
R R R

P 1 P 7 P 44 97
L L L
R R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 44 94 L L
R R

P 3 P 14 P 36 P 94
L L L L
R R R

P 1 P 7 P 44 97
L L L
R R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

Output: 22 9 3 1 7 14 67 P 9 P 67
36 44 94 L L
R R

P 3 P 14 P 36 P 94
L L L L
R R R R

P 1 P 7 P 44 97
L L L
R R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

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

P 3 P 14 P 36 P 94
L L L L
R R R R

P P 7 P 44 P 97
1
L L L
R R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

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

P 3 P 14 P 36 P 94
L L L L
R R R R

P P 7 P 44 P 97
1
L L L
L
R R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

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

P 3 P 14 P 36 P 94
L L L L
R R R R

P P 7 P 44 P 97
1
L L L
L
R R R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

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

P 3 P 14 P 36 P 94
L L L L
R R R R

P P 7 P 44 P 97
1
L L L
L
R R R
R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

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

P 3 P 14 P 36 P 94
L L L L
R R R R

P P 7 P 44 P 97
1
L L L
L
R R R
R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

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

P 3 P 14 P 36 P 94
L L L L
R R R R

P P 7 P 44 P 97
1
L L L
L
R R R
R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

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

P 3 P 14 P 36 P 94
L L L L
R R R R

P P 7 P 44 P 97
1
L L L
L
R R R
R
Proc PreOrderPrint(pointer)
root
pointer NOT NIL?
P print(data) P
L PreOrderPrint(left child) 22
L
R PreOrderPrint(right child) R

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

P 3 P 14 P 36 P 94
L L L L
R R R R

P P 7 P 44 P 97
1
L L L
L
R R R
R

You might also like