Skip to content

[mypy] Fix type annotations for binary tree traversals in data structures #5556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 28, 2021
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 25 additions & 33 deletions data_structures/binary_tree/binary_tree_traversals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# https://en.wikipedia.org/wiki/Tree_traversal
from __future__ import annotations

from collections import deque
from dataclasses import dataclass
from typing import Sequence


@dataclass
Expand All @@ -11,11 +13,11 @@ class Node:
right: Node | None = None


def make_tree() -> Node:
def make_tree() -> Node | None:
return Node(1, Node(2, Node(4), Node(5)), Node(3))


def preorder(root: Node):
def preorder(root: Node | None) -> list[int]:
"""
Pre-order traversal visits root node, left subtree, right subtree.
>>> preorder(make_tree())
Expand All @@ -24,7 +26,7 @@ def preorder(root: Node):
return [root.data] + preorder(root.left) + preorder(root.right) if root else []


def postorder(root: Node):
def postorder(root: Node | None) -> list[int]:
"""
Post-order traversal visits left subtree, right subtree, root node.
>>> postorder(make_tree())
Expand All @@ -33,7 +35,7 @@ def postorder(root: Node):
return postorder(root.left) + postorder(root.right) + [root.data] if root else []


def inorder(root: Node):
def inorder(root: Node | None) -> list[int]:
"""
In-order traversal visits left subtree, root node, right subtree.
>>> inorder(make_tree())
Expand All @@ -42,7 +44,7 @@ def inorder(root: Node):
return inorder(root.left) + [root.data] + inorder(root.right) if root else []


def height(root: Node):
def height(root: Node | None) -> int:
"""
Recursive function for calculating the height of the binary tree.
>>> height(None)
Expand All @@ -53,39 +55,29 @@ def height(root: Node):
return (max(height(root.left), height(root.right)) + 1) if root else 0


def level_order_1(root: Node):
def level_order(root: Node | None) -> Sequence[Node | None]:
"""
Print whole binary tree in Level Order Traverse.
Level Order traverse: Visit nodes of the tree level-by-level.
"""
if not root:
return
return []

temp = root
que = [temp]
while len(que) > 0:
print(que[0].data, end=" ")
temp = que.pop(0)
if temp.left:
que.append(temp.left)
if temp.right:
que.append(temp.right)
return que
process_queue = deque([temp])

while process_queue:
print(process_queue[0].data, end=" ")
temp = process_queue.popleft()

def level_order_2(root: Node, level: int):
"""
Level-wise traversal: Print all nodes present at the given level of the binary tree
"""
if not root:
return root
if level == 1:
print(root.data, end=" ")
elif level > 1:
level_order_2(root.left, level - 1)
level_order_2(root.right, level - 1)
if temp.left:
process_queue.append(temp.left)
if temp.right:
process_queue.append(temp.right)
return process_queue


def print_left_to_right(root: Node, level: int):
def print_left_to_right(root: Node | None, level: int) -> None:
"""
Print elements on particular level from left to right direction of the binary tree.
"""
Expand All @@ -98,7 +90,7 @@ def print_left_to_right(root: Node, level: int):
print_left_to_right(root.right, level - 1)


def print_right_to_left(root: Node, level: int):
def print_right_to_left(root: Node | None, level: int) -> None:
"""
Print elements on particular level from right to left direction of the binary tree.
"""
Expand All @@ -111,7 +103,7 @@ def print_right_to_left(root: Node, level: int):
print_right_to_left(root.left, level - 1)


def zigzag(root: Node):
def zigzag(root: Node | None) -> None:
"""
ZigZag traverse: Print node left to right and right to left, alternatively.
"""
Expand All @@ -126,7 +118,7 @@ def zigzag(root: Node):
flag = 0


def main(): # Main function for testing.
def main() -> None: # Main function for testing.
"""
Create binary tree.
"""
Expand All @@ -139,10 +131,10 @@ def main(): # Main function for testing.
print(f"Post-order Traversal is {postorder(root)}")
print(f"Height of Tree is {height(root)}")
print("Complete Level Order Traversal is : ")
level_order_1(root)
level_order(root)
print("\nLevel-wise order Traversal is : ")
for h in range(1, height(root) + 1):
level_order_2(root, h)
print_left_to_right(root, h)
print("\nZigZag order Traversal is : ")
zigzag(root)
print()
Expand Down