Skip to content

Commit 6f28333

Browse files
crackCodeLognpoyea
authored andcommitted
Adding function for actual level order traversal (TheAlgorithms#699)
1 parent 3014930 commit 6f28333

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

traversals/binary_tree_traversals.py

+39-9
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
This is pure python implementation of tree traversal algorithms
33
"""
44
from __future__ import print_function
5+
56
import queue
67

78
try:
8-
raw_input # Python 2
9+
raw_input # Python 2
910
except NameError:
1011
raw_input = input # Python 3
1112

@@ -22,7 +23,7 @@ def build_tree():
2223
print("Enter the value of the root node: ", end="")
2324
check = raw_input().strip().lower()
2425
if check == 'n':
25-
return None
26+
return None
2627
data = int(check)
2728
q = queue.Queue()
2829
tree_node = TreeNode(data)
@@ -46,13 +47,15 @@ def build_tree():
4647
node_found.right = right_node
4748
q.put(right_node)
4849

50+
4951
def pre_order(node):
5052
if not isinstance(node, TreeNode) or not node:
5153
return
5254
print(node.data, end=" ")
5355
pre_order(node.left)
5456
pre_order(node.right)
5557

58+
5659
def in_order(node):
5760
if not isinstance(node, TreeNode) or not node:
5861
return
@@ -82,22 +85,43 @@ def level_order(node):
8285
if node_dequeued.right:
8386
q.put(node_dequeued.right)
8487

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
86109
def pre_order_iter(node):
87110
if not isinstance(node, TreeNode) or not node:
88111
return
89112
stack = []
90113
n = node
91114
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
93116
print(n.data, end=" ")
94117
stack.append(n)
95118
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
97120
n = stack.pop()
98-
#start to traverse its right child
121+
# start to traverse its right child
99122
n = n.right
100123

124+
101125
def in_order_iter(node):
102126
if not isinstance(node, TreeNode) or not node:
103127
return
@@ -111,22 +135,24 @@ def in_order_iter(node):
111135
print(n.data, end=" ")
112136
n = n.right
113137

138+
114139
def post_order_iter(node):
115140
if not isinstance(node, TreeNode) or not node:
116141
return
117142
stack1, stack2 = [], []
118143
n = node
119144
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
121146
n = stack1.pop()
122147
if n.left:
123148
stack1.append(n.left)
124149
if n.right:
125150
stack1.append(n.right)
126151
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
128153
print(stack2.pop().data, end=" ")
129154

155+
130156
if __name__ == '__main__':
131157
print("\n********* Binary Tree Traversals ************\n")
132158

@@ -147,6 +173,10 @@ def post_order_iter(node):
147173
level_order(node)
148174
print("\n******************************************\n")
149175

176+
print("\n********* Actual Level Order Traversal ************")
177+
level_order_actual(node)
178+
print("\n******************************************\n")
179+
150180
print("\n********* Pre Order Traversal - Iteration Version ************")
151181
pre_order_iter(node)
152182
print("\n******************************************\n")
@@ -157,4 +187,4 @@ def post_order_iter(node):
157187

158188
print("\n********* Post Order Traversal - Iteration Version ************")
159189
post_order_iter(node)
160-
print("\n******************************************\n")
190+
print("\n******************************************\n")

0 commit comments

Comments
 (0)