-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathBST1.py
More file actions
210 lines (178 loc) · 4.94 KB
/
BST1.py
File metadata and controls
210 lines (178 loc) · 4.94 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#Search In BST
"""
Given a BST and an integer k. Find if the integer k is present in given BST or not.
Return the node with data k if it is present, return null otherwise.
Assume that BST contains all unique elements.
Input Format :
Line 1 : Elements in level order form (separated by space)
(If any node does not have left or right child, take -1 in its place)
Line 2 : Integer k
Output Format :
Node with data k
"""
Sample Input 1 :
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
2
Sample Output 1 :
2
Sample Input 2 :
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
12
Sample Output 2 :
(empty)
Solution :
import queue
class BinaryTreeNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def searchInBST(root, k):
if root==None:
return False
if root.data==k:
return root.data
elif root.data>k:
return searchInBST(root.left,k)
elif root.data<k:
return searchInBST(root.right,k)
else:
return False
def buildLevelTree(levelorder):
index = 0
length = len(levelorder)
if length<=0 or levelorder[0]==-1:
return None
root = BinaryTreeNode(levelorder[index])
index += 1
q = queue.Queue()
q.put(root)
while not q.empty():
currentNode = q.get()
leftChild = levelorder[index]
index += 1
if leftChild != -1:
leftNode = BinaryTreeNode(leftChild)
currentNode.left =leftNode
q.put(leftNode)
rightChild = levelorder[index]
index += 1
if rightChild != -1:
rightNode = BinaryTreeNode(rightChild)
currentNode.right =rightNode
q.put(rightNode)
return root
levelOrder = [int(i) for i in input().strip().split()]
root = buildLevelTree(levelOrder)
k=int(input())
node=searchInBST(root, k)
if node:
print(node)
#Elements Between K1 and K2
"""
Given a Binary Search Tree and two integers k1 and k2,
find and print the elements which are in range k1 and k2 (both inclusive).
Print the elements in increasing order.
Input format :
Line 1 : Elements in level order form (separated by space)
(If any node does not have left or right child, take -1 in its place)
Line 2 : Two Integers k1 and k2
Output Format :
Required elements (separated by space)
"""
Sample Input :
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
6 10
Sample Output :
6 7 8 10
Solution :
import queue
class BinaryTreeNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def elementsInRangeK1K2(root, k1, k2):
if root == None:
return
if k1 < root.data:
elementsInRangeK1K2(root.left, k1, k2)
if k1 <= root.data and k2 >= root.data:
print(root.data, end=" ")
if k2 > root.data:
elementsInRangeK1K2(root.right, k1, k2)
def buildLevelTree(levelorder):
index = 0
length = len(levelorder)
if length <= 0 or levelorder[0] == -1:
return None
root = BinaryTreeNode(levelorder[index])
index += 1
q = queue.Queue()
q.put(root)
while not q.empty():
currentNode = q.get()
leftChild = levelorder[index]
index += 1
if leftChild != -1:
leftNode = BinaryTreeNode(leftChild)
currentNode.left = leftNode
q.put(leftNode)
rightChild = levelorder[index]
index += 1
if rightChild != -1:
rightNode = BinaryTreeNode(rightChild)
currentNode.right = rightNode
q.put(rightNode)
return root
levelOrder = [int(i) for i in input().strip().split()]
root = buildLevelTree(levelOrder)
k1, k2 = (int(i) for i in input().strip().split())
elementsInRangeK1K2(root, k1, k2)
#Construct BST
"""
Given a sorted integer array A of size n which contains all unique elements.
You need to construct a balanced BST from this input array. Return the root of constructed BST.
Note : If array size is even, take first mid as root.
Input format :
Line 1 : Integer n (Size of array)
Line 2 : Array elements (separated by space)
Output Format :
BST elements (in pre order traversal, separated by space)
"""
Sample Input :
7
1 2 3 4 5 6 7
Sample Output :
4 2 1 3 6 5 7
Solution :
import queue
class BinaryTreeNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def constructBST(lst):
if not lst:
return None
if len(lst) % 2 != 0:
mid = (len(lst)) // 2
root = BinaryTreeNode(lst[mid])
root.left = constructBST(lst[:mid])
root.right = constructBST(lst[mid + 1:])
else:
mid = ((len(lst)) // 2) - 1
root = BinaryTreeNode(lst[mid])
root.left = constructBST(lst[:mid])
root.right = constructBST(lst[mid + 1:])
return root
def preOrder(root):
if root == None:
return
print(root.data, end=' ')
preOrder(root.left)
preOrder(root.right)
n = int(input())
lst = [int(i) for i in input().strip().split()]
root = constructBST(lst)
preOrder(root)