-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathall_traversals_one_pass.py
More file actions
146 lines (117 loc) · 3.95 KB
/
all_traversals_one_pass.py
File metadata and controls
146 lines (117 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
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 __future__ import annotations
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.
Reference: https://en.wikipedia.org/wiki/Binary_tree
"""
def __init__(self, data: int):
self.data = data
self.left: Node | None = None
self.right: Node | None = None
def all_traversals(root: Node | None) -> 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.
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:
return [], [], []
stack: list[tuple[Node, int]] = [(root, 1)] # (node, state)
preorder: list[int] = []
inorder: list[int] = []
postorder: list[int] = []
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.
Reference: https://en.wikipedia.org/wiki/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)