Skip to content

Commit 3448ae5

Browse files
kondekarshubham123pre-commit-ci[bot]cclauss
authored
[Binary Tree] Different views of binary tree added (TheAlgorithms#6965)
* Different views of binary tree added * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * mypy errors resolved * doc test for remaining functions * Flake8 comments resolved * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Example moved in if block * doctest cases added * Cases from if block removed * Update data_structures/binary_tree/diff_views_of_binary_tree.py Co-authored-by: Christian Clauss <[email protected]> * Update data_structures/binary_tree/diff_views_of_binary_tree.py Co-authored-by: Christian Clauss <[email protected]> * PR Comments resolved * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * flake8 warning resolved * Changes revered * flake8 issue resolved * Put the diagrams just above the doctests * Update diff_views_of_binary_tree.py * Update diff_views_of_binary_tree.py * I love mypy * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci 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 0c7c5fa commit 3448ae5

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
r"""
2+
Problem: Given root of a binary tree, return the:
3+
1. binary-tree-right-side-view
4+
2. binary-tree-left-side-view
5+
3. binary-tree-top-side-view
6+
4. binary-tree-bottom-side-view
7+
"""
8+
9+
from __future__ import annotations
10+
11+
from collections import defaultdict
12+
from dataclasses import dataclass
13+
14+
15+
@dataclass
16+
class TreeNode:
17+
val: int
18+
left: TreeNode | None = None
19+
right: TreeNode | None = None
20+
21+
22+
def make_tree() -> TreeNode:
23+
"""
24+
>>> make_tree().val
25+
3
26+
"""
27+
return TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7)))
28+
29+
30+
def binary_tree_right_side_view(root: TreeNode) -> list[int]:
31+
r"""
32+
Function returns the right side view of binary tree.
33+
34+
3 <- 3
35+
/ \
36+
9 20 <- 20
37+
/ \
38+
15 7 <- 7
39+
40+
>>> binary_tree_right_side_view(make_tree())
41+
[3, 20, 7]
42+
>>> binary_tree_right_side_view(None)
43+
[]
44+
"""
45+
46+
def depth_first_search(
47+
root: TreeNode | None, depth: int, right_view: list[int]
48+
) -> None:
49+
"""
50+
A depth first search preorder traversal to append the values at
51+
right side of tree.
52+
"""
53+
if not root:
54+
return
55+
56+
if depth == len(right_view):
57+
right_view.append(root.val)
58+
59+
depth_first_search(root.right, depth + 1, right_view)
60+
depth_first_search(root.left, depth + 1, right_view)
61+
62+
right_view: list = []
63+
if not root:
64+
return right_view
65+
66+
depth_first_search(root, 0, right_view)
67+
return right_view
68+
69+
70+
def binary_tree_left_side_view(root: TreeNode) -> list[int]:
71+
r"""
72+
Function returns the left side view of binary tree.
73+
74+
3 -> 3
75+
/ \
76+
9 -> 9 20
77+
/ \
78+
15 -> 15 7
79+
80+
>>> binary_tree_left_side_view(make_tree())
81+
[3, 9, 15]
82+
>>> binary_tree_left_side_view(None)
83+
[]
84+
"""
85+
86+
def depth_first_search(
87+
root: TreeNode | None, depth: int, left_view: list[int]
88+
) -> None:
89+
"""
90+
A depth first search preorder traversal to append the values
91+
at left side of tree.
92+
"""
93+
if not root:
94+
return
95+
96+
if depth == len(left_view):
97+
left_view.append(root.val)
98+
99+
depth_first_search(root.left, depth + 1, left_view)
100+
depth_first_search(root.right, depth + 1, left_view)
101+
102+
left_view: list = []
103+
if not root:
104+
return left_view
105+
106+
depth_first_search(root, 0, left_view)
107+
return left_view
108+
109+
110+
def binary_tree_top_side_view(root: TreeNode) -> list[int]:
111+
r"""
112+
Function returns the top side view of binary tree.
113+
114+
9 3 20 7
115+
⬇ ⬇ ⬇ ⬇
116+
117+
3
118+
/ \
119+
9 20
120+
/ \
121+
15 7
122+
123+
>>> binary_tree_top_side_view(make_tree())
124+
[9, 3, 20, 7]
125+
>>> binary_tree_top_side_view(None)
126+
[]
127+
"""
128+
129+
def breadth_first_search(root: TreeNode, top_view: list[int]) -> None:
130+
"""
131+
A breadth first search traversal with defaultdict ds to append
132+
the values of tree from top view
133+
"""
134+
queue = [(root, 0)]
135+
lookup = defaultdict(list)
136+
137+
while queue:
138+
first = queue.pop(0)
139+
node, hd = first
140+
141+
lookup[hd].append(node.val)
142+
143+
if node.left:
144+
queue.append((node.left, hd - 1))
145+
if node.right:
146+
queue.append((node.right, hd + 1))
147+
148+
for pair in sorted(lookup.items(), key=lambda each: each[0]):
149+
top_view.append(pair[1][0])
150+
151+
top_view: list = []
152+
if not root:
153+
return top_view
154+
155+
breadth_first_search(root, top_view)
156+
return top_view
157+
158+
159+
def binary_tree_bottom_side_view(root: TreeNode) -> list[int]:
160+
r"""
161+
Function returns the bottom side view of binary tree
162+
163+
3
164+
/ \
165+
9 20
166+
/ \
167+
15 7
168+
↑ ↑ ↑ ↑
169+
9 15 20 7
170+
171+
>>> binary_tree_bottom_side_view(make_tree())
172+
[9, 15, 20, 7]
173+
>>> binary_tree_bottom_side_view(None)
174+
[]
175+
"""
176+
from collections import defaultdict
177+
178+
def breadth_first_search(root: TreeNode, bottom_view: list[int]) -> None:
179+
"""
180+
A breadth first search traversal with defaultdict ds to append
181+
the values of tree from bottom view
182+
"""
183+
queue = [(root, 0)]
184+
lookup = defaultdict(list)
185+
186+
while queue:
187+
first = queue.pop(0)
188+
node, hd = first
189+
lookup[hd].append(node.val)
190+
191+
if node.left:
192+
queue.append((node.left, hd - 1))
193+
if node.right:
194+
queue.append((node.right, hd + 1))
195+
196+
for pair in sorted(lookup.items(), key=lambda each: each[0]):
197+
bottom_view.append(pair[1][-1])
198+
199+
bottom_view: list = []
200+
if not root:
201+
return bottom_view
202+
203+
breadth_first_search(root, bottom_view)
204+
return bottom_view
205+
206+
207+
if __name__ == "__main__":
208+
import doctest
209+
210+
doctest.testmod()

0 commit comments

Comments
 (0)