PreOrder Traversal
PreOrder Traversal
TRAVERSAL
Outline of Pre-Order Traversal
• Three principle steps:
• Do work (Current)
• Traverse Left
• Traverse Right
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