Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#81 from gupta2140/master
Browse files Browse the repository at this point in the history
Added feature for negative number input
  • Loading branch information
harshildarji authored Jun 8, 2017
2 parents ef9082b + 4fe88f2 commit 65de84b
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions traverals/binary_tree_traversals.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@ def __init__(self, data):


def build_tree():
print("\n********Press N to stop entering at any point of time********\n")
print("Enter the value of the root node: ", end="")
data = eval(input())
if data < 0:
return None
else:
q = queue.Queue()
tree_node = TreeNode(data)
q.put(tree_node)
while not q.empty():
node_found = q.get()
print("Enter the left node of %s: " % node_found.data, end="")
left_data = eval(input())
if left_data < 0:
return tree_node
elif left_data >= 0:
left_node = TreeNode(left_data)
node_found.left = left_node
q.put(left_node)
print("Enter the right node of %s: " % node_found.data, end="")
right_data = eval(input())
if right_data < 0:
return tree_node
elif right_data >= 0:
right_node = TreeNode(right_data)
node_found.right = right_node
q.put(right_node)
check=input()
if check=='N' or check=='n':
return None
data=int(check)
q = queue.Queue()
tree_node = TreeNode(data)
q.put(tree_node)
while not q.empty():
node_found = q.get()
print("Enter the left node of %s: " % node_found.data, end="")
check=input()
if check=='N' or check =='n':
return tree_node
left_data = int(check)
left_node = TreeNode(left_data)
node_found.left = left_node
q.put(left_node)
print("Enter the right node of %s: " % node_found.data, end="")
check = input()
if check == 'N' or check == 'n':
return tree_node
right_data = int(check)
right_node = TreeNode(right_data)
node_found.right = right_node
q.put(right_node)


def pre_order(node):
if not isinstance(node, TreeNode) or not node:
#print("Invalid input")
return
print(node.data, end=" ")
pre_order(node.left)
Expand Down

0 comments on commit 65de84b

Please sign in to comment.