From c5fbb3e7721fd9bcbde307f86fbed1b15da3d9e5 Mon Sep 17 00:00:00 2001 From: amankumargupta Date: Wed, 7 Jun 2017 15:50:53 +0530 Subject: [PATCH] Fixed infinite loop while entering, and preorder traversal. --- traverals/binary_tree_traversals.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/traverals/binary_tree_traversals.py b/traverals/binary_tree_traversals.py index 591142b27..50185a399 100644 --- a/traverals/binary_tree_traversals.py +++ b/traverals/binary_tree_traversals.py @@ -1,7 +1,7 @@ """ This is pure python implementation of tree traversal algorithms """ - +from __future__ import print_function import queue @@ -25,22 +25,25 @@ def build_tree(): node_found = q.get() print("Enter the left node of %s: " % node_found.data, end="") left_data = eval(input()) - if left_data >= 0: + 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: + 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) - return tree_node def pre_order(node): if not isinstance(node, TreeNode) or not node: - print("Invalid input") + #print("Invalid input") return print(node.data, end=" ") pre_order(node.left)