-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsa-project-1.0.py
260 lines (235 loc) · 9 KB
/
dsa-project-1.0.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Created by jack1806
# On HP notebook using Linux Kali 2.0
# DSA project Winter Sem 16-17
#!/usr/bin/python
from flask import Flask
from flask import render_template
# from random import randint
class Node:
if __name__ == "__main__":
value = 0
right = None
left = None
def insert(self, item):
current_node = self
while True:
if current_node is None:
return 1
elif current_node.value < item.value:
if current_node.right is None:
current_node.right = item
return 1
current_node = current_node.right
else:
if current_node.left is None:
current_node.left = item
return 1
current_node = current_node.left
def search(self, val):
current_node = self
steps = 0
nodes_changed = 0
while True:
if current_node.value == val:
steps += 1
return True, steps, nodes_changed
if current_node.value < val:
steps += 1
if current_node.right is None:
steps += 1
return False, steps, nodes_changed
elif current_node.right.value == val:
steps += 1
return True, steps, nodes_changed
nodes_changed += 1
current_node = current_node.right
else:
if current_node.left is None:
steps += 1
return False, steps, nodes_changed
elif current_node.left.value == val:
steps += 1
return True, steps, nodes_changed
nodes_changed += 1
current_node = current_node.left
class CustomNode:
if __name__=="__main__":
value = 0
right = None
left = None
name = None
right_right = None
right_left = None
left_right = None
left_left = None
def getChilds(self):
childs = [self.left_left,self.left,self.left_right,self.right_left,self.right,self.right_right]
return childs
def insert(self,item):
item_value = item.value
current_node = self
while(True):
if(current_node.value > item_value ):
if(current_node.left!=None):
if(current_node.left.value>item_value):
if(current_node.left_left==None):
current_node.left_left = item
print("Added to LL of :",current_node.value)
return 1
current_node = current_node.left_left
else:
if(current_node.left_right==None):
current_node.left_right = item
print("Added to LR of :",current_node.value)
return 1
current_node = current_node.left_right;
else:
current_node.left = item
print("Added to L of :",current_node.value)
return 1
else:
if(current_node.right!=None):
if(current_node.right.value>item_value):
if(current_node.right_left==None):
current_node.right_left = item
print("Added to RL of :",current_node.value)
return 1
current_node = current_node.right_left
else:
if(current_node.right_right==None):
current_node.right_right = item
print("Added to RR of :",current_node.value)
return 1
current_node = current_node.right_right
else:
current_node.right = item
print("Added to R of :",current_node.value)
return 1
def search(self,val):
current_node = self
steps = 0
nodes_changed = 0
while(True):
if(current_node.value == val):
steps +=1
return True,steps,nodes_changed,current_node
elif(current_node.value > val):
steps +=1
if(current_node.left == None):
steps +=1
return False,steps,nodes_changed,None
if(current_node.left.value==val):
steps +=1
return True,steps,nodes_changed,current_node.left
elif(current_node.left.value > val):
steps +=1
if(current_node.left_left == None):
steps +=1
return False,steps,nodes_changed,None
if(current_node.left_left.value==val):
steps +=1
return True,steps,nodes_changed,current_node.left_left
steps+=1
nodes_changed+=1
current_node = current_node.left_left
else:
if(current_node.left_right == None):
steps +=1
return False,steps,nodes_changed,None
if(current_node.left_right.value==val):
steps +=1
return True,steps,nodes_changed,current_node.left_right
steps+=1
nodes_changed+=1
current_node = current_node.left_right
else:
if(current_node.right == None):
steps +=1
return False,steps,nodes_changed,None
if(current_node.right.value==val):
steps +=1
return True,steps,nodes_changed,current_node.right
elif(current_node.right.value > val):
steps +=1
if(current_node.right_left == None):
steps +=1
return False,steps,nodes_changed,None
if(current_node.right_left.value==val):
steps +=1
return True,steps,nodes_changed,current_node.right_left
steps+=1
nodes_changed+=1
current_node = current_node.right_left
else:
if(current_node.right_right == None):
steps +=1
return False,steps,nodes_changed,None
if(current_node.right_right.value==val):
steps +=1
return True,steps,nodes_changed,current_node.right_right
steps+=1
nodes_changed+=1
current_node = current_node.right_right
def init():
n = CustomNode()
m = Node()
f = open('adhaar.txt','r')
l = []
l2 = {}
for i in f:
l.append(int("".join(i.split()[0:3])))
l2.update({int("".join(i.split()[0:3])):" ".join(i.split()[3::])})
n.value = int(l[0])
m.value = n.value
for i in range(1,len(l)):
t = CustomNode()
r = Node()
t.value = l[i]
t.name = l2[l[i]]
r.value = t.value
n.insert(t)
m.insert(r)
return n,m,l2,l
def create_tree(n,m,searchvalue):
temp = int(searchvalue)
status,steps,nodes_changed,name = n.search(temp)
status2,steps2,nodes_changed2 = m.search(temp)
return status,steps,nodes_changed,name
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/<temp>')
def options(temp):
n,m,l,l2 = init()
if(temp == 'view'):
return render_template("view.html",list=l2,dic=l,length=len(l2))
elif(temp == 'jst'):
return render_template("jst.html",parent=n.value,childs=n.getChilds())
return render_template(""+temp+".html")
@app.route('/search/<tosearch>')
def searchfor(tosearch):
n,m,l, temp = init()
status, steps, node_ch, node = create_tree(n, m, tosearch)
return "Steps required : %d<br>Status : %s<br>Nodes Changed : %d<br>Holder name : %s" % (steps, status, node_ch, node.name)
@app.route('/add/<aadhaar>/<name>')
def add(aadhaar,name):
n,m,l,temp = init()
status, steps, node, na = create_tree(n, m, int(aadhaar))
if(status):
return "Fail , User already Exist"
s = aadhaar[0:4]+" "+aadhaar[4:8]+" "+aadhaar[8::]+" "
with open('adhaar.txt','a') as file:
file.write(s+name+"\n")
file.close()
return "Done"
@app.route('/jst/<node>')
def manualnodeview(node):
n,m,l, temp = init()
status, steps, node_ch, node = create_tree(n, m, node)
return render_template("jst.html",parent=node.value,childs=node.getChilds())
if __name__ == '__main__':
app.run()
n = CustomNode()
m = Node()
n, m, l, temp = init()