Skip to content

[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
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
44b79f4
Different views of binary tree added
kondekarshubham123 Oct 11, 2022
70f582d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 11, 2022
4b443a8
mypy errors resolved
kondekarshubham123 Oct 11, 2022
7f421cd
doc test for remaining functions
kondekarshubham123 Oct 11, 2022
b1c0cf2
Flake8 comments resolved
kondekarshubham123 Oct 11, 2022
9b20d14
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 11, 2022
cff7de0
Example moved in if block
kondekarshubham123 Oct 11, 2022
5fff376
doctest cases added
kondekarshubham123 Oct 11, 2022
51d1918
Cases from if block removed
kondekarshubham123 Oct 11, 2022
d815e9e
Update data_structures/binary_tree/diff_views_of_binary_tree.py
kondekarshubham123 Oct 17, 2022
dfeb531
Update data_structures/binary_tree/diff_views_of_binary_tree.py
kondekarshubham123 Oct 17, 2022
9f3e60a
PR Comments resolved
kondekarshubham123 Oct 17, 2022
70823fd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 17, 2022
588c83b
flake8 warning resolved
kondekarshubham123 Oct 17, 2022
1961c3e
Changes revered
kondekarshubham123 Oct 17, 2022
c26e54e
flake8 issue resolved
kondekarshubham123 Oct 17, 2022
c338298
Merge branch 'TheAlgorithms:master' into binary_tree/diff_views_of_bi…
kondekarshubham123 Oct 17, 2022
75b898b
Put the diagrams just above the doctests
cclauss Oct 17, 2022
f46ec01
Update diff_views_of_binary_tree.py
cclauss Oct 17, 2022
0cad195
Update diff_views_of_binary_tree.py
cclauss Oct 17, 2022
04ac3eb
I love mypy
cclauss Oct 17, 2022
afff3f4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions data_structures/binary_tree/diff_views_of_binary_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
r"""
Problem: Given root of a binary tree, return 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
"""

from __future__ import annotations

from collections import defaultdict
from dataclasses import dataclass


@dataclass
class TreeNode:
val: int
left: TreeNode | None = None
right: TreeNode | None = None


def make_tree() -> TreeNode:
"""
>>> make_tree().val
3
"""
return TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7)))


def binary_tree_right_side_view(root: TreeNode) -> list[int]:
r"""
Function returns the right side view of binary tree.

3 <- 3
/ \
9 20 <- 20
/ \
15 7 <- 7

>>> binary_tree_right_side_view(make_tree())
[3, 20, 7]
>>> binary_tree_right_side_view(None)
[]
"""

def depth_first_search(root: TreeNode, depth: int, right_view: list[int]) -> None:

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function depth_first_search

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function depth_first_search

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function depth_first_search

"""
A depth first search preorder traversal to append the values at
right side of tree.
"""
if not root:
return

if depth == len(right_view):
right_view.append(root.val)

depth_first_search(root.right, depth + 1, right_view)
depth_first_search(root.left, depth + 1, right_view)

right_view: list = []
if not root:
return right_view

depth_first_search(root, 0, right_view)
return right_view


def binary_tree_left_side_view(root: TreeNode) -> list[int]:
r"""
Function returns the left side view of binary tree.

3 -> 3
/ \
9 -> 9 20
/ \
15 -> 15 7

>>> binary_tree_left_side_view(make_tree())
[3, 9, 15]
>>> binary_tree_left_side_view(None)
[]
"""

def depth_first_search(root: TreeNode, depth: int, left_view: list[int]) -> None:

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function depth_first_search

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function depth_first_search

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function depth_first_search

"""
A depth first search preorder traversal to append the values
at left side of tree.
"""
if not root:
return

if depth == len(left_view):
left_view.append(root.val)

depth_first_search(root.left, depth + 1, left_view)
depth_first_search(root.right, depth + 1, left_view)

left_view: list = []
if not root:
return left_view

depth_first_search(root, 0, left_view)
return left_view


def binary_tree_top_side_view(root: TreeNode) -> list[int]:
r"""
Function returns the top side view of binary tree.

9 3 20 7
⬇ ⬇ ⬇ ⬇

3
/ \
9 20
/ \
15 7

>>> binary_tree_top_side_view(make_tree())
[9, 3, 20, 7]
>>> binary_tree_top_side_view(None)
[]
"""

def breadth_first_search(root: TreeNode, top_view: list[int]) -> None:

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function breadth_first_search

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function breadth_first_search

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function breadth_first_search

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function breadth_first_search

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function breadth_first_search

"""
A breadth first search traversal with defaultdict ds to append
the values of tree from top view
"""
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 pair in sorted(lookup.items(), key=lambda each: each[0]):
top_view.append(pair[1][0])

top_view: list = []
if not root:
return top_view

breadth_first_search(root, top_view)
return top_view


def binary_tree_bottom_side_view(root: TreeNode) -> list[int]:
r"""
Function returns the bottom side view of binary tree

3
/ \
9 20
/ \
15 7
↑ ↑ ↑ ↑
9 15 20 7

>>> binary_tree_bottom_side_view(make_tree())
[9, 15, 20, 7]
>>> binary_tree_bottom_side_view(None)
[]
"""
from collections import defaultdict

def breadth_first_search(root: TreeNode, bottom_view: list[int]) -> None:

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function breadth_first_search

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function breadth_first_search

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhruvmanila doctests do not run on functions inside functions so the keeperbot should not expect to find them.

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function breadth_first_search

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function breadth_first_search

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/diff_views_of_binary_tree.py, please provide doctest for the function breadth_first_search

"""
A breadth first search traversal with defaultdict ds to append
the values of tree from bottom view
"""
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 pair in sorted(lookup.items(), key=lambda each: each[0]):
bottom_view.append(pair[1][-1])

bottom_view: list = []
if not root:
return bottom_view

breadth_first_search(root, bottom_view)
return bottom_view


if __name__ == "__main__":
import doctest

doctest.testmod()