2016-09-25 21:15:14 +00:00
|
|
|
"""
|
|
|
|
This is pure python implementation of tree traversal algorithms
|
|
|
|
"""
|
2017-06-07 10:20:53 +00:00
|
|
|
from __future__ import print_function
|
2019-02-16 16:16:43 +00:00
|
|
|
|
2016-09-25 21:15:14 +00:00
|
|
|
import queue
|
|
|
|
|
2017-11-25 09:23:50 +00:00
|
|
|
try:
|
2019-02-16 16:16:43 +00:00
|
|
|
raw_input # Python 2
|
2017-11-25 09:23:50 +00:00
|
|
|
except NameError:
|
|
|
|
raw_input = input # Python 3
|
|
|
|
|
2016-09-25 21:15:14 +00:00
|
|
|
|
|
|
|
class TreeNode:
|
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data
|
|
|
|
self.right = None
|
|
|
|
self.left = None
|
|
|
|
|
|
|
|
|
|
|
|
def build_tree():
|
2017-06-07 18:11:44 +00:00
|
|
|
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="")
|
2017-11-25 09:23:50 +00:00
|
|
|
check = raw_input().strip().lower()
|
|
|
|
if check == 'n':
|
2019-02-16 16:16:43 +00:00
|
|
|
return None
|
2017-11-25 09:23:50 +00:00
|
|
|
data = int(check)
|
2017-06-07 18:11:44 +00:00
|
|
|
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="")
|
2017-11-25 09:23:50 +00:00
|
|
|
check = raw_input().strip().lower()
|
|
|
|
if check == 'n':
|
2017-06-07 18:11:44 +00:00
|
|
|
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="")
|
2017-11-25 09:23:50 +00:00
|
|
|
check = raw_input().strip().lower()
|
|
|
|
if check == 'n':
|
2017-06-07 18:11:44 +00:00
|
|
|
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
|
|
|
|
2019-02-16 16:16:43 +00:00
|
|
|
|
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)
|
|
|
|
|
2019-02-16 16:16:43 +00:00
|
|
|
|
2016-09-25 21:15:14 +00:00
|
|
|
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)
|
|
|
|
|
2019-02-16 16:16:43 +00:00
|
|
|
|
|
|
|
def level_order_actual(node):
|
|
|
|
if not isinstance(node, TreeNode) or not node:
|
|
|
|
return
|
|
|
|
q = queue.Queue()
|
|
|
|
q.put(node)
|
|
|
|
while not q.empty():
|
|
|
|
list = []
|
|
|
|
while not q.empty():
|
|
|
|
node_dequeued = q.get()
|
|
|
|
print(node_dequeued.data, end=" ")
|
|
|
|
if node_dequeued.left:
|
|
|
|
list.append(node_dequeued.left)
|
|
|
|
if node_dequeued.right:
|
|
|
|
list.append(node_dequeued.right)
|
|
|
|
print()
|
|
|
|
for node in list:
|
|
|
|
q.put(node)
|
|
|
|
|
|
|
|
|
|
|
|
# iteration version
|
2019-02-09 01:54:07 +00:00
|
|
|
def pre_order_iter(node):
|
|
|
|
if not isinstance(node, TreeNode) or not node:
|
|
|
|
return
|
|
|
|
stack = []
|
|
|
|
n = node
|
|
|
|
while n or stack:
|
2019-02-16 16:16:43 +00:00
|
|
|
while n: # start from root node, find its left child
|
2019-02-09 01:54:07 +00:00
|
|
|
print(n.data, end=" ")
|
|
|
|
stack.append(n)
|
|
|
|
n = n.left
|
2019-02-16 16:16:43 +00:00
|
|
|
# end of while means current node doesn't have left child
|
2019-02-09 01:54:07 +00:00
|
|
|
n = stack.pop()
|
2019-02-16 16:16:43 +00:00
|
|
|
# start to traverse its right child
|
2019-02-09 01:54:07 +00:00
|
|
|
n = n.right
|
|
|
|
|
2019-02-16 16:16:43 +00:00
|
|
|
|
2019-02-09 01:54:07 +00:00
|
|
|
def in_order_iter(node):
|
|
|
|
if not isinstance(node, TreeNode) or not node:
|
|
|
|
return
|
|
|
|
stack = []
|
|
|
|
n = node
|
|
|
|
while n or stack:
|
|
|
|
while n:
|
|
|
|
stack.append(n)
|
|
|
|
n = n.left
|
|
|
|
n = stack.pop()
|
|
|
|
print(n.data, end=" ")
|
|
|
|
n = n.right
|
|
|
|
|
2019-02-16 16:16:43 +00:00
|
|
|
|
2019-02-09 01:54:07 +00:00
|
|
|
def post_order_iter(node):
|
|
|
|
if not isinstance(node, TreeNode) or not node:
|
|
|
|
return
|
|
|
|
stack1, stack2 = [], []
|
|
|
|
n = node
|
|
|
|
stack1.append(n)
|
2019-02-16 16:16:43 +00:00
|
|
|
while stack1: # to find the reversed order of post order, store it in stack2
|
2019-02-09 01:54:07 +00:00
|
|
|
n = stack1.pop()
|
|
|
|
if n.left:
|
|
|
|
stack1.append(n.left)
|
|
|
|
if n.right:
|
|
|
|
stack1.append(n.right)
|
|
|
|
stack2.append(n)
|
2019-02-16 16:16:43 +00:00
|
|
|
while stack2: # pop up from stack2 will be the post order
|
2019-02-09 01:54:07 +00:00
|
|
|
print(stack2.pop().data, end=" ")
|
2016-09-25 21:15:14 +00:00
|
|
|
|
2019-02-16 16:16:43 +00:00
|
|
|
|
2016-09-25 21:22:05 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
print("\n********* Binary Tree Traversals ************\n")
|
2016-09-25 21:15:14 +00:00
|
|
|
|
2016-09-25 21:22:05 +00:00
|
|
|
node = build_tree()
|
|
|
|
print("\n********* Pre Order Traversal ************")
|
|
|
|
pre_order(node)
|
|
|
|
print("\n******************************************\n")
|
2016-09-25 21:15:14 +00:00
|
|
|
|
2016-09-25 21:22:05 +00:00
|
|
|
print("\n********* In Order Traversal ************")
|
|
|
|
in_order(node)
|
|
|
|
print("\n******************************************\n")
|
2016-09-25 21:15:14 +00:00
|
|
|
|
2016-09-25 21:22:05 +00:00
|
|
|
print("\n********* Post Order Traversal ************")
|
|
|
|
post_order(node)
|
|
|
|
print("\n******************************************\n")
|
2016-09-25 21:15:14 +00:00
|
|
|
|
2016-09-25 21:22:05 +00:00
|
|
|
print("\n********* Level Order Traversal ************")
|
|
|
|
level_order(node)
|
|
|
|
print("\n******************************************\n")
|
2019-02-09 01:54:07 +00:00
|
|
|
|
2019-02-16 16:16:43 +00:00
|
|
|
print("\n********* Actual Level Order Traversal ************")
|
|
|
|
level_order_actual(node)
|
|
|
|
print("\n******************************************\n")
|
|
|
|
|
2019-02-09 01:54:07 +00:00
|
|
|
print("\n********* Pre Order Traversal - Iteration Version ************")
|
|
|
|
pre_order_iter(node)
|
|
|
|
print("\n******************************************\n")
|
|
|
|
|
|
|
|
print("\n********* In Order Traversal - Iteration Version ************")
|
|
|
|
in_order_iter(node)
|
|
|
|
print("\n******************************************\n")
|
|
|
|
|
|
|
|
print("\n********* Post Order Traversal - Iteration Version ************")
|
|
|
|
post_order_iter(node)
|
2019-02-16 16:16:43 +00:00
|
|
|
print("\n******************************************\n")
|