2
2
This is pure python implementation of tree traversal algorithms
3
3
"""
4
4
from __future__ import print_function
5
+
5
6
import queue
6
7
7
8
try :
8
- raw_input # Python 2
9
+ raw_input # Python 2
9
10
except NameError :
10
11
raw_input = input # Python 3
11
12
@@ -22,7 +23,7 @@ def build_tree():
22
23
print ("Enter the value of the root node: " , end = "" )
23
24
check = raw_input ().strip ().lower ()
24
25
if check == 'n' :
25
- return None
26
+ return None
26
27
data = int (check )
27
28
q = queue .Queue ()
28
29
tree_node = TreeNode (data )
@@ -46,13 +47,15 @@ def build_tree():
46
47
node_found .right = right_node
47
48
q .put (right_node )
48
49
50
+
49
51
def pre_order (node ):
50
52
if not isinstance (node , TreeNode ) or not node :
51
53
return
52
54
print (node .data , end = " " )
53
55
pre_order (node .left )
54
56
pre_order (node .right )
55
57
58
+
56
59
def in_order (node ):
57
60
if not isinstance (node , TreeNode ) or not node :
58
61
return
@@ -82,22 +85,43 @@ def level_order(node):
82
85
if node_dequeued .right :
83
86
q .put (node_dequeued .right )
84
87
85
- #iteration version
88
+
89
+ def level_order_actual (node ):
90
+ if not isinstance (node , TreeNode ) or not node :
91
+ return
92
+ q = queue .Queue ()
93
+ q .put (node )
94
+ while not q .empty ():
95
+ list = []
96
+ while not q .empty ():
97
+ node_dequeued = q .get ()
98
+ print (node_dequeued .data , end = " " )
99
+ if node_dequeued .left :
100
+ list .append (node_dequeued .left )
101
+ if node_dequeued .right :
102
+ list .append (node_dequeued .right )
103
+ print ()
104
+ for node in list :
105
+ q .put (node )
106
+
107
+
108
+ # iteration version
86
109
def pre_order_iter (node ):
87
110
if not isinstance (node , TreeNode ) or not node :
88
111
return
89
112
stack = []
90
113
n = node
91
114
while n or stack :
92
- while n : # start from root node, find its left child
115
+ while n : # start from root node, find its left child
93
116
print (n .data , end = " " )
94
117
stack .append (n )
95
118
n = n .left
96
- #end of while means current node doesn't have left child
119
+ # end of while means current node doesn't have left child
97
120
n = stack .pop ()
98
- #start to traverse its right child
121
+ # start to traverse its right child
99
122
n = n .right
100
123
124
+
101
125
def in_order_iter (node ):
102
126
if not isinstance (node , TreeNode ) or not node :
103
127
return
@@ -111,22 +135,24 @@ def in_order_iter(node):
111
135
print (n .data , end = " " )
112
136
n = n .right
113
137
138
+
114
139
def post_order_iter (node ):
115
140
if not isinstance (node , TreeNode ) or not node :
116
141
return
117
142
stack1 , stack2 = [], []
118
143
n = node
119
144
stack1 .append (n )
120
- while stack1 : # to find the reversed order of post order, store it in stack2
145
+ while stack1 : # to find the reversed order of post order, store it in stack2
121
146
n = stack1 .pop ()
122
147
if n .left :
123
148
stack1 .append (n .left )
124
149
if n .right :
125
150
stack1 .append (n .right )
126
151
stack2 .append (n )
127
- while stack2 : # pop up from stack2 will be the post order
152
+ while stack2 : # pop up from stack2 will be the post order
128
153
print (stack2 .pop ().data , end = " " )
129
154
155
+
130
156
if __name__ == '__main__' :
131
157
print ("\n ********* Binary Tree Traversals ************\n " )
132
158
@@ -147,6 +173,10 @@ def post_order_iter(node):
147
173
level_order (node )
148
174
print ("\n ******************************************\n " )
149
175
176
+ print ("\n ********* Actual Level Order Traversal ************" )
177
+ level_order_actual (node )
178
+ print ("\n ******************************************\n " )
179
+
150
180
print ("\n ********* Pre Order Traversal - Iteration Version ************" )
151
181
pre_order_iter (node )
152
182
print ("\n ******************************************\n " )
@@ -157,4 +187,4 @@ def post_order_iter(node):
157
187
158
188
print ("\n ********* Post Order Traversal - Iteration Version ************" )
159
189
post_order_iter (node )
160
- print ("\n ******************************************\n " )
190
+ print ("\n ******************************************\n " )
0 commit comments