Skip to content

Commit a9dfc98

Browse files
Arunsiva003pre-commit-ci[bot]cclauss
authored andcommitted
Arunsiva003 patch 1 flatten tree (TheAlgorithms#9695)
* infix to prefix missing feature added * infix to prefix missing feature added * infix to prefix missing feature added * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * infix to prefix missing feature added (comments) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * infix to prefix missing feature added (comments) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * newly updated infix_to_prefix * newly updated infix_to_prefix_2 * newly updated infix_to_prefix_3 * from the beginning * Created flatten_binarytree_to_linkedlist.py * Update flatten_binarytree_to_linkedlist.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update flatten_binarytree_to_linkedlist.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update flatten_binarytree_to_linkedlist.py * Update flatten_binarytree_to_linkedlist.py * Update flatten_binarytree_to_linkedlist.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update flatten_binarytree_to_linkedlist.py (space added) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update flatten_binarytree_to_linkedlist.py space added * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update flatten_binarytree_to_linkedlist.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * flatten binary tree to linked list - 1 * flatten binary tree to linked list final * flatten binary tree to linked list final * review updated * Update flatten_binarytree_to_linkedlist.py * Update .pre-commit-config.yaml * Update flatten_binarytree_to_linkedlist.py * Update flatten_binarytree_to_linkedlist.py --------- Co-authored-by: ArunSiva <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 9055c8c commit a9dfc98

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
"""
2+
Binary Tree Flattening Algorithm
3+
4+
This code defines an algorithm to flatten a binary tree into a linked list
5+
represented using the right pointers of the tree nodes. It uses in-place
6+
flattening and demonstrates the flattening process along with a display
7+
function to visualize the flattened linked list.
8+
https://www.geeksforgeeks.org/flatten-a-binary-tree-into-linked-list
9+
10+
Author: Arunkumar A
11+
Date: 04/09/2023
12+
"""
13+
from __future__ import annotations
14+
15+
16+
class TreeNode:
17+
"""
18+
A TreeNode has data variable and pointers to TreeNode objects
19+
for its left and right children.
20+
"""
21+
22+
def __init__(self, data: int) -> None:
23+
self.data = data
24+
self.left: TreeNode | None = None
25+
self.right: TreeNode | None = None
26+
27+
28+
def build_tree() -> TreeNode:
29+
"""
30+
Build and return a sample binary tree.
31+
32+
Returns:
33+
TreeNode: The root of the binary tree.
34+
35+
Examples:
36+
>>> root = build_tree()
37+
>>> root.data
38+
1
39+
>>> root.left.data
40+
2
41+
>>> root.right.data
42+
5
43+
>>> root.left.left.data
44+
3
45+
>>> root.left.right.data
46+
4
47+
>>> root.right.right.data
48+
6
49+
"""
50+
root = TreeNode(1)
51+
root.left = TreeNode(2)
52+
root.right = TreeNode(5)
53+
root.left.left = TreeNode(3)
54+
root.left.right = TreeNode(4)
55+
root.right.right = TreeNode(6)
56+
return root
57+
58+
59+
def flatten(root: TreeNode | None) -> None:
60+
"""
61+
Flatten a binary tree into a linked list in-place, where the linked list is
62+
represented using the right pointers of the tree nodes.
63+
64+
Args:
65+
root (TreeNode): The root of the binary tree to be flattened.
66+
67+
Examples:
68+
>>> root = TreeNode(1)
69+
>>> root.left = TreeNode(2)
70+
>>> root.right = TreeNode(5)
71+
>>> root.left.left = TreeNode(3)
72+
>>> root.left.right = TreeNode(4)
73+
>>> root.right.right = TreeNode(6)
74+
>>> flatten(root)
75+
>>> root.data
76+
1
77+
>>> root.right.right is None
78+
False
79+
>>> root.right.right = TreeNode(3)
80+
>>> root.right.right.right is None
81+
True
82+
"""
83+
if not root:
84+
return
85+
86+
# Flatten the left subtree
87+
flatten(root.left)
88+
89+
# Save the right subtree
90+
right_subtree = root.right
91+
92+
# Make the left subtree the new right subtree
93+
root.right = root.left
94+
root.left = None
95+
96+
# Find the end of the new right subtree
97+
current = root
98+
while current.right:
99+
current = current.right
100+
101+
# Append the original right subtree to the end
102+
current.right = right_subtree
103+
104+
# Flatten the updated right subtree
105+
flatten(right_subtree)
106+
107+
108+
def display_linked_list(root: TreeNode | None) -> None:
109+
"""
110+
Display the flattened linked list.
111+
112+
Args:
113+
root (TreeNode | None): The root of the flattened linked list.
114+
115+
Examples:
116+
>>> root = TreeNode(1)
117+
>>> root.right = TreeNode(2)
118+
>>> root.right.right = TreeNode(3)
119+
>>> display_linked_list(root)
120+
1 2 3
121+
>>> root = None
122+
>>> display_linked_list(root)
123+
124+
"""
125+
current = root
126+
while current:
127+
if current.right is None:
128+
print(current.data, end="")
129+
break
130+
print(current.data, end=" ")
131+
current = current.right
132+
133+
134+
if __name__ == "__main__":
135+
print("Flattened Linked List:")
136+
root = build_tree()
137+
flatten(root)
138+
display_linked_list(root)

0 commit comments

Comments
 (0)