-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
[Binary Tree] Different views of binary tree added #6965
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 22 commits into
TheAlgorithms:master
from
kondekarshubham123:binary_tree/diff_views_of_binary_tree
Oct 17, 2022
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
44b79f4
Different views of binary tree added
kondekarshubham123 70f582d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4b443a8
mypy errors resolved
kondekarshubham123 7f421cd
doc test for remaining functions
kondekarshubham123 b1c0cf2
Flake8 comments resolved
kondekarshubham123 9b20d14
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cff7de0
Example moved in if block
kondekarshubham123 5fff376
doctest cases added
kondekarshubham123 51d1918
Cases from if block removed
kondekarshubham123 d815e9e
Update data_structures/binary_tree/diff_views_of_binary_tree.py
kondekarshubham123 dfeb531
Update data_structures/binary_tree/diff_views_of_binary_tree.py
kondekarshubham123 9f3e60a
PR Comments resolved
kondekarshubham123 70823fd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 588c83b
flake8 warning resolved
kondekarshubham123 1961c3e
Changes revered
kondekarshubham123 c26e54e
flake8 issue resolved
kondekarshubham123 c338298
Merge branch 'TheAlgorithms:master' into binary_tree/diff_views_of_bi…
kondekarshubham123 75b898b
Put the diagrams just above the doctests
cclauss f46ec01
Update diff_views_of_binary_tree.py
cclauss 0cad195
Update diff_views_of_binary_tree.py
cclauss 04ac3eb
I love mypy
cclauss afff3f4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
195 changes: 195 additions & 0 deletions
195
data_structures/binary_tree/diff_views_of_binary_tree.py
This file contains hidden or 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,195 @@ | ||
""" | ||
Problem: | ||
Given root of binary Tree, print the | ||
|
||
1. binary-tree-right-side-view | ||
2. binary-tree-left-side-view | ||
3. binary-tree-top-side-view | ||
4. binary-tree-bottom-side-view | ||
|
||
|
||
1. binary-tree-right-side-view | ||
|
||
3 <- 3 | ||
/ \ | ||
9 20 <- 20 | ||
/ \ | ||
15 7 <- 7 | ||
|
||
Output: [3, 20, 7] | ||
|
||
|
||
2. binary-tree-left-side-view | ||
|
||
3 -> 3 | ||
/ \ | ||
9 -> 9 20 | ||
/ \ | ||
15 -> 15 7 | ||
|
||
Output: [3, 9, 15] | ||
|
||
|
||
3. binary-tree-top-side-view | ||
|
||
9 3 20 7 | ||
⬇ ⬇ ⬇ ⬇ | ||
|
||
3 | ||
/ \ | ||
9 20 | ||
/ \ | ||
15 7 | ||
|
||
Output: [9, 3, 20, 7] | ||
|
||
4. binary-tree-bottom-side-view | ||
|
||
3 | ||
/ \ | ||
9 20 | ||
/ \ | ||
15 7 | ||
↑ ↑ ↑ ↑ | ||
9 15 20 7 | ||
|
||
Output: [9, 15, 20, 7] | ||
|
||
""" | ||
|
||
from __future__ import annotations | ||
|
||
class TreeNode: | ||
def __init__( | ||
self, val: int = 0, left: TreeNode | None = None, right: TreeNode | None = None | ||
) -> None: | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
|
||
def binary_tree_right_side_view(root: TreeNode | None) -> list[int]: | ||
""" | ||
Function returns the right side view of binary tree. | ||
|
||
>>> binary_tree_right_side_view(None) | ||
[] | ||
""" | ||
def dfs(root: TreeNode | None, depth: int, right_view: list[int]) -> None: | ||
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not root: | ||
return | ||
|
||
if depth == len(right_view): | ||
right_view.append(root.val) | ||
|
||
dfs(root.right, depth + 1, right_view) | ||
dfs(root.left, depth + 1, right_view) | ||
|
||
right_view = [] | ||
if not root: | ||
return right_view | ||
dfs(root, 0, right_view) | ||
return right_view | ||
|
||
def binary_tree_left_side_view(root: TreeNode | None) -> list[int]: | ||
""" | ||
Function returns the left side view of binary tree. | ||
|
||
>>> binary_tree_left_side_view(None) | ||
[] | ||
""" | ||
def dfs(root: TreeNode | None, depth: int, left_view: list[int]) -> None: | ||
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not root: | ||
return | ||
|
||
if depth == len(left_view): | ||
left_view.append(root.val) | ||
|
||
dfs(root.left, depth + 1, left_view) | ||
dfs(root.right, depth + 1, left_view) | ||
|
||
left_view = [] | ||
if not root: | ||
return left_view | ||
dfs(root, 0, left_view) | ||
return left_view | ||
|
||
def binary_tree_top_side_view(root: TreeNode | None) -> list[int]: | ||
""" | ||
Function returns the top side view of binary tree. | ||
|
||
>>> binary_tree_top_side_view(None) | ||
[] | ||
""" | ||
from collections import defaultdict | ||
|
||
def bfs(root: TreeNode | None, top_view: list[int]) -> None: | ||
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
queue = [(root, 0)] | ||
lookup = defaultdict(list) | ||
|
||
while queue: | ||
first = queue.pop(0) | ||
node,hd = first | ||
lookup[hd].append(node.val) | ||
|
||
if node.left: | ||
queue.append((node.left, hd - 1)) | ||
if node.right: | ||
queue.append((node.right, hd + 1)) | ||
|
||
for key, val in sorted(lookup.items(), key = lambda x:x[0]): | ||
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
top_view.append(val[0]) | ||
|
||
top_view = [] | ||
if not root: | ||
return top_view | ||
bfs(root, top_view) | ||
return top_view | ||
|
||
def binary_tree_bottom_side_view(root: TreeNode | None) -> list[int]: | ||
""" | ||
Function returns the bottom side view of binary tree | ||
|
||
>>> binary_tree_bottom_side_view(None) | ||
[] | ||
""" | ||
from collections import defaultdict | ||
|
||
def bfs(root: TreeNode | None, bottom_view: list[int]) -> None: | ||
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
queue = [(root, 0)] | ||
lookup = defaultdict(list) | ||
|
||
while queue: | ||
first = queue.pop(0) | ||
node,hd = first | ||
lookup[hd].append(node.val) | ||
|
||
if node.left: | ||
queue.append((node.left, hd - 1)) | ||
if node.right: | ||
queue.append((node.right, hd + 1)) | ||
|
||
for key, val in sorted(lookup.items(), key = lambda x:x[0]): | ||
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bottom_view.append(val[-1]) | ||
|
||
bottom_view = [] | ||
if not root: | ||
return bottom_view | ||
bfs(root, bottom_view) | ||
return bottom_view | ||
|
||
|
||
tree_1 = TreeNode(3) | ||
tree_1.left = TreeNode(9) | ||
tree_1.right = TreeNode(20) | ||
tree_1.right.left = TreeNode(15) | ||
tree_1.right.right = TreeNode(7) | ||
|
||
print(binary_tree_right_side_view(tree_1)) # Output: [3, 20, 7] | ||
print(binary_tree_left_side_view(tree_1)) # Output: [3, 9, 15] | ||
print(binary_tree_top_side_view(tree_1)) # Output: [9, 3, 20, 7] | ||
print(binary_tree_bottom_side_view(tree_1)) # Output: [9, 15, 20, 7] | ||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
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.
Uh oh!
There was an error while loading. Please reload this page.