Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ff6044a

Browse files
committed
Binary Search Trees 2 solutions added
1 parent 5cdad73 commit ff6044a

File tree

2 files changed

+387
-0
lines changed

2 files changed

+387
-0
lines changed
Lines changed: 387 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,387 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "Problems_Binary-Search-Tree-2",
7+
"provenance": [],
8+
"collapsed_sections": []
9+
},
10+
"kernelspec": {
11+
"name": "python3",
12+
"display_name": "Python 3"
13+
},
14+
"language_info": {
15+
"name": "python"
16+
}
17+
},
18+
"cells": [
19+
{
20+
"cell_type": "markdown",
21+
"source": [
22+
"**CONTENT**"
23+
],
24+
"metadata": {
25+
"id": "o7_kl1tdQBpY"
26+
}
27+
},
28+
{
29+
"cell_type": "code",
30+
"source": [
31+
"class BinaryTreeNode:\n",
32+
"\n",
33+
" def __init__(self,data):\n",
34+
" self.data=data;\n",
35+
" self.left=None\n",
36+
" self.right=None\n",
37+
" \n",
38+
"def search(root,x):\n",
39+
" if root==None:\n",
40+
" return False\n",
41+
" if root.data==x:\n",
42+
" return True\n",
43+
" elif root.data>x:\n",
44+
" return search(root.left,x)\n",
45+
" else:\n",
46+
" return search(root.left,x)\n",
47+
"\n",
48+
"def printTreeDetailed(root):\n",
49+
" if root==None:\n",
50+
" return\n",
51+
" print(root.data,end=\":\")\n",
52+
" if root.left!=None:\n",
53+
" print(\"L\",root.left.data,end=\",\")\n",
54+
" if root.right!=None:\n",
55+
" print(\"R\",root.right.data,end=\"\")\n",
56+
" print()\n",
57+
" printTreeDetailed(root.left)\n",
58+
" printTreeDetailed(root.right)\n",
59+
"\n",
60+
"import queue\n",
61+
"def takeTreeInputLevelWise():\n",
62+
" q=queue.Queue()\n",
63+
" print(\"Enter root\")\n",
64+
" rootData=int(input())\n",
65+
" if rootData==-1:\n",
66+
" return None\n",
67+
" root=BinaryTreeNode(rootData)\n",
68+
" q.put(root)\n",
69+
" while(not(q.empty())):\n",
70+
" current_node=q.get()\n",
71+
" print(\"Enter left child of\",current_node.data)\n",
72+
" leftChildData=int(input())\n",
73+
" if leftChildData!=-1:\n",
74+
" leftChild=BinaryTreeNode(leftChildData)\n",
75+
" current_node.left=leftChild\n",
76+
" q.put(leftChild)\n",
77+
" print(\"Enter right child of\",current_node.data)\n",
78+
" rightChildData=int(input())\n",
79+
" if rightChildData!=-1:\n",
80+
" rightChild=BinaryTreeNode(rightChildData)\n",
81+
" current_node.right=rightChild\n",
82+
" q.put(rightChild)\n",
83+
" return root\n",
84+
"\n",
85+
"def nodeToRootPath(root,s):\n",
86+
" if root==None:\n",
87+
" return None\n",
88+
" if root.data==s:\n",
89+
" l=list()\n",
90+
" l.append(root.data)\n",
91+
" return l\n",
92+
" leftOutput=nodeToRootPath(root.left,s)\n",
93+
" if leftOutput!=None:\n",
94+
" leftOutput.append(root.data)\n",
95+
" return leftOutput\n",
96+
" rightOutput=nodeToRootPath(root.right,s)\n",
97+
" if rightOutput!=None:\n",
98+
" rightOutput.append(root.data)\n",
99+
" return rightOutput\n",
100+
" else:\n",
101+
" return None\n",
102+
"\n",
103+
"root=takeTreeInputLevelWise()\n",
104+
"printTreeDetailed(root)\n",
105+
"nodeToRootPath(root,5)"
106+
],
107+
"metadata": {
108+
"id": "ne3Yz9XAQECO"
109+
},
110+
"execution_count": null,
111+
"outputs": []
112+
},
113+
{
114+
"cell_type": "markdown",
115+
"source": [
116+
"**CONTENT PROBLEMS**"
117+
],
118+
"metadata": {
119+
"id": "LAMbOztNJgpj"
120+
}
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": null,
125+
"metadata": {
126+
"id": "7hl6_i-8JW-_"
127+
},
128+
"outputs": [],
129+
"source": [
130+
"'''\n",
131+
"Find path in BST\n",
132+
"\n",
133+
"Given a BST and an integer k. Find and return the path from the node with data k and root \n",
134+
"(if a node with data k is present in given BST) in a list. Return empty list otherwise.\n",
135+
"\n",
136+
"Note: Assume that BST contains all unique elements.\n",
137+
"\n",
138+
"Input Format :\n",
139+
"The first line of input contains data of the nodes of the tree in level order form. \n",
140+
"The data of the nodes of the tree is separated by space. If any node does not have left or right child, \n",
141+
"take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, \n",
142+
"it will not be a part of the data of any node. \n",
143+
"\n",
144+
"The following line of input contains an integer, that denotes the value of k.\n",
145+
"\n",
146+
"Output Format :\n",
147+
"The first line and only line of output prints the data of the nodes in the path from node k to root. \n",
148+
"The data of the nodes is separated by single space.\n",
149+
"\n",
150+
"Constraints:\n",
151+
"Time Limit: 1 second \n",
152+
"\n",
153+
"Sample Input 1:\n",
154+
"8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1\n",
155+
"2\n",
156+
"Sample Output 1:\n",
157+
"2 5 8\n",
158+
"'''\n",
159+
"\n",
160+
"import queue\n",
161+
"class BinaryTreeNode:\n",
162+
" def __init__(self, data):\n",
163+
" self.data = data\n",
164+
" self.left = None\n",
165+
" self.right = None\n",
166+
"\n",
167+
"def findPathBST(root,data):\n",
168+
" if root is None:\n",
169+
" return None\n",
170+
" if root.data == data:\n",
171+
" lst = list()\n",
172+
" lst.append(root.data)\n",
173+
" return lst\n",
174+
" leftOutput = findPathBST(root.left,data)\n",
175+
" if leftOutput != None:\n",
176+
" leftOutput.append(root.data)\n",
177+
" return leftOutput\n",
178+
" rightOutput = findPathBST(root.right,data)\n",
179+
" if rightOutput != None:\n",
180+
" rightOutput.append(root.data)\n",
181+
" return rightOutput\n",
182+
" \n",
183+
"def buildLevelTree(levelorder):\n",
184+
" index = 0\n",
185+
" length = len(levelorder)\n",
186+
" if length<=0 or levelorder[0]==-1:\n",
187+
" return None\n",
188+
" root = BinaryTreeNode(levelorder[index])\n",
189+
" index += 1\n",
190+
" q = queue.Queue()\n",
191+
" q.put(root)\n",
192+
" while not q.empty():\n",
193+
" currentNode = q.get()\n",
194+
" leftChild = levelorder[index]\n",
195+
" index += 1\n",
196+
" if leftChild != -1:\n",
197+
" leftNode = BinaryTreeNode(leftChild)\n",
198+
" currentNode.left =leftNode\n",
199+
" q.put(leftNode)\n",
200+
" rightChild = levelorder[index]\n",
201+
" index += 1\n",
202+
" if rightChild != -1:\n",
203+
" rightNode = BinaryTreeNode(rightChild)\n",
204+
" currentNode.right =rightNode\n",
205+
" q.put(rightNode)\n",
206+
" return root\n",
207+
"\n",
208+
"# Main\n",
209+
"levelOrder = [int(i) for i in input().strip().split()]\n",
210+
"root = buildLevelTree(levelOrder)\n",
211+
"data = int(input())\n",
212+
"path = findPathBST(root,data)\n",
213+
"if path is not None:\n",
214+
" for ele in path:\n",
215+
" print(ele,end=' ')"
216+
]
217+
},
218+
{
219+
"cell_type": "code",
220+
"source": [
221+
"'''\n",
222+
"BST Class\n",
223+
"\n",
224+
"Implement the BST class which includes following functions -\n",
225+
"\n",
226+
"1. search\n",
227+
"Given an element, find if that is present in BST or not. Return true or false.\n",
228+
"\n",
229+
"2. insert -\n",
230+
"Given an element, insert that element in the BST at the correct position. \n",
231+
"If element is equal to the data of the node, insert it in the left subtree.\n",
232+
"\n",
233+
"3. delete -\n",
234+
"Given an element, remove that element from the BST. \n",
235+
"If the element which is to be deleted has both children, \n",
236+
"replace that with the minimum element from right sub-tree.\n",
237+
"\n",
238+
"4. printTree (recursive) -\n",
239+
"Print the BST in ithe following format -\n",
240+
"\n",
241+
"For printing a node with data N, you need to follow the exact format -\n",
242+
"N:L:x,R:y\n",
243+
"\n",
244+
"where, N is data of any node present in the binary tree. \n",
245+
"x and y are the values of left and right child of node N. Print the children only if it is not null.\n",
246+
"\n",
247+
"There is no space in between.\n",
248+
"You need to print all nodes in the recursive format in different lines.\n",
249+
"\n",
250+
"'''\n",
251+
"\n",
252+
"\n",
253+
"\n",
254+
"class BinaryTreeNode:\n",
255+
" def __init__(self, data):\n",
256+
" self.data = data\n",
257+
" self.left = None\n",
258+
" self.right = None\n",
259+
" \n",
260+
"class BST:\n",
261+
" \n",
262+
" def __init__(self):\n",
263+
" self.root = None\n",
264+
" self.numNodes = 0\n",
265+
" \n",
266+
" \n",
267+
" def printTreeHelper(self,root):\n",
268+
" if root==None:\n",
269+
" return\n",
270+
" print(root.data,end=\":\")\n",
271+
" if root.left!=None:\n",
272+
" print(\"L:\" + str(root.left.data),end=\",\")\n",
273+
" if root.right!=None:\n",
274+
" print(\"R:\" + str(root.right.data),end=\"\")\n",
275+
" print()\n",
276+
" self.printTreeHelper(root.left)\n",
277+
" self.printTreeHelper(root.right)\n",
278+
" \n",
279+
" def printTree(self):\n",
280+
" self.printTreeHelper(self.root)\n",
281+
" \n",
282+
" \n",
283+
" def isPresentHelper(self,root,data):\n",
284+
" if root==None:\n",
285+
" return False\n",
286+
" if root.data==data:\n",
287+
" return True\n",
288+
" if root.data>data:\n",
289+
" #call on left\n",
290+
" return self.isPresentHelper(root.left,data)\n",
291+
" else:\n",
292+
" #Call on right\n",
293+
" return self.isPresentHelper(root.right,data)\n",
294+
" \n",
295+
" def search(self, data):\n",
296+
" return self.isPresentHelper(self.root,data)\n",
297+
" \n",
298+
" \n",
299+
" def insertHelper(self,root,data):\n",
300+
" if root==None:\n",
301+
" node=BinaryTreeNode(data)\n",
302+
" return node\n",
303+
" if root.data>=data:\n",
304+
" root.left=self.insertHelper(root.left,data)\n",
305+
" return root\n",
306+
" else:\n",
307+
" root.right=self.insertHelper(root.right,data)\n",
308+
" return root\n",
309+
" \n",
310+
" def insert(self, data):\n",
311+
" self.numNodes += 1\n",
312+
" self.root=self.insertHelper(self.root,data)\n",
313+
" \n",
314+
" \n",
315+
" def min(self,root):\n",
316+
" if root==None:\n",
317+
" return 10000\n",
318+
" if root.left==None:\n",
319+
" return root.data\n",
320+
" \n",
321+
" def deleteDataHelper(self,root,data):\n",
322+
" if root==None:\n",
323+
" return False, None\n",
324+
" if root.data<data:\n",
325+
" deleted,newRightNode=self.deleteDataHelper(root.right,data)\n",
326+
" root.right=newRightNode\n",
327+
" return deleted,root\n",
328+
" if root.data>data:\n",
329+
" deleted,newLeftNode=self.deleteDataHelper(root.left,data)\n",
330+
" root.left=newLeftNode\n",
331+
" return deleted,root\n",
332+
" #root is leaf\n",
333+
" if root.left==None and root.right==None:\n",
334+
" return True, None\n",
335+
" # root has one child\n",
336+
" if root.left==None:\n",
337+
" return True,root.right\n",
338+
" if root.right==None:\n",
339+
" return True,root.left\n",
340+
" #root has 2 children\n",
341+
" replacement=self.min(root.right)\n",
342+
" root.data=replacement\n",
343+
" deleted,newRightNode=self.deleteDataHelper(root.right,replacement)\n",
344+
" root.right=newRightNode\n",
345+
" return True,root\n",
346+
" \n",
347+
" def delete(self, data):\n",
348+
" deleted,newRoot=self.deleteDataHelper(self.root,data)\n",
349+
" if deleted:\n",
350+
" self.numNodes-=1\n",
351+
" self.root=newRoot\n",
352+
" return deleted\n",
353+
" \n",
354+
" def count(self):\n",
355+
" return self.numNodes\n",
356+
"\n",
357+
" \n",
358+
"b = BST()\n",
359+
"q = int(input())\n",
360+
"while (q > 0) :\n",
361+
" li = [int(ele) for ele in input().strip().split()]\n",
362+
" choice = li[0]\n",
363+
" q-=1\n",
364+
" if choice == 1:\n",
365+
" data = li[1]\n",
366+
" b.insert(data)\n",
367+
" elif choice == 2:\n",
368+
" data = li[1]\n",
369+
" b.delete(data)\n",
370+
" elif choice == 3:\n",
371+
" data = li[1]\n",
372+
" ans = b.search(data)\n",
373+
" if ans is True:\n",
374+
" print('true')\n",
375+
" else:\n",
376+
" print('false')\n",
377+
" else:\n",
378+
" b.printTree()"
379+
],
380+
"metadata": {
381+
"id": "uoTa5z4DzIJC"
382+
},
383+
"execution_count": null,
384+
"outputs": []
385+
}
386+
]
387+
}
437 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /