1
1
"""
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)
4
4
5
- https://www.geeksforgeeks.org/inorder-tree- traversal-without-recursion-and-without-stack /
5
+ https://www.geeksforgeeks.org/morris- traversal-for-postorder /
6
6
"""
7
7
8
8
@@ -35,8 +35,8 @@ class BinaryTree:
35
35
insert(value: int) -> None:
36
36
Insert a value into the binary tree following binary search tree (BST) rules.
37
37
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.
40
40
41
41
42
42
>>> bt = BinaryTree()
@@ -49,8 +49,8 @@ class BinaryTree:
49
49
>>> bt.insert(2)
50
50
>>> bt.insert(5)
51
51
>>> 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 ]
54
54
55
55
"""
56
56
@@ -92,9 +92,9 @@ def _insert_recursive(self, node: TreeNode, value: int) -> None:
92
92
else :
93
93
self ._insert_recursive (node .right , value )
94
94
95
- def _predecessor (self , node : TreeNode ) -> TreeNode :
95
+ def _successor (self , node : TreeNode ) -> TreeNode :
96
96
"""
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
98
98
99
99
Parameters:
100
100
-----------
@@ -104,62 +104,64 @@ def _predecessor(self, node: TreeNode) -> TreeNode:
104
104
Returns:
105
105
--------
106
106
TreeNode:
107
- The predecessor of the node passed in the parameter
107
+ The successor of the node passed in the parameter
108
108
"""
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"
113
113
return temp_node
114
114
115
- def morris_inorder_traversal (self ) -> list [int ]:
115
+ def morris_postorder_traversal (self ) -> list [int ]:
116
116
"""
117
- Function for inorder traversal using morris inorder traversal.
117
+ Function for postorder traversal using morris postorder traversal.
118
118
119
119
Algorithm :
120
120
------------
121
121
First set current node as root node.
122
122
123
123
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
127
127
128
128
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.
133
133
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.
137
139
138
140
Returns:
139
141
--------
140
142
List[int]:
141
- A list of integers representing the inorder traversal.
143
+ A list of integers representing the postorder traversal.
142
144
143
145
144
146
145
147
"""
146
- inorder_traversal = []
148
+ postorder_traversal = []
147
149
current_node = self .root
148
150
149
151
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
153
155
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
157
159
current_node = current_node .left
158
160
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
161
163
current_node = current_node .right
162
- return inorder_traversal
164
+ return postorder_traversal [:: - 1 ]
163
165
164
166
165
167
if __name__ == "__main__" :
0 commit comments