Compare commits

...

7 Commits

Author SHA1 Message Date
Kuntal Gorai
669967da84
Merge 7e010687c4 into f3f32ae3ca 2024-11-19 12:01:23 +05:30
pre-commit-ci[bot]
f3f32ae3ca
[pre-commit.ci] pre-commit autoupdate (#12385)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.3 → v0.7.4](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.3...v0.7.4)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-11-18 22:07:12 +01:00
kuntal0901
7e010687c4 fix: code review changes 2024-10-05 23:44:30 +05:30
kuntal0901
31bc93f1a2 feat: Implemented Morris postorder traversal for Binary tree 2024-10-05 19:22:01 +05:30
kuntal0901
f08e77f6aa deleting unecessary files 2024-10-05 19:19:06 +05:30
kuntal0901
c6f14ee860 feat: Implemented Morris postorder traversal for Binary tree 2024-10-05 19:17:09 +05:30
kuntal0901
e4a9f543a0 feat: Implemented Morris Inorder traversal for Binary tree 2024-10-05 18:40:46 +05:30
2 changed files with 153 additions and 1 deletions

View File

@ -16,7 +16,7 @@ repos:
- id: auto-walrus - id: auto-walrus
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.3 rev: v0.7.4
hooks: hooks:
- id: ruff - id: ruff
- id: ruff-format - id: ruff-format

View File

@ -0,0 +1,152 @@
"""
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 : The value stored at the node.
left : Pointer to the left child node (default is None).
right : 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.
>>> 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 : 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 : The current node in the binary tree.
value : 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 : A node in the binary tree.
Returns:
--------
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:
--------
A list of integers representing the postorder traversal.
"""
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()