|
| 1 | +""" |
| 2 | +This is pure python implementation of tree traversal algorithms |
| 3 | +""" |
| 4 | + |
| 5 | +importqueue |
| 6 | + |
| 7 | + |
| 8 | +classTreeNode: |
| 9 | + |
| 10 | +def__init__(self, data): |
| 11 | +self.data=data |
| 12 | +self.right=None |
| 13 | +self.left=None |
| 14 | + |
| 15 | + |
| 16 | +defbuild_tree(): |
| 17 | +print("Enter the value of the root node: ", end="") |
| 18 | +data=eval(input()) |
| 19 | +ifdata<0: |
| 20 | +returnNone |
| 21 | +else: |
| 22 | +q=queue.Queue() |
| 23 | +tree_node=TreeNode(data) |
| 24 | +q.put(tree_node) |
| 25 | +whilenotq.empty(): |
| 26 | +node_found=q.get() |
| 27 | +print("Enter the left node of %s: "%node_found.data, end="") |
| 28 | +left_data=eval(input()) |
| 29 | +ifleft_data>=0: |
| 30 | +left_node=TreeNode(left_data) |
| 31 | +node_found.left=left_node |
| 32 | +q.put(left_node) |
| 33 | +print("Enter the right node of %s: "%node_found.data, end="") |
| 34 | +right_data=eval(input()) |
| 35 | +ifright_data>=0: |
| 36 | +right_node=TreeNode(right_data) |
| 37 | +node_found.right=right_node |
| 38 | +q.put(right_node) |
| 39 | +returntree_node |
| 40 | + |
| 41 | + |
| 42 | +defpre_order(node): |
| 43 | +ifnotnode: |
| 44 | +return |
| 45 | +print(node.data, end=" ") |
| 46 | +pre_order(node.left) |
| 47 | +pre_order(node.right) |
| 48 | + |
| 49 | + |
| 50 | +defin_order(node): |
| 51 | +ifnotnode: |
| 52 | +return |
| 53 | +pre_order(node.left) |
| 54 | +print(node.data, end=" ") |
| 55 | +pre_order(node.right) |
| 56 | + |
| 57 | + |
| 58 | +defpost_order(node): |
| 59 | +ifnotnode: |
| 60 | +return |
| 61 | +post_order(node.left) |
| 62 | +post_order(node.right) |
| 63 | +print(node.data, end=" ") |
| 64 | + |
| 65 | + |
| 66 | +deflevel_order(node): |
| 67 | +ifnotnode: |
| 68 | +return |
| 69 | +q=queue.Queue() |
| 70 | +q.put(node) |
| 71 | +whilenotq.empty(): |
| 72 | +node_dequeued=q.get() |
| 73 | +print(node_dequeued.data, end=" ") |
| 74 | +ifnode_dequeued.left: |
| 75 | +q.put(node_dequeued.left) |
| 76 | +ifnode_dequeued.right: |
| 77 | +q.put(node_dequeued.right) |
| 78 | + |
| 79 | + |
| 80 | +if__name__=='__main__': |
| 81 | +importsys |
| 82 | +print("\n********* Binary Tree Traversals ************\n") |
| 83 | +# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin |
| 84 | +# otherwise 2.x's input builtin function is too "smart" |
| 85 | +ifsys.version_info.major<3: |
| 86 | +input_function=raw_input |
| 87 | +else: |
| 88 | +input_function=input |
| 89 | + |
| 90 | +node=build_tree() |
| 91 | +print("\n********* Pre Order Traversal ************") |
| 92 | +pre_order(node) |
| 93 | +print("\n******************************************\n") |
| 94 | + |
| 95 | +print("\n********* In Order Traversal ************") |
| 96 | +in_order(node) |
| 97 | +print("\n******************************************\n") |
| 98 | + |
| 99 | +print("\n********* Post Order Traversal ************") |
| 100 | +post_order(node) |
| 101 | +print("\n******************************************\n") |
| 102 | + |
| 103 | +print("\n********* Level Order Traversal ************") |
| 104 | +level_order(node) |
| 105 | +print("\n******************************************\n") |
0 commit comments