-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
cclauss
merged 39 commits into
TheAlgorithms:master
from
Arunsiva003:Arunsiva003-patch-1-flattenTree
Oct 4, 2023
Merged
Changes from 36 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
e3f51e6
infix to prefix missing feature added
Arunsiva003 64aa843
infix to prefix missing feature added
Arunsiva003 c59a81c
infix to prefix missing feature added
Arunsiva003 2ed09ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5cb6799
infix to prefix missing feature added (comments)
Arunsiva003 56f6ff8
Merge branch 'master' of https://github.com/Arunsiva003/Python
Arunsiva003 a8a9a6f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c8d12c9
infix to prefix missing feature added (comments)
Arunsiva003 c2caec5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b51c2a2
newly updated infix_to_prefix
Arunsiva003 da601b7
Merge branch 'master' of https://github.com/Arunsiva003/Python
Arunsiva003 2c4166f
newly updated infix_to_prefix_2
Arunsiva003 b0f71e9
newly updated infix_to_prefix_3
Arunsiva003 71f9365
Merge branch 'master' of https://github.com/Arunsiva003/Python
Arunsiva003 1931aee
from the beginning
Arunsiva003 922af56
Created flatten_binarytree_to_linkedlist.py
Arunsiva003 4b7f3c2
Update flatten_binarytree_to_linkedlist.py
Arunsiva003 c07f0ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 16e1edd
Update flatten_binarytree_to_linkedlist.py
Arunsiva003 f55280a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 599c2d9
Update flatten_binarytree_to_linkedlist.py
Arunsiva003 c7a79b8
Update flatten_binarytree_to_linkedlist.py
Arunsiva003 cdc2b4d
Update flatten_binarytree_to_linkedlist.py
Arunsiva003 199e3fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e7f0809
Update flatten_binarytree_to_linkedlist.py (space added)
Arunsiva003 ba58a13
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 79608ab
Update flatten_binarytree_to_linkedlist.py space added
Arunsiva003 b8f2bda
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6cc0235
Update flatten_binarytree_to_linkedlist.py
Arunsiva003 81797ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fd55b9e
flatten binary tree to linked list - 1
Arunsiva003 74320a1
Merge branch 'Arunsiva003-patch-1-flattenTree' of https://github.com/…
Arunsiva003 4132662
flatten binary tree to linked list final
Arunsiva003 924b5b6
flatten binary tree to linked list final
Arunsiva003 9504a42
review updated
Arunsiva003 b356328
Update flatten_binarytree_to_linkedlist.py
cclauss a9a35b6
Update .pre-commit-config.yaml
cclauss 7c1a2f6
Update flatten_binarytree_to_linkedlist.py
cclauss 5a78f7d
Update flatten_binarytree_to_linkedlist.py
cclauss 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
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
136 changes: 136 additions & 0 deletions
136
data_structures/binary_tree/flatten_binarytree_to_linkedlist.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,136 @@ | ||
""" | ||
Arunsiva003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 build_tree() -> TreeNode: | ||
""" | ||
Build and return a sample binary tree. | ||
|
||
Returns: | ||
TreeNode: The root of the binary tree. | ||
|
||
Examples: | ||
>>> root = build_tree() | ||
>>> root.data | ||
1 | ||
>>> root.left.data | ||
2 | ||
>>> root.right.data | ||
5 | ||
>>> root.left.left.data | ||
3 | ||
>>> root.left.right.data | ||
4 | ||
>>> root.right.right.data | ||
6 | ||
""" | ||
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) | ||
return root | ||
|
||
|
||
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.right is None | ||
False | ||
>>> root.right.right = TreeNode(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) | ||
|
||
""" | ||
current = root | ||
while current: | ||
if current.right is None: | ||
print(current.data, end="") | ||
break | ||
print(current.data, end=" ") | ||
current = current.right | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"Flattened Linked List:") | ||
display_linked_list(flatten(build_tree())) |
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.
Revert.
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.
why am i getting error as
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases
for the validate filenames checking??
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.
What happens when you
run python
in the command prompt?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.
It goes to script mode
I even checked python --version
And it gave like 3.11.4