Skip to content

Commit c5fbb3e

Browse files
committed
Fixed infinite loop while entering, and preorder traversal.
1 parent e268bf1 commit c5fbb3e

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

traverals/binary_tree_traversals.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
This is pure python implementation of tree traversal algorithms
33
"""
4-
4+
from __future__ import print_function
55
import queue
66

77

@@ -25,22 +25,25 @@ def build_tree():
2525
node_found = q.get()
2626
print("Enter the left node of %s: " % node_found.data, end="")
2727
left_data = eval(input())
28-
if left_data >= 0:
28+
if left_data < 0:
29+
return tree_node
30+
elif left_data >= 0:
2931
left_node = TreeNode(left_data)
3032
node_found.left = left_node
3133
q.put(left_node)
3234
print("Enter the right node of %s: " % node_found.data, end="")
3335
right_data = eval(input())
34-
if right_data >= 0:
36+
if right_data < 0:
37+
return tree_node
38+
elif right_data >= 0:
3539
right_node = TreeNode(right_data)
3640
node_found.right = right_node
3741
q.put(right_node)
38-
return tree_node
3942

4043

4144
def pre_order(node):
4245
if not isinstance(node, TreeNode) or not node:
43-
print("Invalid input")
46+
#print("Invalid input")
4447
return
4548
print(node.data, end=" ")
4649
pre_order(node.left)

0 commit comments

Comments
 (0)