-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
feat: Binary tree node sum (#7020) #7162
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7892bf8
feat: Binary tree node sum (#7020)
CaedenPH c515a5f
feat: Sum of all nodes in binary tree explanation (#7020)
CaedenPH 6d50bed
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 413f4f9
Update data_structures/binary_tree/binary_tree_node_sum.py
CaedenPH c9078c7
refactor: Change replace method with `__iter__` overriding (#7020)
CaedenPH 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
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
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,76 @@ | ||
""" | ||
Sum of all nodes in a binary tree. | ||
|
||
Python implementation: | ||
O(n) time complexity - Recurses through :meth:`depth_first_search` | ||
with each element. | ||
O(n) space complexity - At any point in time maximum number of stack | ||
frames that could be in memory is `n` | ||
""" | ||
|
||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Iterator | ||
|
||
|
||
class Node: | ||
""" | ||
A Node has a value variable and pointers to Nodes to its left and right. | ||
""" | ||
|
||
def __init__(self, value: int) -> None: | ||
self.value = value | ||
self.left: Node | None = None | ||
self.right: Node | None = None | ||
|
||
|
||
class BinaryTreeNodeSum: | ||
r""" | ||
The below tree looks like this | ||
10 | ||
/ \ | ||
5 -3 | ||
/ / \ | ||
12 8 0 | ||
|
||
>>> tree = Node(10) | ||
>>> sum(BinaryTreeNodeSum(tree)) | ||
10 | ||
|
||
>>> tree.left = Node(5) | ||
>>> sum(BinaryTreeNodeSum(tree)) | ||
15 | ||
|
||
>>> tree.right = Node(-3) | ||
>>> sum(BinaryTreeNodeSum(tree)) | ||
12 | ||
|
||
>>> tree.left.left = Node(12) | ||
>>> sum(BinaryTreeNodeSum(tree)) | ||
24 | ||
|
||
>>> tree.right.left = Node(8) | ||
>>> tree.right.right = Node(0) | ||
>>> sum(BinaryTreeNodeSum(tree)) | ||
32 | ||
""" | ||
|
||
def __init__(self, tree: Node) -> None: | ||
self.tree = tree | ||
|
||
def depth_first_search(self, node: Node | None) -> int: | ||
if node is None: | ||
return 0 | ||
return node.value + ( | ||
self.depth_first_search(node.left) + self.depth_first_search(node.right) | ||
) | ||
|
||
def __iter__(self) -> Iterator[int]: | ||
yield self.depth_first_search(self.tree) | ||
|
||
|
||
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.
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.
from collections.abc import Iterator
Extra credit:
def __iter__(self) -> Iterator[int]
https://docs.python.org/3/reference/datamodel.html#object.__iter__
https://github.com/TheAlgorithms/Python/search?q=__iter__
Then you could replace
node_sum()
withsum(binary_tree_node_sum)
.