Skip to content

Commit

Permalink
refactor: Change replace method with __iter__ overriding (TheAlgori…
Browse files Browse the repository at this point in the history
  • Loading branch information
CaedenPH committed Oct 14, 2022
1 parent 413f4f9 commit c9078c7
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions data_structures/binary_tree/binary_tree_node_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from __future__ import annotations

from collections.abc import Iterator


class Node:
"""
Expand All @@ -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
"""

Expand All @@ -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__":
Expand Down

0 comments on commit c9078c7

Please sign in to comment.