Skip to content

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 5 commits into from
Oct 15, 2022

Conversation

CaedenPH
Copy link
Contributor

@CaedenPH CaedenPH commented Oct 14, 2022

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
Contributor Author

@cclauss pinging for review

Copy link
Member

@cclauss cclauss left a comment

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.

def __init__(self, tree: Node) -> None:
self.tree = tree

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

@cclauss cclauss Oct 14, 2022

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
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 14, 2022
@CaedenPH
Copy link
Contributor Author

@cclauss
Is this what you meant?

@dhruvmanila
Copy link
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
Copy link
Member

cclauss commented Oct 15, 2022

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
Contributor Author

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.

Okay, I understand. However, perhaps all of this should be implemented in the basic binary tree or some other more general file instead of binary tree path sum

@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