2020-05-22 06:10:11 +00:00
|
|
|
# flake8: noqa
|
|
|
|
|
2016-09-25 21:15:14 +00:00
|
|
|
"""
|
2020-03-04 12:40:28 +00:00
|
|
|
This is pure Python implementation of tree traversal algorithms
|
2016-09-25 21:15:14 +00:00
|
|
|
"""
|
2020-09-23 11:30:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-09-25 21:15:14 +00:00
|
|
|
import queue
|
2017-11-25 09:23:50 +00:00
|
|
|
|
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")
|
2019-08-12 15:59:59 +00:00
|
|
|
check = input("Enter the value of the root node: ").strip().lower() or "n"
|
|
|
|
if check == "n":
|
2019-02-16 16:16:43 +00:00
|
|
|
return None
|
2019-08-12 15:59:59 +00:00
|
|
|
q: queue.Queue = queue.Queue()
|
|
|
|
tree_node = TreeNode(int(check))
|
2017-06-07 18:11:44 +00:00
|
|
|
q.put(tree_node)
|
|
|
|
while not q.empty():
|
|
|
|
node_found = q.get()
|
2022-07-07 14:34:07 +00:00
|
|
|
msg = f"Enter the left node of {node_found.data}: "
|
2019-08-12 15:59:59 +00:00
|
|
|
check = input(msg).strip().lower() or "n"
|
|
|
|
if check == "n":
|
2017-06-07 18:11:44 +00:00
|
|
|
return tree_node
|
2019-08-12 15:59:59 +00:00
|
|
|
left_node = TreeNode(int(check))
|
2017-06-07 18:11:44 +00:00
|
|
|
node_found.left = left_node
|
|
|
|
q.put(left_node)
|
2022-07-07 14:34:07 +00:00
|
|
|
msg = f"Enter the right node of {node_found.data}: "
|
2019-08-12 15:59:59 +00:00
|
|
|
check = input(msg).strip().lower() or "n"
|
|
|
|
if check == "n":
|
2017-06-07 18:11:44 +00:00
|
|
|
return tree_node
|
2019-08-12 15:59:59 +00:00
|
|
|
right_node = TreeNode(int(check))
|
2017-06-07 18:11:44 +00:00
|
|
|
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
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
def pre_order(node: TreeNode) -> None:
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
|
|
|
>>> root = TreeNode(1)
|
|
|
|
>>> tree_node2 = TreeNode(2)
|
|
|
|
>>> tree_node3 = TreeNode(3)
|
|
|
|
>>> tree_node4 = TreeNode(4)
|
|
|
|
>>> tree_node5 = TreeNode(5)
|
|
|
|
>>> tree_node6 = TreeNode(6)
|
|
|
|
>>> tree_node7 = TreeNode(7)
|
|
|
|
>>> root.left, root.right = tree_node2, tree_node3
|
|
|
|
>>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5
|
|
|
|
>>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7
|
|
|
|
>>> pre_order(root)
|
2020-09-10 08:31:26 +00:00
|
|
|
1,2,4,5,3,6,7,
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
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
|
2020-09-10 08:31:26 +00:00
|
|
|
print(node.data, end=",")
|
2016-09-25 21:15:14 +00:00
|
|
|
pre_order(node.left)
|
|
|
|
pre_order(node.right)
|
|
|
|
|
2019-02-16 16:16:43 +00:00
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
def in_order(node: TreeNode) -> None:
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
|
|
|
>>> root = TreeNode(1)
|
|
|
|
>>> tree_node2 = TreeNode(2)
|
|
|
|
>>> tree_node3 = TreeNode(3)
|
|
|
|
>>> tree_node4 = TreeNode(4)
|
|
|
|
>>> tree_node5 = TreeNode(5)
|
|
|
|
>>> tree_node6 = TreeNode(6)
|
|
|
|
>>> tree_node7 = TreeNode(7)
|
|
|
|
>>> root.left, root.right = tree_node2, tree_node3
|
|
|
|
>>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5
|
|
|
|
>>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7
|
|
|
|
>>> in_order(root)
|
2020-09-10 08:31:26 +00:00
|
|
|
4,2,5,1,6,3,7,
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
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)
|
2020-09-10 08:31:26 +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
|
|
|
|
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
def post_order(node: TreeNode) -> None:
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
|
|
|
>>> root = TreeNode(1)
|
|
|
|
>>> tree_node2 = TreeNode(2)
|
|
|
|
>>> tree_node3 = TreeNode(3)
|
|
|
|
>>> tree_node4 = TreeNode(4)
|
|
|
|
>>> tree_node5 = TreeNode(5)
|
|
|
|
>>> tree_node6 = TreeNode(6)
|
|
|
|
>>> tree_node7 = TreeNode(7)
|
|
|
|
>>> root.left, root.right = tree_node2, tree_node3
|
|
|
|
>>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5
|
|
|
|
>>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7
|
|
|
|
>>> post_order(root)
|
2020-09-10 08:31:26 +00:00
|
|
|
4,5,2,6,7,3,1,
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
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)
|
2020-09-10 08:31:26 +00:00
|
|
|
print(node.data, end=",")
|
2016-09-25 21:15:14 +00:00
|
|
|
|
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
def level_order(node: TreeNode) -> None:
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
|
|
|
>>> root = TreeNode(1)
|
|
|
|
>>> tree_node2 = TreeNode(2)
|
|
|
|
>>> tree_node3 = TreeNode(3)
|
|
|
|
>>> tree_node4 = TreeNode(4)
|
|
|
|
>>> tree_node5 = TreeNode(5)
|
|
|
|
>>> tree_node6 = TreeNode(6)
|
|
|
|
>>> tree_node7 = TreeNode(7)
|
|
|
|
>>> root.left, root.right = tree_node2, tree_node3
|
|
|
|
>>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5
|
|
|
|
>>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7
|
|
|
|
>>> level_order(root)
|
2020-09-10 08:31:26 +00:00
|
|
|
1,2,3,4,5,6,7,
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
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
|
2019-08-12 15:59:59 +00:00
|
|
|
q: queue.Queue = queue.Queue()
|
2016-09-25 21:15:14 +00:00
|
|
|
q.put(node)
|
|
|
|
while not q.empty():
|
|
|
|
node_dequeued = q.get()
|
2020-09-10 08:31:26 +00:00
|
|
|
print(node_dequeued.data, end=",")
|
2016-09-25 21:15:14 +00:00
|
|
|
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
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
def level_order_actual(node: TreeNode) -> None:
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
|
|
|
>>> root = TreeNode(1)
|
|
|
|
>>> tree_node2 = TreeNode(2)
|
|
|
|
>>> tree_node3 = TreeNode(3)
|
|
|
|
>>> tree_node4 = TreeNode(4)
|
|
|
|
>>> tree_node5 = TreeNode(5)
|
|
|
|
>>> tree_node6 = TreeNode(6)
|
|
|
|
>>> tree_node7 = TreeNode(7)
|
|
|
|
>>> root.left, root.right = tree_node2, tree_node3
|
|
|
|
>>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5
|
|
|
|
>>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7
|
2020-09-10 08:31:26 +00:00
|
|
|
>>> level_order_actual(root)
|
|
|
|
1,
|
|
|
|
2,3,
|
|
|
|
4,5,6,7,
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
2019-02-16 16:16:43 +00:00
|
|
|
if not isinstance(node, TreeNode) or not node:
|
|
|
|
return
|
2019-08-12 15:59:59 +00:00
|
|
|
q: queue.Queue = queue.Queue()
|
2019-02-16 16:16:43 +00:00
|
|
|
q.put(node)
|
|
|
|
while not q.empty():
|
|
|
|
list = []
|
|
|
|
while not q.empty():
|
|
|
|
node_dequeued = q.get()
|
2020-09-10 08:31:26 +00:00
|
|
|
print(node_dequeued.data, end=",")
|
2019-02-16 16:16:43 +00:00
|
|
|
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-08-12 15:59:59 +00:00
|
|
|
def pre_order_iter(node: TreeNode) -> None:
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
|
|
|
>>> root = TreeNode(1)
|
|
|
|
>>> tree_node2 = TreeNode(2)
|
|
|
|
>>> tree_node3 = TreeNode(3)
|
|
|
|
>>> tree_node4 = TreeNode(4)
|
|
|
|
>>> tree_node5 = TreeNode(5)
|
|
|
|
>>> tree_node6 = TreeNode(6)
|
|
|
|
>>> tree_node7 = TreeNode(7)
|
|
|
|
>>> root.left, root.right = tree_node2, tree_node3
|
|
|
|
>>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5
|
|
|
|
>>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7
|
|
|
|
>>> pre_order_iter(root)
|
2020-09-10 08:31:26 +00:00
|
|
|
1,2,4,5,3,6,7,
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
2019-02-09 01:54:07 +00:00
|
|
|
if not isinstance(node, TreeNode) or not node:
|
|
|
|
return
|
2021-03-26 11:21:16 +00:00
|
|
|
stack: list[TreeNode] = []
|
2019-02-09 01:54:07 +00:00
|
|
|
n = node
|
|
|
|
while n or stack:
|
2019-02-16 16:16:43 +00:00
|
|
|
while n: # start from root node, find its left child
|
2020-09-10 08:31:26 +00:00
|
|
|
print(n.data, end=",")
|
2019-02-09 01:54:07 +00:00
|
|
|
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-08-12 15:59:59 +00:00
|
|
|
def in_order_iter(node: TreeNode) -> None:
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
|
|
|
>>> root = TreeNode(1)
|
|
|
|
>>> tree_node2 = TreeNode(2)
|
|
|
|
>>> tree_node3 = TreeNode(3)
|
|
|
|
>>> tree_node4 = TreeNode(4)
|
|
|
|
>>> tree_node5 = TreeNode(5)
|
|
|
|
>>> tree_node6 = TreeNode(6)
|
|
|
|
>>> tree_node7 = TreeNode(7)
|
|
|
|
>>> root.left, root.right = tree_node2, tree_node3
|
|
|
|
>>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5
|
|
|
|
>>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7
|
|
|
|
>>> in_order_iter(root)
|
2020-09-10 08:31:26 +00:00
|
|
|
4,2,5,1,6,3,7,
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
2019-02-09 01:54:07 +00:00
|
|
|
if not isinstance(node, TreeNode) or not node:
|
|
|
|
return
|
2021-03-26 11:21:16 +00:00
|
|
|
stack: list[TreeNode] = []
|
2019-02-09 01:54:07 +00:00
|
|
|
n = node
|
|
|
|
while n or stack:
|
|
|
|
while n:
|
|
|
|
stack.append(n)
|
|
|
|
n = n.left
|
|
|
|
n = stack.pop()
|
2020-09-10 08:31:26 +00:00
|
|
|
print(n.data, end=",")
|
2019-02-09 01:54:07 +00:00
|
|
|
n = n.right
|
|
|
|
|
2019-02-16 16:16:43 +00:00
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
def post_order_iter(node: TreeNode) -> None:
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
|
|
|
>>> root = TreeNode(1)
|
|
|
|
>>> tree_node2 = TreeNode(2)
|
|
|
|
>>> tree_node3 = TreeNode(3)
|
|
|
|
>>> tree_node4 = TreeNode(4)
|
|
|
|
>>> tree_node5 = TreeNode(5)
|
|
|
|
>>> tree_node6 = TreeNode(6)
|
|
|
|
>>> tree_node7 = TreeNode(7)
|
|
|
|
>>> root.left, root.right = tree_node2, tree_node3
|
|
|
|
>>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5
|
|
|
|
>>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7
|
|
|
|
>>> post_order_iter(root)
|
2020-09-10 08:31:26 +00:00
|
|
|
4,5,2,6,7,3,1,
|
2019-08-25 12:14:17 +00:00
|
|
|
"""
|
2019-02-09 01:54:07 +00:00
|
|
|
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
|
2020-09-10 08:31:26 +00:00
|
|
|
print(stack2.pop().data, end=",")
|
2016-09-25 21:15:14 +00:00
|
|
|
|
2019-02-16 16:16:43 +00:00
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
def prompt(s: str = "", width=50, char="*") -> str:
|
|
|
|
if not s:
|
|
|
|
return "\n" + width * char
|
|
|
|
left, extra = divmod(width - len(s) - 2, 2)
|
|
|
|
return f"{left * char} {s} {(left + extra) * char}"
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-08-25 12:14:17 +00:00
|
|
|
import doctest
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-08-25 12:14:17 +00:00
|
|
|
doctest.testmod()
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt("Binary Tree Traversals"))
|
2016-09-25 21:15:14 +00:00
|
|
|
|
2016-09-25 21:22:05 +00:00
|
|
|
node = build_tree()
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt("Pre Order Traversal"))
|
2016-09-25 21:22:05 +00:00
|
|
|
pre_order(node)
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt() + "\n")
|
2016-09-25 21:15:14 +00:00
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt("In Order Traversal"))
|
2016-09-25 21:22:05 +00:00
|
|
|
in_order(node)
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt() + "\n")
|
2016-09-25 21:15:14 +00:00
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt("Post Order Traversal"))
|
2016-09-25 21:22:05 +00:00
|
|
|
post_order(node)
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt() + "\n")
|
2016-09-25 21:15:14 +00:00
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt("Level Order Traversal"))
|
2016-09-25 21:22:05 +00:00
|
|
|
level_order(node)
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt() + "\n")
|
2019-02-09 01:54:07 +00:00
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt("Actual Level Order Traversal"))
|
2019-02-16 16:16:43 +00:00
|
|
|
level_order_actual(node)
|
2019-08-12 15:59:59 +00:00
|
|
|
print("*" * 50 + "\n")
|
2019-02-16 16:16:43 +00:00
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt("Pre Order Traversal - Iteration Version"))
|
2019-02-09 01:54:07 +00:00
|
|
|
pre_order_iter(node)
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt() + "\n")
|
2019-02-09 01:54:07 +00:00
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt("In Order Traversal - Iteration Version"))
|
2019-02-09 01:54:07 +00:00
|
|
|
in_order_iter(node)
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt() + "\n")
|
2019-02-09 01:54:07 +00:00
|
|
|
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt("Post Order Traversal - Iteration Version"))
|
2019-02-09 01:54:07 +00:00
|
|
|
post_order_iter(node)
|
2019-08-12 15:59:59 +00:00
|
|
|
print(prompt())
|