-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Made changes #9950
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
Made changes #9950
Changes from 4 commits
bf44f09
56377fd
fb0fdb7
066902c
ab7c64d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from __future__ import annotations | ||
|
||
class Node: | ||
def __init__(self, number: int) -> None: | ||
self.data = number | ||
self.left = None | ||
self.right = None | ||
|
||
class BinaryTree: | ||
def __init__(self, root=None) -> None: | ||
self.root = root | ||
|
||
def __iter__(self) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
if self.root is not None: | ||
yield from self._traverse_inorder(self.root) | ||
|
||
def __str__(self) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
return " ".join(str(data) for data in self) | ||
|
||
def _traverse_inorder(self, node: Node) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
if node is not None: | ||
yield from self._traverse_inorder(node.left) | ||
yield node.data | ||
yield from self._traverse_inorder(node.right) | ||
|
||
def build_a_tree(self) -> Node: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
# Create a binary tree with the specified structure | ||
self.root = Node(11) | ||
self.root.left = Node(2) | ||
self.root.right = Node(29) | ||
self.root.left.left = Node(1) | ||
self.root.left.right = Node(7) | ||
self.root.right.left = Node(15) | ||
self.root.right.right = Node(40) | ||
self.root.right.right.left = Node(35) | ||
return self.root | ||
|
||
def transform_tree_util(root: Node | None) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
if root is None: | ||
return | ||
|
||
# Recur for right subtree | ||
transform_tree_util(root.right) | ||
|
||
# Update sum | ||
global total | ||
total = total + root.data | ||
|
||
# Store old sum in the current node | ||
root.data = total - root.data | ||
|
||
# Recur for left subtree | ||
transform_tree_util(root.left) | ||
|
||
def binary_tree_to_sum_tree(root: Node | None) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
# Call the utility function to transform the tree | ||
transform_tree_util(root) | ||
|
||
if __name__ == '__main__': | ||
total = 0 | ||
tree = BinaryTree() | ||
root = tree.build_a_tree() | ||
# Transform the tree | ||
binary_tree_to_sum_tree(root) | ||
print("Transformed Tree:") | ||
print(tree) | ||
|
||
""" | ||
Test Cases: | ||
|
||
>>> root = Node(11) | ||
>>> root.left = Node(2) | ||
>>> root.right = Node(29) | ||
>>> transform_tree(root) | ||
>>> root.data | ||
60 | ||
>>> root.left.data | ||
31 | ||
>>> root.right.data | ||
29 | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide type hint for the parameter:
root