mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
* feat: Binary tree node sum (#7020) * feat: Sum of all nodes in binary tree explanation (#7020) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update data_structures/binary_tree/binary_tree_node_sum.py Co-authored-by: Christian Clauss <cclauss@me.com> * refactor: Change replace method with `__iter__` overriding (#7020) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
6be9500b2f
commit
98a4c24878
@ -154,6 +154,7 @@
|
|||||||
* [Binary Search Tree](data_structures/binary_tree/binary_search_tree.py)
|
* [Binary Search Tree](data_structures/binary_tree/binary_search_tree.py)
|
||||||
* [Binary Search Tree Recursive](data_structures/binary_tree/binary_search_tree_recursive.py)
|
* [Binary Search Tree Recursive](data_structures/binary_tree/binary_search_tree_recursive.py)
|
||||||
* [Binary Tree Mirror](data_structures/binary_tree/binary_tree_mirror.py)
|
* [Binary Tree Mirror](data_structures/binary_tree/binary_tree_mirror.py)
|
||||||
|
* [Binary Tree Node Sum](data_structures/binary_tree/binary_tree_node_sum.py)
|
||||||
* [Binary Tree Traversals](data_structures/binary_tree/binary_tree_traversals.py)
|
* [Binary Tree Traversals](data_structures/binary_tree/binary_tree_traversals.py)
|
||||||
* [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py)
|
* [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py)
|
||||||
* [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py)
|
* [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py)
|
||||||
|
76
data_structures/binary_tree/binary_tree_node_sum.py
Normal file
76
data_structures/binary_tree/binary_tree_node_sum.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user