mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 17:20:16 +00:00
Add type hints to binary_tree_traversals.py (#1123)
This commit is contained in:
parent
158b319d22
commit
4fea48072a
|
@ -1,14 +1,8 @@
|
||||||
"""
|
"""
|
||||||
This is pure python implementation of tree traversal algorithms
|
This is pure python implementation of tree traversal algorithms
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import queue
|
import queue
|
||||||
|
from typing import List
|
||||||
try:
|
|
||||||
raw_input # Python 2
|
|
||||||
except NameError:
|
|
||||||
raw_input = input # Python 3
|
|
||||||
|
|
||||||
|
|
||||||
class TreeNode:
|
class TreeNode:
|
||||||
|
@ -20,35 +14,31 @@ class TreeNode:
|
||||||
|
|
||||||
def build_tree():
|
def build_tree():
|
||||||
print("\n********Press N to stop entering at any point of time********\n")
|
print("\n********Press N to stop entering at any point of time********\n")
|
||||||
print("Enter the value of the root node: ", end="")
|
check = input("Enter the value of the root node: ").strip().lower() or "n"
|
||||||
check = raw_input().strip().lower()
|
if check == "n":
|
||||||
if check == 'n':
|
|
||||||
return None
|
return None
|
||||||
data = int(check)
|
q: queue.Queue = queue.Queue()
|
||||||
q = queue.Queue()
|
tree_node = TreeNode(int(check))
|
||||||
tree_node = TreeNode(data)
|
|
||||||
q.put(tree_node)
|
q.put(tree_node)
|
||||||
while not q.empty():
|
while not q.empty():
|
||||||
node_found = q.get()
|
node_found = q.get()
|
||||||
print("Enter the left node of %s: " % node_found.data, end="")
|
msg = "Enter the left node of %s: " % node_found.data
|
||||||
check = raw_input().strip().lower()
|
check = input(msg).strip().lower() or "n"
|
||||||
if check == 'n':
|
if check == "n":
|
||||||
return tree_node
|
return tree_node
|
||||||
left_data = int(check)
|
left_node = TreeNode(int(check))
|
||||||
left_node = TreeNode(left_data)
|
|
||||||
node_found.left = left_node
|
node_found.left = left_node
|
||||||
q.put(left_node)
|
q.put(left_node)
|
||||||
print("Enter the right node of %s: " % node_found.data, end="")
|
msg = "Enter the right node of %s: " % node_found.data
|
||||||
check = raw_input().strip().lower()
|
check = input(msg).strip().lower() or "n"
|
||||||
if check == 'n':
|
if check == "n":
|
||||||
return tree_node
|
return tree_node
|
||||||
right_data = int(check)
|
right_node = TreeNode(int(check))
|
||||||
right_node = TreeNode(right_data)
|
|
||||||
node_found.right = right_node
|
node_found.right = right_node
|
||||||
q.put(right_node)
|
q.put(right_node)
|
||||||
|
|
||||||
|
|
||||||
def pre_order(node):
|
def pre_order(node: TreeNode) -> None:
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
print(node.data, end=" ")
|
print(node.data, end=" ")
|
||||||
|
@ -56,7 +46,7 @@ def pre_order(node):
|
||||||
pre_order(node.right)
|
pre_order(node.right)
|
||||||
|
|
||||||
|
|
||||||
def in_order(node):
|
def in_order(node: TreeNode) -> None:
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
in_order(node.left)
|
in_order(node.left)
|
||||||
|
@ -64,7 +54,7 @@ def in_order(node):
|
||||||
in_order(node.right)
|
in_order(node.right)
|
||||||
|
|
||||||
|
|
||||||
def post_order(node):
|
def post_order(node: TreeNode) -> None:
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
post_order(node.left)
|
post_order(node.left)
|
||||||
|
@ -72,10 +62,10 @@ def post_order(node):
|
||||||
print(node.data, end=" ")
|
print(node.data, end=" ")
|
||||||
|
|
||||||
|
|
||||||
def level_order(node):
|
def level_order(node: TreeNode) -> None:
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
q = queue.Queue()
|
q: queue.Queue = queue.Queue()
|
||||||
q.put(node)
|
q.put(node)
|
||||||
while not q.empty():
|
while not q.empty():
|
||||||
node_dequeued = q.get()
|
node_dequeued = q.get()
|
||||||
|
@ -86,10 +76,10 @@ def level_order(node):
|
||||||
q.put(node_dequeued.right)
|
q.put(node_dequeued.right)
|
||||||
|
|
||||||
|
|
||||||
def level_order_actual(node):
|
def level_order_actual(node: TreeNode) -> None:
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
q = queue.Queue()
|
q: queue.Queue = queue.Queue()
|
||||||
q.put(node)
|
q.put(node)
|
||||||
while not q.empty():
|
while not q.empty():
|
||||||
list = []
|
list = []
|
||||||
|
@ -106,10 +96,10 @@ def level_order_actual(node):
|
||||||
|
|
||||||
|
|
||||||
# iteration version
|
# iteration version
|
||||||
def pre_order_iter(node):
|
def pre_order_iter(node: TreeNode) -> None:
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
stack = []
|
stack: List[TreeNode] = []
|
||||||
n = node
|
n = node
|
||||||
while n or stack:
|
while n or stack:
|
||||||
while n: # start from root node, find its left child
|
while n: # start from root node, find its left child
|
||||||
|
@ -122,10 +112,10 @@ def pre_order_iter(node):
|
||||||
n = n.right
|
n = n.right
|
||||||
|
|
||||||
|
|
||||||
def in_order_iter(node):
|
def in_order_iter(node: TreeNode) -> None:
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
stack = []
|
stack: List[TreeNode] = []
|
||||||
n = node
|
n = node
|
||||||
while n or stack:
|
while n or stack:
|
||||||
while n:
|
while n:
|
||||||
|
@ -136,7 +126,7 @@ def in_order_iter(node):
|
||||||
n = n.right
|
n = n.right
|
||||||
|
|
||||||
|
|
||||||
def post_order_iter(node):
|
def post_order_iter(node: TreeNode) -> None:
|
||||||
if not isinstance(node, TreeNode) or not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
stack1, stack2 = [], []
|
stack1, stack2 = [], []
|
||||||
|
@ -153,38 +143,45 @@ def post_order_iter(node):
|
||||||
print(stack2.pop().data, end=" ")
|
print(stack2.pop().data, end=" ")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def prompt(s: str = "", width=50, char="*") -> str:
|
||||||
print("\n********* Binary Tree Traversals ************\n")
|
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__":
|
||||||
|
print(prompt("Binary Tree Traversals"))
|
||||||
|
|
||||||
node = build_tree()
|
node = build_tree()
|
||||||
print("\n********* Pre Order Traversal ************")
|
print(prompt("Pre Order Traversal"))
|
||||||
pre_order(node)
|
pre_order(node)
|
||||||
print("\n******************************************\n")
|
print(prompt() + "\n")
|
||||||
|
|
||||||
print("\n********* In Order Traversal ************")
|
print(prompt("In Order Traversal"))
|
||||||
in_order(node)
|
in_order(node)
|
||||||
print("\n******************************************\n")
|
print(prompt() + "\n")
|
||||||
|
|
||||||
print("\n********* Post Order Traversal ************")
|
print(prompt("Post Order Traversal"))
|
||||||
post_order(node)
|
post_order(node)
|
||||||
print("\n******************************************\n")
|
print(prompt() + "\n")
|
||||||
|
|
||||||
print("\n********* Level Order Traversal ************")
|
print(prompt("Level Order Traversal"))
|
||||||
level_order(node)
|
level_order(node)
|
||||||
print("\n******************************************\n")
|
print(prompt() + "\n")
|
||||||
|
|
||||||
print("\n********* Actual Level Order Traversal ************")
|
print(prompt("Actual Level Order Traversal"))
|
||||||
level_order_actual(node)
|
level_order_actual(node)
|
||||||
print("\n******************************************\n")
|
print("*" * 50 + "\n")
|
||||||
|
|
||||||
print("\n********* Pre Order Traversal - Iteration Version ************")
|
print(prompt("Pre Order Traversal - Iteration Version"))
|
||||||
pre_order_iter(node)
|
pre_order_iter(node)
|
||||||
print("\n******************************************\n")
|
print(prompt() + "\n")
|
||||||
|
|
||||||
print("\n********* In Order Traversal - Iteration Version ************")
|
print(prompt("In Order Traversal - Iteration Version"))
|
||||||
in_order_iter(node)
|
in_order_iter(node)
|
||||||
print("\n******************************************\n")
|
print(prompt() + "\n")
|
||||||
|
|
||||||
print("\n********* Post Order Traversal - Iteration Version ************")
|
print(prompt("Post Order Traversal - Iteration Version"))
|
||||||
post_order_iter(node)
|
post_order_iter(node)
|
||||||
print("\n******************************************\n")
|
print(prompt())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user