From 7892bf85c57f9ecfbdb155f498b0f3ef86457299 Mon Sep 17 00:00:00 2001 From: Caeden Date: Fri, 14 Oct 2022 12:06:36 +0100 Subject: [PATCH 1/5] feat: Binary tree node sum (#7020) --- DIRECTORY.md | 1 + .../binary_tree/binary_tree_node_sum.py | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 data_structures/binary_tree/binary_tree_node_sum.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 2786e1f82de8..3dc1fcec3f70 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -151,6 +151,7 @@ * [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 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) * [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py) * [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py) diff --git a/data_structures/binary_tree/binary_tree_node_sum.py b/data_structures/binary_tree/binary_tree_node_sum.py new file mode 100644 index 000000000000..ffc11a336130 --- /dev/null +++ b/data_structures/binary_tree/binary_tree_node_sum.py @@ -0,0 +1,65 @@ +from __future__ import annotations + + +class Node: + """ + A Node has 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) + >>> BinaryTreeNodeSum(tree).node_sum() + 10 + + >>> tree.left = Node(5) + >>> BinaryTreeNodeSum(tree).node_sum() + 15 + + >>> tree.right = Node(-3) + >>> BinaryTreeNodeSum(tree).node_sum() + 12 + + >>> tree.left.left = Node(12) + >>> BinaryTreeNodeSum(tree).node_sum() + 24 + + >>> tree.right.left = Node(8) + >>> tree.right.right = Node(0) + >>> BinaryTreeNodeSum(tree).node_sum() + 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 node_sum(self) -> int: + return self.depth_first_search(self.tree) + +if __name__ == "__main__": + import doctest + + doctest.testmod() + From c515a5f34078747eca9ca0c3fcea309b05d44843 Mon Sep 17 00:00:00 2001 From: Caeden Date: Fri, 14 Oct 2022 12:15:23 +0100 Subject: [PATCH 2/5] feat: Sum of all nodes in binary tree explanation (#7020) --- data_structures/binary_tree/binary_tree_node_sum.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/data_structures/binary_tree/binary_tree_node_sum.py b/data_structures/binary_tree/binary_tree_node_sum.py index ffc11a336130..a7c34ceaccda 100644 --- a/data_structures/binary_tree/binary_tree_node_sum.py +++ b/data_structures/binary_tree/binary_tree_node_sum.py @@ -1,3 +1,14 @@ +""" +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 6d50bed04654ec93f12641e6b4dd348923aaffed Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Oct 2022 11:16:40 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/binary_tree/binary_tree_node_sum.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/data_structures/binary_tree/binary_tree_node_sum.py b/data_structures/binary_tree/binary_tree_node_sum.py index a7c34ceaccda..cf2f99f9d7ac 100644 --- a/data_structures/binary_tree/binary_tree_node_sum.py +++ b/data_structures/binary_tree/binary_tree_node_sum.py @@ -1,10 +1,10 @@ """ Sum of all nodes in a binary tree. -Python implementation: +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 + O(n) space complexity - At any point in time maximum number of stack frames that could be in memory is `n` """ @@ -62,15 +62,14 @@ 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) + self.depth_first_search(node.left) + self.depth_first_search(node.right) ) def node_sum(self) -> int: return self.depth_first_search(self.tree) + if __name__ == "__main__": import doctest doctest.testmod() - From 413f4f98e58ad3bed3e90238201d9e07d1fdf02c Mon Sep 17 00:00:00 2001 From: Caeden Date: Fri, 14 Oct 2022 17:37:36 +0100 Subject: [PATCH 4/5] Update data_structures/binary_tree/binary_tree_node_sum.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/binary_tree_node_sum.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data_structures/binary_tree/binary_tree_node_sum.py b/data_structures/binary_tree/binary_tree_node_sum.py index cf2f99f9d7ac..be82e7f580b6 100644 --- a/data_structures/binary_tree/binary_tree_node_sum.py +++ b/data_structures/binary_tree/binary_tree_node_sum.py @@ -14,8 +14,7 @@ class Node: """ - A Node has value variable and pointers - to Nodes to its left and right. + A Node has a value variable and pointers to Nodes to its left and right. """ def __init__(self, value: int) -> None: From c9078c7b6949f36e58b57b128e3b6c7d67de368b Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Fri, 14 Oct 2022 17:43:12 +0100 Subject: [PATCH 5/5] refactor: Change replace method with `__iter__` overriding (#7020) --- .../binary_tree/binary_tree_node_sum.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/data_structures/binary_tree/binary_tree_node_sum.py b/data_structures/binary_tree/binary_tree_node_sum.py index be82e7f580b6..5a13e74e3c9f 100644 --- a/data_structures/binary_tree/binary_tree_node_sum.py +++ b/data_structures/binary_tree/binary_tree_node_sum.py @@ -11,6 +11,8 @@ from __future__ import annotations +from collections.abc import Iterator + class Node: """ @@ -33,24 +35,24 @@ class BinaryTreeNodeSum: 12 8 0 >>> tree = Node(10) - >>> BinaryTreeNodeSum(tree).node_sum() + >>> sum(BinaryTreeNodeSum(tree)) 10 >>> tree.left = Node(5) - >>> BinaryTreeNodeSum(tree).node_sum() + >>> sum(BinaryTreeNodeSum(tree)) 15 >>> tree.right = Node(-3) - >>> BinaryTreeNodeSum(tree).node_sum() + >>> sum(BinaryTreeNodeSum(tree)) 12 >>> tree.left.left = Node(12) - >>> BinaryTreeNodeSum(tree).node_sum() + >>> sum(BinaryTreeNodeSum(tree)) 24 >>> tree.right.left = Node(8) >>> tree.right.right = Node(0) - >>> BinaryTreeNodeSum(tree).node_sum() + >>> sum(BinaryTreeNodeSum(tree)) 32 """ @@ -64,8 +66,8 @@ def depth_first_search(self, node: Node | None) -> int: self.depth_first_search(node.left) + self.depth_first_search(node.right) ) - def node_sum(self) -> int: - return self.depth_first_search(self.tree) + def __iter__(self) -> Iterator[int]: + yield self.depth_first_search(self.tree) if __name__ == "__main__":