Skip to content

Commit b545938

Browse files
committed
Python solution for converting BT to BST in DSA 450
1 parent 52c391f commit b545938

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

DSA 450 GFG/BTtoBST.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Link : https://practice.geeksforgeeks.org/problems/binary-tree-to-bst/1#
2+
3+
'''
4+
# Tree Node
5+
class Node:
6+
def __init__(self, val):
7+
self.right = None
8+
self.data = val
9+
self.left = None
10+
'''
11+
class Solution:
12+
13+
# The given root is the root of the Binary Tree
14+
# Return the root of the generated BST
15+
16+
def BTtoArray(self , root , array):
17+
18+
if root is None:
19+
return
20+
21+
self.BTtoArray(root.left , array)
22+
23+
array.append(root.data)
24+
25+
self.BTtoArray(root.right , array)
26+
27+
28+
def countNodes(self,root):
29+
30+
if root is None:
31+
return 0
32+
33+
return self.countNodes(root.left) + self.countNodes(root.right) + 1
34+
35+
def ArraytoBST(self,array,root):
36+
37+
if root is None:
38+
return
39+
40+
self.ArraytoBST(array , root.left)
41+
42+
root.data = array[0]
43+
array.pop(0)
44+
45+
self.ArraytoBST(array , root.right)
46+
47+
48+
def binaryTreeToBST(self, root):
49+
# code here
50+
51+
if root is None:
52+
return
53+
54+
# Find the number of nodes to create an array of that size
55+
n = self.countNodes(root)
56+
57+
arr = []*n
58+
59+
# Store the inorder traversal of tree in an array
60+
self.BTtoArray(root , arr)
61+
62+
# Sort the array
63+
arr.sort()
64+
65+
# Tranfer the elements from root to the tree and return the BST
66+
self.ArraytoBST(arr , root)
67+
68+
69+
70+
71+
#{
72+
# Driver Code Starts
73+
#Initial Template for Python 3
74+
75+
from collections import deque
76+
# Tree Node
77+
class Node:
78+
def __init__(self, val):
79+
self.right = None
80+
self.data = val
81+
self.left = None
82+
83+
# Function to Build Tree
84+
def buildTree(s):
85+
#Corner Case
86+
if(len(s)==0 or s[0]=="N"):
87+
return None
88+
89+
# Creating list of strings from input
90+
# string after spliting by space
91+
ip=list(map(str,s.split()))
92+
93+
# Create the root of the tree
94+
root=Node(int(ip[0]))
95+
size=0
96+
q=deque()
97+
98+
# Push the root to the queue
99+
q.append(root)
100+
size=size+1
101+
102+
# Starting from the second element
103+
i=1
104+
while(size>0 and i<len(ip)):
105+
# Get and remove the front of the queue
106+
currNode=q[0]
107+
q.popleft()
108+
size=size-1
109+
110+
# Get the current node's value from the string
111+
currVal=ip[i]
112+
113+
# If the left child is not null
114+
if(currVal!="N"):
115+
116+
# Create the left child for the current node
117+
currNode.left=Node(int(currVal))
118+
119+
# Push it to the queue
120+
q.append(currNode.left)
121+
size=size+1
122+
# For the right child
123+
i=i+1
124+
if(i>=len(ip)):
125+
break
126+
currVal=ip[i]
127+
128+
# If the right child is not null
129+
if(currVal!="N"):
130+
131+
# Create the right child for the current node
132+
currNode.right=Node(int(currVal))
133+
134+
# Push it to the queue
135+
q.append(currNode.right)
136+
size=size+1
137+
i=i+1
138+
return root
139+
140+
def printInorder(root):
141+
if root is None:
142+
return
143+
printInorder(root.left)
144+
print (root.data, end=' ')
145+
printInorder(root.right)
146+
147+
if __name__=="__main__":
148+
t=int(input())
149+
for _ in range(0,t):
150+
s=input()
151+
root=buildTree(s)
152+
Solution().binaryTreeToBST(root)
153+
printInorder(root)
154+
print()
155+
# } Driver Code Ends

0 commit comments

Comments
 (0)