Skip to content

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

Closed
wants to merge 5 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions data_structures/binary_tree/transform_bst_sum_tree.py
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:

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

self.root = root

def __iter__(self) -> int:

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/transform_bst_sum_tree.py, please provide doctest for the function __iter__

if self.root is not None:
yield from self._traverse_inorder(self.root)

def __str__(self) -> str:

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/transform_bst_sum_tree.py, please provide doctest for the function __str__

return " ".join(str(data) for data in self)

def _traverse_inorder(self, node: Node) -> int:

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/transform_bst_sum_tree.py, please provide doctest for the function _traverse_inorder

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:

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/transform_bst_sum_tree.py, please provide doctest for the function build_a_tree

# 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:

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/transform_bst_sum_tree.py, please provide doctest for the function transform_tree_util

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:

Choose a reason for hiding this comment

The 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 data_structures/binary_tree/transform_bst_sum_tree.py, please provide doctest for the function binary_tree_to_sum_tree

# 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
"""