feat: Binary tree node sum (#7020) by CaedenPH · Pull Request #7162 · TheAlgorithms/Python · GitHub
Skip to content

feat: Binary tree node sum (#7020) - #7162

Merged
cclauss merged 5 commits into
TheAlgorithms:masterfrom
CaedenPH:binary-tree-node-sum
Oct 15, 2022
Merged

cclauss merged 5 commits into
TheAlgorithms:masterfrom
CaedenPH:binary-tree-node-sum

Conversation

@CaedenPH

@CaedenPH CaedenPH commented Oct 14, 2022

Copy link
Copy Markdown
Contributor

Describe your change:

Implements #7020
Algorithm to get the sum of all tree nodes

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Oct 14, 2022
@CaedenPH

Copy link
Copy Markdown
Contributor Author

@cclauss cclauss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let me know if you want to try the extra credit bit or not.

Comment thread data_structures/binary_tree/binary_tree_node_sum.py Outdated
def __init__(self, tree: Node) -> None:
self.tree = tree

def depth_first_search(self, node: Node | None) -> int:

@cclauss cclauss Oct 14, 2022

Copy link
Copy Markdown
Member

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]

Then you could replace node_sum() with sum(binary_tree_node_sum).

@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Oct 14, 2022
Co-authored-by: Christian Clauss <cclauss@me.com>
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Oct 14, 2022
@CaedenPH

Copy link
Copy Markdown
Contributor Author

@cclauss
Is this what you meant?

@dhruvmanila

Copy link
Copy Markdown
Member

Is this what you meant?

No, that's not what he meant. He's asking you to implement the iteration protocol for a custom Python object. It comprises of two methods __iter__ to which returns an iterator and __next__ which returns the next object.

These methods would be made available to the Node class which then would allow us to do:

sum(node.value for node in tree)

If you need a bit more understanding, I could help you out with an example.

A class for doing a sum seems redundant. Alternatively, you could just create a function instead.

@cclauss

cclauss commented Oct 15, 2022

Copy link
Copy Markdown
Member

list(binary_tree_node_sum) would return [10, 5, 12, -3, 8, 0]

sum(binary_tree_node_sum) would return 32.

max(), min(), mean() would all just work out-of-the-box.

I would just define __iter__ and not define __next__ but whatever works for you. I would use yield more than return. There are lots of examples in this repo that I linked to above.

@CaedenPH

Copy link
Copy Markdown
Contributor Author

@cclauss
cclauss merged commit 98a4c24 into TheAlgorithms:master Oct 15, 2022
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Oct 15, 2022
@CaedenPH
CaedenPH deleted the binary-tree-node-sum branch October 16, 2022 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants