-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Morris Postorder traversal #11775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
kuntal0901
wants to merge
5
commits into
TheAlgorithms:master
from
kuntal0901:morris_postorder_traversal
+152
−0
Closed
Morris Postorder traversal #11775
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e4a9f54
feat: Implemented Morris Inorder traversal for Binary tree
kuntal0901 c6f14ee
feat: Implemented Morris postorder traversal for Binary tree
kuntal0901 f08e77f
deleting unecessary files
kuntal0901 31bc93f
feat: Implemented Morris postorder traversal for Binary tree
kuntal0901 7e01068
fix: code review changes
kuntal0901 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
170 changes: 170 additions & 0 deletions
170
data_structures/binary_tree/morris_postorder_traversal.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,170 @@ | ||||
""" | ||||
Problem Statement: Given a binary perform an postorder traversal using Morris Postorder | ||||
traversal algorithm. (Iterative version of Postorder traversal of tree) | ||||
|
||||
https://www.geeksforgeeks.org/morris-traversal-for-postorder/ | ||||
""" | ||||
|
||||
|
||||
class TreeNode: | ||||
""" | ||||
Class representing a node in a binary tree. | ||||
|
||||
Attributes: | ||||
----------- | ||||
value : int | ||||
The value stored at the node. | ||||
left : TreeNode | ||||
Pointer to the left child node (default is None). | ||||
right : TreeNode | ||||
Pointer to the right child node (default is None). | ||||
""" | ||||
|
||||
def __init__(self, value: int) -> None: | ||||
self.value = value | ||||
self.left: TreeNode | None = None | ||||
self.right: TreeNode | None = None | ||||
|
||||
|
||||
class BinaryTree: | ||||
""" | ||||
Class representing a binary tree. | ||||
|
||||
Methods: | ||||
-------- | ||||
insert(value: int) -> None: | ||||
Insert a value into the binary tree following binary search tree (BST) rules. | ||||
|
||||
morris_postorder_traversal() -> List[int]: | ||||
Perform postorder traversal and return list of node values. | ||||
|
||||
|
||||
>>> bt = BinaryTree() | ||||
>>> bt.insert(9) | ||||
>>> bt.insert(6) | ||||
>>> bt.insert(10) | ||||
>>> bt.insert(3) | ||||
>>> bt.insert(7) | ||||
>>> bt.insert(12) | ||||
>>> bt.insert(2) | ||||
>>> bt.insert(5) | ||||
>>> bt.insert(4) | ||||
>>> bt.morris_postorder_traversal() | ||||
[2, 4, 5, 3, 7, 6, 12, 10, 9] | ||||
|
||||
""" | ||||
|
||||
def __init__(self) -> None: | ||||
self.root: TreeNode | None = None | ||||
|
||||
def insert(self, value: int) -> None: | ||||
""" | ||||
Insert a value into the binary tree. | ||||
|
||||
Parameters: | ||||
----------- | ||||
value : int | ||||
The value to be inserted into the binary tree. | ||||
""" | ||||
if self.root is None: | ||||
self.root = TreeNode(value) | ||||
else: | ||||
self._insert_recursive(self.root, value) | ||||
|
||||
def _insert_recursive(self, node: TreeNode, value: int) -> None: | ||||
""" | ||||
Helper function to insert a value recursively into the tree. | ||||
|
||||
Parameters: | ||||
----------- | ||||
node : TreeNode | ||||
The current node in the binary tree. | ||||
value : int | ||||
The value to be inserted. | ||||
""" | ||||
if value < node.value: | ||||
if node.left is None: | ||||
node.left = TreeNode(value) | ||||
else: | ||||
self._insert_recursive(node.left, value) | ||||
elif node.right is None: | ||||
node.right = TreeNode(value) | ||||
else: | ||||
self._insert_recursive(node.right, value) | ||||
|
||||
def _successor(self, node: TreeNode) -> TreeNode: | ||||
""" | ||||
Helper Function to return successor of the given node in a binary tree | ||||
|
||||
Parameters: | ||||
----------- | ||||
node : TreeNode | ||||
A node in the binary tree. | ||||
|
||||
Returns: | ||||
-------- | ||||
TreeNode: | ||||
The successor of the node passed in the parameter | ||||
""" | ||||
temp_node = node.right | ||||
while temp_node and temp_node.left and temp_node.left != node: | ||||
temp_node = temp_node.left | ||||
assert temp_node is not None, "Successor should not be None" | ||||
return temp_node | ||||
|
||||
def morris_postorder_traversal(self) -> list[int]: | ||||
""" | ||||
Function for postorder traversal using morris postorder traversal. | ||||
|
||||
Algorithm : | ||||
------------ | ||||
First set current node as root node. | ||||
|
||||
while current node is not empty | ||||
If the current node has no right child. | ||||
push the current node to temp. | ||||
point the current node to its left child | ||||
|
||||
else. | ||||
find successor node of the current node. | ||||
if successor left child points to current node, | ||||
remove the link of the left child of successor node. | ||||
point the current node to its left child. | ||||
else, | ||||
push the current node to temp. | ||||
make the current node as the left child of the successor node. | ||||
point current node to its right child. | ||||
|
||||
Reverse the temp array to get postorder traversaol. | ||||
|
||||
Returns: | ||||
-------- | ||||
List[int]: | ||||
A list of integers representing the postorder traversal. | ||||
|
||||
|
||||
|
||||
Comment on lines
+126
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
""" | ||||
postorder_traversal = [] | ||||
current_node = self.root | ||||
|
||||
while current_node: | ||||
if current_node.right is None: | ||||
postorder_traversal.append(current_node.value) | ||||
current_node = current_node.left | ||||
else: | ||||
successor_node = self._successor(current_node) | ||||
if successor_node.left == current_node: | ||||
successor_node.left = current_node | ||||
current_node = current_node.left | ||||
else: | ||||
postorder_traversal.append(current_node.value) | ||||
successor_node.left = current_node | ||||
current_node = current_node.right | ||||
return postorder_traversal[::-1] | ||||
|
||||
|
||||
if __name__ == "__main__": | ||||
import doctest | ||||
|
||||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment on your previous PR: #11774 (comment)