Skip to content

Commit 31bc93f

Browse files
committed
feat: Implemented Morris postorder traversal for Binary tree
1 parent f08e77f commit 31bc93f

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

Diff for: data_structures/binary_tree/morris_inorder_traversal.py renamed to data_structures/binary_tree/morris_postorder_traversal.py

+39-37
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
2-
Problem Statement: Given a binary perform an inorder traversal using Morris Inorder
3-
traversal algorithm. (Iterative version of Inorder traversal of tree)
2+
Problem Statement: Given a binary perform an postorder traversal using Morris Postorder
3+
traversal algorithm. (Iterative version of Postorder traversal of tree)
44
5-
https://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/
5+
https://www.geeksforgeeks.org/morris-traversal-for-postorder/
66
"""
77

88

@@ -35,8 +35,8 @@ class BinaryTree:
3535
insert(value: int) -> None:
3636
Insert a value into the binary tree following binary search tree (BST) rules.
3737
38-
morris_inorder_traversal() -> List[int]:
39-
Perform inorder traversal and return list of node values.
38+
morris_postorder_traversal() -> List[int]:
39+
Perform postorder traversal and return list of node values.
4040
4141
4242
>>> bt = BinaryTree()
@@ -49,8 +49,8 @@ class BinaryTree:
4949
>>> bt.insert(2)
5050
>>> bt.insert(5)
5151
>>> bt.insert(4)
52-
>>> bt.morris_inorder_traversal()
53-
[2, 3, 4, 5, 6, 7, 9, 10, 12]
52+
>>> bt.morris_postorder_traversal()
53+
[2, 4, 5, 3, 7, 6, 12, 10, 9]
5454
5555
"""
5656

@@ -92,9 +92,9 @@ def _insert_recursive(self, node: TreeNode, value: int) -> None:
9292
else:
9393
self._insert_recursive(node.right, value)
9494

95-
def _predecessor(self, node: TreeNode) -> TreeNode:
95+
def _successor(self, node: TreeNode) -> TreeNode:
9696
"""
97-
Helper Function to return predecessor of the given node in a binary tree
97+
Helper Function to return successor of the given node in a binary tree
9898
9999
Parameters:
100100
-----------
@@ -104,62 +104,64 @@ def _predecessor(self, node: TreeNode) -> TreeNode:
104104
Returns:
105105
--------
106106
TreeNode:
107-
The predecessor of the node passed in the parameter
107+
The successor of the node passed in the parameter
108108
"""
109-
temp_node = node.left
110-
while temp_node and temp_node.right and temp_node.right != node:
111-
temp_node = temp_node.right
112-
assert temp_node is not None, "Predecessor should not be None"
109+
temp_node = node.right
110+
while temp_node and temp_node.left and temp_node.left != node:
111+
temp_node = temp_node.left
112+
assert temp_node is not None, "Successor should not be None"
113113
return temp_node
114114

115-
def morris_inorder_traversal(self) -> list[int]:
115+
def morris_postorder_traversal(self) -> list[int]:
116116
"""
117-
Function for inorder traversal using morris inorder traversal.
117+
Function for postorder traversal using morris postorder traversal.
118118
119119
Algorithm :
120120
------------
121121
First set current node as root node.
122122
123123
while current node is not empty
124-
If the current node has no left child.
125-
print the current node.
126-
point the current node to its right child
124+
If the current node has no right child.
125+
push the current node to temp.
126+
point the current node to its left child
127127
128128
else.
129-
find predecssor node of the current node.
130-
if predecessor has no right child,
131-
make the current node as the right child of the predecessor node.
132-
point current node to its left child.
129+
find successor node of the current node.
130+
if successor left child points to current node,
131+
remove the link of the left child of successor node.
132+
point the current node to its left child.
133133
else,
134-
remove the link of the right child of predecessor node.
135-
print the current node
136-
point the current node to its right child.
134+
push the current node to temp.
135+
make the current node as the left child of the successor node.
136+
point current node to its right child.
137+
138+
Reverse the temp array to get postorder traversaol.
137139
138140
Returns:
139141
--------
140142
List[int]:
141-
A list of integers representing the inorder traversal.
143+
A list of integers representing the postorder traversal.
142144
143145
144146
145147
"""
146-
inorder_traversal = []
148+
postorder_traversal = []
147149
current_node = self.root
148150

149151
while current_node:
150-
if current_node.left is None:
151-
inorder_traversal.append(current_node.value)
152-
current_node = current_node.right
152+
if current_node.right is None:
153+
postorder_traversal.append(current_node.value)
154+
current_node = current_node.left
153155
else:
154-
predecessor_node = self._predecessor(current_node)
155-
if predecessor_node.right is None:
156-
predecessor_node.right = current_node
156+
successor_node = self._successor(current_node)
157+
if successor_node.left == current_node:
158+
successor_node.left = current_node
157159
current_node = current_node.left
158160
else:
159-
predecessor_node.right = None
160-
inorder_traversal.append(current_node.value)
161+
postorder_traversal.append(current_node.value)
162+
successor_node.left = current_node
161163
current_node = current_node.right
162-
return inorder_traversal
164+
return postorder_traversal[::-1]
163165

164166

165167
if __name__ == "__main__":

0 commit comments

Comments
 (0)