Skip to content

feat: Implemented Morris Inorder traversal for Binary tree #11774

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
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: code review changes
  • Loading branch information
kuntal0901 committed Oct 5, 2024
commit 727686ed7a42359190893a40b2d5e93258c8b234
36 changes: 9 additions & 27 deletions data_structures/binary_tree/morris_inorder_traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ class TreeNode:

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).
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:
Expand All @@ -30,15 +27,6 @@ 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_inorder_traversal() -> List[int]:
Perform inorder traversal and return list of node values.


>>> bt = BinaryTree()
>>> bt.insert(9)
>>> bt.insert(6)
Expand All @@ -63,8 +51,7 @@ def insert(self, value: int) -> None:

Parameters:
-----------
value : int
The value to be inserted into the binary tree.
value : The value to be inserted into the binary tree.
"""
if self.root is None:
self.root = TreeNode(value)
Expand All @@ -77,10 +64,8 @@ def _insert_recursive(self, node: TreeNode, value: int) -> None:

Parameters:
-----------
node : TreeNode
The current node in the binary tree.
value : int
The value to be inserted.
node : The current node in the binary tree.
value : The value to be inserted.
"""
if value < node.value:
if node.left is None:
Expand All @@ -98,13 +83,11 @@ def _predecessor(self, node: TreeNode) -> TreeNode:

Parameters:
-----------
node : TreeNode
A node in the binary tree.
node : A node in the binary tree.

Returns:
--------
TreeNode:
The predecessor of the node passed in the parameter
The predecessor of the node passed in the parameter
"""
temp_node = node.left
while temp_node and temp_node.right and temp_node.right != node:
Expand Down Expand Up @@ -137,8 +120,7 @@ def morris_inorder_traversal(self) -> list[int]:

Returns:
--------
List[int]:
A list of integers representing the inorder traversal.
A list of integers representing the inorder traversal.



Expand Down