forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_bst_sum_tree.py
129 lines (103 loc) · 2.69 KB
/
transform_bst_sum_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from __future__ import annotations
class Node:
"""
prints the inorder Traversal of transformed tree
>>> sum = 0
>>> root = Node(11)
>>> root.left = Node(2)
>>> root.right = Node(29)
>>> root.left.left = Node(1)
>>> root.left.right = Node(7)
>>> root.right.left = Node(15)
>>> root.right.right = Node(40)
>>> root.right.right.left = Node(35)
>>> printInorder(root)
1 2 7 11 15 29 35 40
>>> transformTree(root)
>>> printInorder(root)
139 137 130 119 104 75 40 0
"""
def __init__(self, number: int) -> None:
self.data = number
self.left = None
self.right = None
# Recursive function to transform a BST to sum tree.
# This function traverses the tree in reverse inorder so
# that we have visited all greater key nodes of the currently
# visited node
def transform_tree_util(root: Node | None) -> None:
"""
Transform a binary tree into a sum tree.
Example:
>>> root = Node(11)
>>> root.left = Node(2)
>>> root.right = Node(29)
>>> transformTree(root)
>>> root.data
60
>>> root.left.data
31
>>> root.right.data
29
"""
# Base case
if root == None:
return
# Recur for right subtree
transform_tree_util(root.right)
# Update sum
global sum
sum = sum + root.data
# Store old sum in the current node
root.data = sum - root.data
# Recur for left subtree
transform_tree_util(root.left)
# A wrapper over transformTreeUtil()
def transform_tree(root: Node | None) -> None:
"""
Transform a binary tree into a sum tree.
Example:
>>> root = Node(11)
>>> root.left = Node(2)
>>> root.right = Node(29)
>>> transformTree(root)
>>> root.data
60
>>> root.left.data
31
>>> root.right.data
29
"""
# sum = 0 #Initialize sum
transform_tree_util(root)
# A utility function to prindorder traversal of a
# binary tree
def print_inorder(root: Node | None):
"""
Perform an inorder traversal of a binary tree and print the nodes.
Example:
>>> root = Node(11)
>>> root.left = Node(2)
>>> root.right = Node(29)
>>> printInorder(root)
2 11 29
"""
if root == None:
return
print_inorder(root.left)
print(root.data, end=" ")
print_inorder(root.right)
# Driver Program to test above functions
if __name__ == "__main__":
sum = 0
root = Node(11)
root.left = Node(2)
root.right = Node(29)
root.left.left = Node(1)
root.left.right = Node(7)
root.right.left = Node(15)
root.right.right = Node(40)
root.right.right.left = Node(35)
print_inorder(root)
transform_tree_util(root)
print_inorder(root)