Skip to content

Fixed infinite loop while entering, and preorder traversal. #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions traverals/binary_tree_traversals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
This is pure python implementation of tree traversal algorithms
"""

from __future__ import print_function
import queue


Expand All @@ -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)
Expand Down