File tree
Expand file treeCollapse file tree1 file changed
+8
-5
lines changed Expand file treeCollapse file tree1 file changed
+8
-5
lines changed Original file line number | Diff line number | Diff line change |
---|
|
1 | 1 | """
|
2 | 2 | This is pure python implementation of tree traversal algorithms
|
3 | 3 | """
|
4 |
| - |
| 4 | +from __future__ import print_function |
5 | 5 | import queue
|
6 | 6 |
|
7 | 7 |
|
@@ -25,22 +25,25 @@ def build_tree():
|
25 | 25 | node_found = q.get()
|
26 | 26 | print("Enter the left node of %s: " % node_found.data, end="")
|
27 | 27 | left_data = eval(input())
|
28 |
| -if left_data >= 0: |
| 28 | +if left_data < 0: |
| 29 | +return tree_node |
| 30 | +elif left_data >= 0: |
29 | 31 | left_node = TreeNode(left_data)
|
30 | 32 | node_found.left = left_node
|
31 | 33 | q.put(left_node)
|
32 | 34 | print("Enter the right node of %s: " % node_found.data, end="")
|
33 | 35 | right_data = eval(input())
|
34 |
| -if right_data >= 0: |
| 36 | +if right_data < 0: |
| 37 | +return tree_node |
| 38 | +elif right_data >= 0: |
35 | 39 | right_node = TreeNode(right_data)
|
36 | 40 | node_found.right = right_node
|
37 | 41 | q.put(right_node)
|
38 |
| -return tree_node |
39 | 42 |
|
40 | 43 |
|
41 | 44 | def pre_order(node):
|
42 | 45 | if not isinstance(node, TreeNode) or not node:
|
43 |
| -print("Invalid input") |
| 46 | +#print("Invalid input") |
44 | 47 | return
|
45 | 48 | print(node.data, end=" ")
|
46 | 49 | pre_order(node.left)
|
|
You can’t perform that action at this time.
0 commit comments