Python/traverals/binary_tree_traversals.py

110 lines
3.0 KiB
Python
Raw Normal View History

2016-09-25 21:15:14 +00:00
"""
This is pure python implementation of tree traversal algorithms
"""
from __future__ import print_function
2016-09-25 21:15:14 +00:00
import queue
class TreeNode:
def __init__(self, data):
self.data = data
self.right = None
self.left = None
def build_tree():
print("\n********Press N to stop entering at any point of time********\n")
2016-09-25 21:15:14 +00:00
print("Enter the value of the root node: ", end="")
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)
2016-09-25 21:15:14 +00:00
def pre_order(node):
2016-10-13 18:31:44 +00:00
if not isinstance(node, TreeNode) or not node:
2016-09-25 21:15:14 +00:00
return
print(node.data, end=" ")
pre_order(node.left)
pre_order(node.right)
def in_order(node):
2016-10-13 19:57:52 +00:00
if not isinstance(node, TreeNode) or not node:
2016-09-25 21:15:14 +00:00
return
2016-09-26 11:30:35 +00:00
in_order(node.left)
2016-09-25 21:15:14 +00:00
print(node.data, end=" ")
2016-09-26 11:30:35 +00:00
in_order(node.right)
2016-09-25 21:15:14 +00:00
def post_order(node):
2016-10-13 19:57:52 +00:00
if not isinstance(node, TreeNode) or not node:
2016-09-25 21:15:14 +00:00
return
post_order(node.left)
post_order(node.right)
print(node.data, end=" ")
def level_order(node):
2016-10-13 19:57:52 +00:00
if not isinstance(node, TreeNode) or not node:
2016-09-25 21:15:14 +00:00
return
q = queue.Queue()
q.put(node)
while not q.empty():
node_dequeued = q.get()
print(node_dequeued.data, end=" ")
if node_dequeued.left:
q.put(node_dequeued.left)
if node_dequeued.right:
q.put(node_dequeued.right)
if __name__ == '__main__':
import sys
2016-10-13 19:57:52 +00:00
print("\n********* Binary Tree Traversals ************\n")
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
# otherwise 2.x's input builtin function is too "smart"
if sys.version_info.major < 3:
input_function = raw_input
else:
input_function = input
2016-09-25 21:15:14 +00:00
node = build_tree()
print("\n********* Pre Order Traversal ************")
pre_order(node)
print("\n******************************************\n")
2016-09-25 21:15:14 +00:00
print("\n********* In Order Traversal ************")
in_order(node)
print("\n******************************************\n")
2016-09-25 21:15:14 +00:00
print("\n********* Post Order Traversal ************")
post_order(node)
print("\n******************************************\n")
2016-09-25 21:15:14 +00:00
print("\n********* Level Order Traversal ************")
level_order(node)
print("\n******************************************\n")