Skip to content

Commit 980930e

Browse files
Rohan OrugantiRohan Oruganti
authored andcommitted
Add all_traversals_one_pass: single-pass iterative tree traversals (pre,in,post)
1 parent e2a78d4 commit 980930e

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
"""
2+
all_traversals_one_pass.py
3+
4+
This module demonstrates how to perform Preorder, Inorder, and Postorder traversals
5+
of a binary tree in a single traversal using an iterative approach with a stack.
6+
"""
7+
8+
from typing import Optional, List, Tuple
9+
10+
class Node:
11+
"""
12+
A class to represent a node in a binary tree.
13+
14+
Attributes
15+
----------
16+
data : int
17+
The value stored in the node.
18+
left : Node, optional
19+
The left child of the node.
20+
right : Node, optional
21+
The right child of the node.
22+
"""
23+
24+
def __init__(self, data: int):
25+
self.data = data
26+
self.left: Optional[Node] = None
27+
self.right: Optional[Node] = None
28+
29+
30+
def all_traversals(root: Optional[Node]) -> Tuple[List[int], List[int], List[int]]:
31+
"""
32+
Perform Preorder, Inorder, and Postorder traversals in a single pass.
33+
34+
Parameters
35+
----------
36+
root : Node
37+
The root of the binary tree.
38+
39+
Returns
40+
-------
41+
Tuple[List[int], List[int], List[int]]
42+
A tuple containing three lists:
43+
- preorder : List of nodes in Preorder (Root -> Left -> Right)
44+
- inorder : List of nodes in Inorder (Left -> Root -> Right)
45+
- postorder : List of nodes in Postorder(Left -> Right -> Root)
46+
47+
Explanation
48+
-----------
49+
Each node is paired with a state value:
50+
state = 1 → Preorder processing (Root node is first time seen)
51+
state = 2 → Inorder processing (Left subtree done, process root)
52+
state = 3 → Postorder processing (Both subtrees done)
53+
54+
Algorithm Steps:
55+
---------------
56+
1. Initialize a stack with (root, 1).
57+
2. Pop the top element:
58+
- If state == 1:
59+
→ Add node to Preorder
60+
→ Push (node, 2)
61+
→ If left child exists, push (left, 1)
62+
- If state == 2:
63+
→ Add node to Inorder
64+
→ Push (node, 3)
65+
→ If right child exists, push (right, 1)
66+
- If state == 3:
67+
→ Add node to Postorder
68+
3. Continue until stack is empty.
69+
"""
70+
71+
if root is None:
72+
return [], [], []
73+
74+
stack: List[Tuple[Node, int]] = [(root, 1)] # (node, state)
75+
preorder, inorder, postorder = [], [], []
76+
77+
while stack:
78+
node, state = stack.pop()
79+
80+
if state == 1:
81+
# Preorder position
82+
preorder.append(node.data)
83+
# Increment state to process inorder next time
84+
stack.append((node, 2))
85+
# Left child goes before inorder
86+
if node.left:
87+
stack.append((node.left, 1))
88+
89+
elif state == 2:
90+
# Inorder position
91+
inorder.append(node.data)
92+
# Increment state to process postorder next time
93+
stack.append((node, 3))
94+
# Right child goes before postorder
95+
if node.right:
96+
stack.append((node.right, 1))
97+
98+
else:
99+
# Postorder position
100+
postorder.append(node.data)
101+
102+
return preorder, inorder, postorder
103+
104+
105+
def build_sample_tree() -> Node:
106+
"""
107+
Build and return a sample binary tree for demonstration.
108+
109+
1
110+
/ \\
111+
2 3
112+
/ \\ \\
113+
4 5 6
114+
115+
Returns
116+
-------
117+
Node
118+
The root node of the binary tree.
119+
"""
120+
root = Node(1)
121+
root.left = Node(2)
122+
root.right = Node(3)
123+
root.left.left = Node(4)
124+
root.left.right = Node(5)
125+
root.right.right = Node(6)
126+
return root
127+
128+
129+
if __name__ == "__main__":
130+
# Example
131+
root = build_sample_tree()
132+
preorder, inorder, postorder = all_traversals(root)
133+
134+
print("Preorder :", preorder)
135+
print("Inorder :", inorder)
136+
print("Postorder :", postorder)

0 commit comments

Comments
 (0)