-
-
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
Changes from 32 commits
e3f51e6
64aa843
c59a81c
2ed09ab
5cb6799
56f6ff8
a8a9a6f
c8d12c9
c2caec5
b51c2a2
da601b7
2c4166f
b0f71e9
71f9365
1931aee
922af56
4b7f3c2
c07f0ee
16e1edd
f55280a
599c2d9
c7a79b8
cdc2b4d
199e3fe
e7f0809
ba58a13
79608ab
b8f2bda
6cc0235
81797ab
fd55b9e
74320a1
4132662
924b5b6
9504a42
b356328
a9a35b6
7c1a2f6
5a78f7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
""" | ||
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 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) | ||
(no output) | ||
""" | ||
current = root | ||
while current: | ||
print(current.data, end=" ") | ||
current = current.right | ||
|
||
|
||
def main() -> None: | ||
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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file
Arunsiva003 marked this conversation as resolved.
Show resolved
Hide resolved
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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file |
||
# 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) | ||
Arunsiva003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# 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() |
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