Skip to content

Commit d056b11

Browse files
Binary Search Tree --> insertion LCA
1 parent 9deba0a commit d056b11

File tree

4 files changed

+330
-0
lines changed

4 files changed

+330
-0
lines changed
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 2,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdin",
10+
"output_type": "stream",
11+
"text": [
12+
" 6\n",
13+
" 20 15 24 26 4 16\n"
14+
]
15+
},
16+
{
17+
"name": "stdout",
18+
"output_type": "stream",
19+
"text": [
20+
"4 15 16 20 24 26 "
21+
]
22+
}
23+
],
24+
"source": [
25+
"class Node:\n",
26+
" def __init__(self,key):\n",
27+
" self.right=None\n",
28+
" self.left=None\n",
29+
" self.val=key\n",
30+
"\n",
31+
"def insert(root,node):\n",
32+
" if root is None:\n",
33+
" root=node\n",
34+
" else:\n",
35+
" if root.val<node.val:\n",
36+
" if root.right is None:\n",
37+
" root.right=node\n",
38+
" else:\n",
39+
" insert(root.right,node)\n",
40+
" else:\n",
41+
" if root.left is None:\n",
42+
" root.left=node\n",
43+
" else:\n",
44+
" insert(root.left,node)\n",
45+
"\n",
46+
"def inorder(root):\n",
47+
" if root:\n",
48+
" inorder(root.left)\n",
49+
" print(root.val,end=' ')\n",
50+
" inorder(root.right)\n",
51+
"\n",
52+
"n=int(input())\n",
53+
"a=list(map(int,input().split()))\n",
54+
"for i in range(n):\n",
55+
" if i==0:\n",
56+
" r=Node(a[i])\n",
57+
" else:\n",
58+
" insert(r,Node(a[i]))\n",
59+
"inorder(r)"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": 2,
65+
"metadata": {},
66+
"outputs": [],
67+
"source": [
68+
"# maxHeight of Binary Search Tree\n",
69+
"def getHeight(root):\n",
70+
" if root:\n",
71+
" leftDepth = getHeight(root.left)\n",
72+
" rightDepth = getHeight(root.right)\n",
73+
" return max(leftDepth,rightDepth) + 1\n",
74+
" else:\n",
75+
" return -1"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 3,
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"data": {
85+
"text/plain": [
86+
"2"
87+
]
88+
},
89+
"execution_count": 3,
90+
"metadata": {},
91+
"output_type": "execute_result"
92+
}
93+
],
94+
"source": [
95+
"getHeight(r)"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 26,
101+
"metadata": {},
102+
"outputs": [
103+
{
104+
"name": "stdin",
105+
"output_type": "stream",
106+
"text": [
107+
"Enter data of Nodes: 5 3 8 2 4 6 7\n"
108+
]
109+
},
110+
{
111+
"name": "stdout",
112+
"output_type": "stream",
113+
"text": [
114+
"5 3 2 4 8 6 7 "
115+
]
116+
}
117+
],
118+
"source": [
119+
"# %load 'Insertion_2(function in class)'.py\n",
120+
"# https://www.hackerrank.com/challenges/binary-search-tree-insertion/problem?h_r=next-challenge&h_v=zen\n",
121+
"class Node:\n",
122+
" def __init__(self, info):\n",
123+
" self.data = info \n",
124+
" self.left = None \n",
125+
" self.right = None \n",
126+
"def preOrder(root):\n",
127+
" if root == None:\n",
128+
" return\n",
129+
" print (root.data, end=\" \")\n",
130+
" preOrder(root.left)\n",
131+
" preOrder(root.right)\n",
132+
"class BinarySearchTree:\n",
133+
" def __init__(self): \n",
134+
" self.root = None\n",
135+
" def add_data(self,data):\n",
136+
" if self.root is None:\n",
137+
" self.root=Node(data)\n",
138+
" else:\n",
139+
" head=self.root\n",
140+
" while head:\n",
141+
" if head.data<data:\n",
142+
" if head.right is None:\n",
143+
" head.right=Node(data)\n",
144+
" break\n",
145+
" else:\n",
146+
" head=head.right\n",
147+
" else:\n",
148+
" if head.left is None:\n",
149+
" head.left=Node(data)\n",
150+
" break\n",
151+
" else:\n",
152+
" head=head.left\n",
153+
"b=BinarySearchTree()\n",
154+
"a=map(int,input('Enter data of Nodes: ').split())\n",
155+
"for i in a:\n",
156+
" b.add_data(i)\n",
157+
"preOrder(b.root)"
158+
]
159+
},
160+
{
161+
"cell_type": "markdown",
162+
"metadata": {},
163+
"source": [
164+
"### [Binary Search Tree : Lowest Common Ancestor](https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor/problem)"
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": 27,
170+
"metadata": {},
171+
"outputs": [
172+
{
173+
"name": "stdout",
174+
"output_type": "stream",
175+
"text": [
176+
"5 3 2 4 8 6 7 "
177+
]
178+
}
179+
],
180+
"source": [
181+
"preOrder(b.root)"
182+
]
183+
},
184+
{
185+
"cell_type": "code",
186+
"execution_count": 33,
187+
"metadata": {},
188+
"outputs": [],
189+
"source": [
190+
"# %load Lowest\\ Common\\ Ancestor.py\n",
191+
"# https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor/problem\n",
192+
"def lca(root, v1, v2):\n",
193+
" if (root.data<v1 and root.data<v2):\n",
194+
" return lca(root.right,v1,v2)\n",
195+
" elif (root.data>v1 and root.data>v2):\n",
196+
" return lca(root.left,v1,v2)\n",
197+
" else:\n",
198+
" return root"
199+
]
200+
},
201+
{
202+
"cell_type": "code",
203+
"execution_count": 34,
204+
"metadata": {},
205+
"outputs": [
206+
{
207+
"data": {
208+
"text/plain": [
209+
"3"
210+
]
211+
},
212+
"execution_count": 34,
213+
"metadata": {},
214+
"output_type": "execute_result"
215+
}
216+
],
217+
"source": [
218+
"lca(b.root,2,4).data"
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"execution_count": 35,
224+
"metadata": {},
225+
"outputs": [
226+
{
227+
"data": {
228+
"text/plain": [
229+
"5"
230+
]
231+
},
232+
"execution_count": 35,
233+
"metadata": {},
234+
"output_type": "execute_result"
235+
}
236+
],
237+
"source": [
238+
"lca(b.root,2,6).data"
239+
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"execution_count": null,
244+
"metadata": {},
245+
"outputs": [],
246+
"source": []
247+
}
248+
],
249+
"metadata": {
250+
"kernelspec": {
251+
"display_name": "Python 3",
252+
"language": "python",
253+
"name": "python3"
254+
},
255+
"language_info": {
256+
"codemirror_mode": {
257+
"name": "ipython",
258+
"version": 3
259+
},
260+
"file_extension": ".py",
261+
"mimetype": "text/x-python",
262+
"name": "python",
263+
"nbconvert_exporter": "python",
264+
"pygments_lexer": "ipython3",
265+
"version": "3.8.2"
266+
}
267+
},
268+
"nbformat": 4,
269+
"nbformat_minor": 4
270+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def Insert(root,data):
2+
if root is None:
3+
root=node(data)
4+
else:
5+
if root.data < data:
6+
if root.right is None:
7+
root.right=node(data)
8+
else:
9+
Insert(root.right,data)
10+
else:
11+
if root.left is None:
12+
root.left=node(data)
13+
else:
14+
Insert(root.left,data)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# https://www.hackerrank.com/challenges/binary-search-tree-insertion/problem?h_r=next-challenge&h_v=zen
2+
class Node:
3+
def __init__(self, info):
4+
self.data = info
5+
self.left = None
6+
self.right = None
7+
def preOrder(root):
8+
if root == None:
9+
return
10+
print (root.data, end=" ")
11+
preOrder(root.left)
12+
preOrder(root.right)
13+
class BinarySearchTree:
14+
def __init__(self):
15+
self.root = None
16+
def add_data(self,data):
17+
if self.root is None:
18+
self.root=Node(data)
19+
else:
20+
head=self.root
21+
while head:
22+
if head.data<data:
23+
if head.right is None:
24+
head.right=Node(data)
25+
break
26+
else:
27+
head=head.right
28+
else:
29+
if head.left is None:
30+
head.left=Node(data)
31+
break
32+
else:
33+
head=head.left
34+
b=BinarySearchTree()
35+
a=map(int,input('Enter data of Nodes: ').split())
36+
for i in a:
37+
b.add_data(i)
38+
preOrder(b.root)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor/problem
2+
def lca(root, v1, v2):
3+
if (root.data<v1 and root.data<v2):
4+
return lca(root.right,v1,v2)
5+
elif (root.data>v1 and root.data>v2):
6+
return lca(root.left,v1,v2)
7+
else:
8+
return root

0 commit comments

Comments
 (0)