Skip to content

Arunsiva003 patch 1 flatten tree #9695

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

Merged
Merged
Changes from 19 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
e3f51e6
infix to prefix missing feature added
Arunsiva003 Oct 2, 2023
64aa843
infix to prefix missing feature added
Arunsiva003 Oct 2, 2023
c59a81c
infix to prefix missing feature added
Arunsiva003 Oct 2, 2023
2ed09ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2023
5cb6799
infix to prefix missing feature added (comments)
Arunsiva003 Oct 2, 2023
56f6ff8
Merge branch 'master' of https://github.com/Arunsiva003/Python
Arunsiva003 Oct 2, 2023
a8a9a6f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2023
c8d12c9
infix to prefix missing feature added (comments)
Arunsiva003 Oct 2, 2023
c2caec5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2023
b51c2a2
newly updated infix_to_prefix
Arunsiva003 Oct 3, 2023
da601b7
Merge branch 'master' of https://github.com/Arunsiva003/Python
Arunsiva003 Oct 3, 2023
2c4166f
newly updated infix_to_prefix_2
Arunsiva003 Oct 3, 2023
b0f71e9
newly updated infix_to_prefix_3
Arunsiva003 Oct 3, 2023
71f9365
Merge branch 'master' of https://github.com/Arunsiva003/Python
Arunsiva003 Oct 4, 2023
1931aee
from the beginning
Arunsiva003 Oct 4, 2023
922af56
Created flatten_binarytree_to_linkedlist.py
Arunsiva003 Oct 4, 2023
4b7f3c2
Update flatten_binarytree_to_linkedlist.py
Arunsiva003 Oct 4, 2023
c07f0ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
16e1edd
Update flatten_binarytree_to_linkedlist.py
Arunsiva003 Oct 4, 2023
f55280a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
599c2d9
Update flatten_binarytree_to_linkedlist.py
Arunsiva003 Oct 4, 2023
c7a79b8
Update flatten_binarytree_to_linkedlist.py
Arunsiva003 Oct 4, 2023
cdc2b4d
Update flatten_binarytree_to_linkedlist.py
Arunsiva003 Oct 4, 2023
199e3fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
e7f0809
Update flatten_binarytree_to_linkedlist.py (space added)
Arunsiva003 Oct 4, 2023
ba58a13
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
79608ab
Update flatten_binarytree_to_linkedlist.py space added
Arunsiva003 Oct 4, 2023
b8f2bda
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
6cc0235
Update flatten_binarytree_to_linkedlist.py
Arunsiva003 Oct 4, 2023
81797ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
fd55b9e
flatten binary tree to linked list - 1
Arunsiva003 Oct 4, 2023
74320a1
Merge branch 'Arunsiva003-patch-1-flattenTree' of https://github.com/…
Arunsiva003 Oct 4, 2023
4132662
flatten binary tree to linked list final
Arunsiva003 Oct 4, 2023
924b5b6
flatten binary tree to linked list final
Arunsiva003 Oct 4, 2023
9504a42
review updated
Arunsiva003 Oct 4, 2023
b356328
Update flatten_binarytree_to_linkedlist.py
cclauss Oct 4, 2023
a9a35b6
Update .pre-commit-config.yaml
cclauss Oct 4, 2023
7c1a2f6
Update flatten_binarytree_to_linkedlist.py
cclauss Oct 4, 2023
5a78f7d
Update flatten_binarytree_to_linkedlist.py
cclauss Oct 4, 2023
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
126 changes: 126 additions & 0 deletions data_structures/binary_tree/flatten_binarytree_to_linkedlist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
"""
Binary Tree Flattening Algorithm

This code defines an algorithm to flatten a binary tree into a linked list
represented using the right pointers of the tree nodes. It uses in-place
flattening and demonstrates the flattening process along with a display
function to visualize the flattened linked list.
https://www.geeksforgeeks.org/flatten-a-binary-tree-into-linked-list

Author: Arunkumar A
Date: 04/09/2023
"""
from __future__ import annotations

class TreeNode:
"""
A TreeNode has data variable and pointers to TreeNode objects
for its left and right children.
"""

def __init__(self, data: int) -> None:
self.data = data
self.left: TreeNode | None = None
self.right: TreeNode | None = None


def flatten(root: TreeNode | None) -> None:
"""
Flatten a binary tree into a linked list in-place, where the linked list is
represented using the right pointers of the tree nodes.

Args:
root (TreeNode): The root of the binary tree to be flattened.

Examples:
>>> root = TreeNode(1)
>>> root.left = TreeNode(2)
>>> root.right = TreeNode(5)
>>> root.left.left = TreeNode(3)
>>> root.left.right = TreeNode(4)
>>> root.right.right = TreeNode(6)
>>> flatten(root)
>>> root.data
1
>>> root.right is None
True
>>> root.right = TreeNode(2)
>>> root.right.data
2
>>> root.right.right is None
True
>>> root.right.right = TreeNode(3)
>>> root.right.right.data
3
>>> root.right.right.right is None
True

"""
if not root:
return

# Flatten the left subtree
flatten(root.left)

# Save the right subtree
right_subtree = root.right

# Make the left subtree the new right subtree
root.right = root.left
root.left = None

# Find the end of the new right subtree
current = root
while current.right:
current = current.right

# Append the original right subtree to the end
current.right = right_subtree

# Flatten the updated right subtree
flatten(right_subtree)


def display_linked_list(root: TreeNode | None) -> None:
"""
Display the flattened linked list.

Args:
root (TreeNode | None): The root of the flattened linked list.

Examples:
>>> root = TreeNode(1)
>>> root.right = TreeNode(2)
>>> root.right.right = TreeNode(3)
>>> display_linked_list(root)
1 2 3

>>> root = None
>>> display_linked_list(root)
(no output)
"""
current = root
while current:
print(current.data, end=" ")
current = current.right


def main() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/flatten_binarytree_to_linkedlist.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/flatten_binarytree_to_linkedlist.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/flatten_binarytree_to_linkedlist.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/flatten_binarytree_to_linkedlist.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/flatten_binarytree_to_linkedlist.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/flatten_binarytree_to_linkedlist.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/flatten_binarytree_to_linkedlist.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/flatten_binarytree_to_linkedlist.py, please provide doctest for the function main

# Create a sample binary tree
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(5)
root.left.left = TreeNode(3)
root.left.right = TreeNode(4)
root.right.right = TreeNode(6)

# Flatten the binary tree to a linked list
flatten(root)

# Display the flattened linked list to verify correctness
print("Flattened Linked List:")
display_linked_list(root)


if __name__ == "__main__":
main()