From 980930ed7b1567f2b6700c0510c2f6a05dab10e6 Mon Sep 17 00:00:00 2001 From: Rohan Oruganti Date: Sun, 26 Oct 2025 17:32:05 +0530 Subject: [PATCH 1/4] Add all_traversals_one_pass: single-pass iterative tree traversals (pre,in,post) --- .../binary_tree/all_traversals_one_pass.py | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 data_structures/binary_tree/all_traversals_one_pass.py diff --git a/data_structures/binary_tree/all_traversals_one_pass.py b/data_structures/binary_tree/all_traversals_one_pass.py new file mode 100644 index 000000000000..d93480328c16 --- /dev/null +++ b/data_structures/binary_tree/all_traversals_one_pass.py @@ -0,0 +1,136 @@ +""" +all_traversals_one_pass.py + +This module demonstrates how to perform Preorder, Inorder, and Postorder traversals +of a binary tree in a single traversal using an iterative approach with a stack. +""" + +from typing import Optional, List, Tuple + +class Node: + """ + A class to represent a node in a binary tree. + + Attributes + ---------- + data : int + The value stored in the node. + left : Node, optional + The left child of the node. + right : Node, optional + The right child of the node. + """ + + def __init__(self, data: int): + self.data = data + self.left: Optional[Node] = None + self.right: Optional[Node] = None + + +def all_traversals(root: Optional[Node]) -> Tuple[List[int], List[int], List[int]]: + """ + Perform Preorder, Inorder, and Postorder traversals in a single pass. + + Parameters + ---------- + root : Node + The root of the binary tree. + + Returns + ------- + Tuple[List[int], List[int], List[int]] + A tuple containing three lists: + - preorder : List of nodes in Preorder (Root -> Left -> Right) + - inorder : List of nodes in Inorder (Left -> Root -> Right) + - postorder : List of nodes in Postorder(Left -> Right -> Root) + + Explanation + ----------- + Each node is paired with a state value: + state = 1 → Preorder processing (Root node is first time seen) + state = 2 → Inorder processing (Left subtree done, process root) + state = 3 → Postorder processing (Both subtrees done) + + Algorithm Steps: + --------------- + 1. Initialize a stack with (root, 1). + 2. Pop the top element: + - If state == 1: + → Add node to Preorder + → Push (node, 2) + → If left child exists, push (left, 1) + - If state == 2: + → Add node to Inorder + → Push (node, 3) + → If right child exists, push (right, 1) + - If state == 3: + → Add node to Postorder + 3. Continue until stack is empty. + """ + + if root is None: + return [], [], [] + + stack: List[Tuple[Node, int]] = [(root, 1)] # (node, state) + preorder, inorder, postorder = [], [], [] + + while stack: + node, state = stack.pop() + + if state == 1: + # Preorder position + preorder.append(node.data) + # Increment state to process inorder next time + stack.append((node, 2)) + # Left child goes before inorder + if node.left: + stack.append((node.left, 1)) + + elif state == 2: + # Inorder position + inorder.append(node.data) + # Increment state to process postorder next time + stack.append((node, 3)) + # Right child goes before postorder + if node.right: + stack.append((node.right, 1)) + + else: + # Postorder position + postorder.append(node.data) + + return preorder, inorder, postorder + + +def build_sample_tree() -> Node: + """ + Build and return a sample binary tree for demonstration. + + 1 + / \\ + 2 3 + / \\ \\ + 4 5 6 + + Returns + ------- + Node + The root node of the binary tree. + """ + root = Node(1) + root.left = Node(2) + root.right = Node(3) + root.left.left = Node(4) + root.left.right = Node(5) + root.right.right = Node(6) + return root + + +if __name__ == "__main__": + # Example + root = build_sample_tree() + preorder, inorder, postorder = all_traversals(root) + + print("Preorder :", preorder) + print("Inorder :", inorder) + print("Postorder :", postorder) From 1605604e96055c1f73491740ee21b9bfd3464195 Mon Sep 17 00:00:00 2001 From: rohan Date: Sun, 26 Oct 2025 17:46:04 +0530 Subject: [PATCH 2/4] Added a python file to Implement Preorder, Inorder, and Postorder traversals in one traversal. --- data_structures/binary_tree/all_traversals_one_pass.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/data_structures/binary_tree/all_traversals_one_pass.py b/data_structures/binary_tree/all_traversals_one_pass.py index d93480328c16..816b8eda10fd 100644 --- a/data_structures/binary_tree/all_traversals_one_pass.py +++ b/data_structures/binary_tree/all_traversals_one_pass.py @@ -19,6 +19,8 @@ class Node: The left child of the node. right : Node, optional The right child of the node. + + Reference: https://en.wikipedia.org/wiki/Binary_tree """ def __init__(self, data: int): @@ -66,6 +68,10 @@ def all_traversals(root: Optional[Node]) -> Tuple[List[int], List[int], List[int - If state == 3: → Add node to Postorder 3. Continue until stack is empty. + + Reference: Tree Traversals- https://en.wikipedia.org/wiki/Tree_traversal + Stack Data Structure- https://en.wikipedia.org/wiki/Stack_(abstract_data_type) + """ if root is None: @@ -116,6 +122,8 @@ def build_sample_tree() -> Node: ------- Node The root node of the binary tree. + + Reference: https://en.wikipedia.org/wiki/Binary_tree """ root = Node(1) root.left = Node(2) From 4baa72bb581ae126b2961b77b3e535f87f707b62 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 26 Oct 2025 12:22:07 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/binary_tree/all_traversals_one_pass.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/all_traversals_one_pass.py b/data_structures/binary_tree/all_traversals_one_pass.py index 816b8eda10fd..35a3695bc67d 100644 --- a/data_structures/binary_tree/all_traversals_one_pass.py +++ b/data_structures/binary_tree/all_traversals_one_pass.py @@ -7,6 +7,7 @@ from typing import Optional, List, Tuple + class Node: """ A class to represent a node in a binary tree. @@ -19,7 +20,7 @@ class Node: The left child of the node. right : Node, optional The right child of the node. - + Reference: https://en.wikipedia.org/wiki/Binary_tree """ @@ -122,7 +123,7 @@ def build_sample_tree() -> Node: ------- Node The root node of the binary tree. - + Reference: https://en.wikipedia.org/wiki/Binary_tree """ root = Node(1) From 04b6d55d3746b434ab4048e457013fbb354f37a4 Mon Sep 17 00:00:00 2001 From: rohan Date: Sun, 26 Oct 2025 17:46:04 +0530 Subject: [PATCH 4/4] Added a python file to Implement Preorder, Inorder, and Postorder traversals in one traversal. --- .../binary_tree/all_traversals_one_pass.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/data_structures/binary_tree/all_traversals_one_pass.py b/data_structures/binary_tree/all_traversals_one_pass.py index 35a3695bc67d..e26135984065 100644 --- a/data_structures/binary_tree/all_traversals_one_pass.py +++ b/data_structures/binary_tree/all_traversals_one_pass.py @@ -5,7 +5,7 @@ of a binary tree in a single traversal using an iterative approach with a stack. """ -from typing import Optional, List, Tuple +from __future__ import annotations class Node: @@ -20,17 +20,16 @@ class Node: The left child of the node. right : Node, optional The right child of the node. - Reference: https://en.wikipedia.org/wiki/Binary_tree """ def __init__(self, data: int): self.data = data - self.left: Optional[Node] = None - self.right: Optional[Node] = None + self.left: Node | None = None + self.right: Node | None = None -def all_traversals(root: Optional[Node]) -> Tuple[List[int], List[int], List[int]]: +def all_traversals(root: Node | None) -> tuple[list[int], list[int], list[int]]: """ Perform Preorder, Inorder, and Postorder traversals in a single pass. @@ -78,8 +77,10 @@ def all_traversals(root: Optional[Node]) -> Tuple[List[int], List[int], List[int if root is None: return [], [], [] - stack: List[Tuple[Node, int]] = [(root, 1)] # (node, state) - preorder, inorder, postorder = [], [], [] + stack: list[tuple[Node, int]] = [(root, 1)] # (node, state) + preorder: list[int] = [] + inorder: list[int] = [] + postorder: list[int] = [] while stack: node, state = stack.pop()