This repository was archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
606 lines (527 loc) · 21.5 KB
/
main.py
File metadata and controls
606 lines (527 loc) · 21.5 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.listview import ListItemButton
from kivy.core.window import Window
from kivy.storage.jsonstore import JsonStore
from kivy.uix.label import Label
from kivy.uix.button import Button
from random import random
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
from kivy.uix.togglebutton import ToggleButton
from functools import partial
from random import random
from kivy.clock import Clock
import insert_data
# to get the reference to ds widget
action_widget = Widget()
current_ds = ''
#True when basics section is displayed
basics_present = False
# True after ds is created
ds_present = False
ds = []
dtype = 'int'
# head of the link list
head_of_list = None
# tail of the queue
q_tail = -1
# front of the queue
q_front = 0
# top of the stack
stack_top = -1
# size of the stack
stack_size = 0
# True when ds is created but not displayed
ds_in_background = False
# list of allowed dat types
dtypes = ['int', 'str']
def clearmsg(dt): action_widget.error_messages.clear_widgets()
def show_error_msg(msg):
action_widget.error_messages.clear_widgets()
action_widget.error_messages.add_widget(
Label(text=msg, color=[0,0,0,1]))
Clock.schedule_once(clearmsg, 2)
def validate_data_type(dtype, data):
if dtype == 'int':
try:
data = int(data)
return True
except: return False
elif dtype == 'str':
try:
data = str(data)
return True
except: return False
else: return False
class ArrayElementWidget(BoxLayout):
pass
class ArrayCreationWidget(BoxLayout):
def create_array(self, size, data_type):
try: s = int (size)
except: return
global ds_present, ds, dtype, ds_in_background, dtypes
ds = []
dtype = data_type
if dtype not in dtypes:
show_error_msg("invaild data type")
return
# Array is created and will be displayed
ds_present = True
ds_in_background = False
#clear everything before displaying the new array
action_widget.disp_sec.clear_widgets()
# display the new array
for i in range(int(size)):
e = ArrayElementWidget()
ds.append(e)
action_widget.disp_sec.add_widget(e)
e.address.text = str(100+i*4)
e.index.text = str(i)
e.val.text = 'Garbage'
class ArrayInsertionWidget(BoxLayout):
def insert_data(self, data, pos):
if (len(ds)):
if len(ds) > int(pos):
if validate_data_type(dtype, data) == True:
ds[int(pos)].val.text = data
else: show_error_msg("invalid data")
class ArrayDeletionWidget(BoxLayout):
def delete_data(self, btn):
try:
pos = int(self.pos_or_data_val.text)
if pos < len(ds):
i = pos
while(i<len(ds)-1):
ds[i].val.text = ds[i+1].val.text
i +=1
ds[len(ds)].val.text = 'value same but not in the array'
except: return
class ArrayReplacementWidget(BoxLayout):
def replace_data(self, opt, btn):
try:
if opt == 'addr':
pos = int(self.pos_or_data_to_replace.text)
if pos < len(ds): ds[pos].val.text = self.replacement.text
else:
data = int(self.pos_or_data_to_replace.text)
ds = [self.replacement.text for d in ds if int(d.val.text) == data]
except: print "error"
class LLHeadWidget(BoxLayout): pass
class LLElementWidget(BoxLayout): pass
class LLCreationWidget(BoxLayout):
def create_ll(self, data_type):
global ds,dtype, head_of_list, dtypes
ds = []
dtype = data_type.text
if dtype not in dtypes:
show_error_msg("invalid data type")
return
ds_present = True
ds_in_background = False
action_widget.disp_sec.clear_widgets()
ll_head = LLHeadWidget()
head_of_list = ll_head
action_widget.disp_sec.add_widget(ll_head)
ll_head.head_address.text = str(int(10000*random()))
ll_head.pointsto.text = 'null'
class LLInsertionWidget(BoxLayout):
def insert_data_front(self, data_in_ll):
global ds,dtype, head_of_list
if validate_data_type(dtype, data_in_ll) == False:
show_error_msg("invalid data")
return
if head_of_list:
action_widget.disp_sec.clear_widgets()
# create a new LL node
new_le = LLElementWidget()
#assign a random address
new_le.node_address.text = str(int(10000*random()))
#assign a value
new_le.val.text = data_in_ll
# next of the new node points to next of head
new_le.nextptr.text = head_of_list.pointsto.text
# head points to new node
head_of_list.pointsto.text = new_le.node_address.text
# add the new node to the global ds list
ds = [new_le] + ds
action_widget.disp_sec.add_widget(head_of_list)
for d in ds:
action_widget.disp_sec.add_widget(d)
def insert_data_end(self, data_in_ll):
global ds,dtype, head_of_list
if validate_data_type(dtype, data_in_ll) == False:
show_error_msg("invalid data")
if len(ds) == 0:
self.insert_data_front(data_in_ll)
return
if head_of_list:
action_widget.disp_sec.clear_widgets()
action_widget.disp_sec.add_widget(head_of_list)
# create a new LL node
new_le = LLElementWidget()
#assign a random address
new_le.node_address.text = str(int(10000*random()))
#assign a value
new_le.val.text = data_in_ll
# next of the new node points to null
new_le.nextptr.text = 'null'
# last node points to new node
ds[len(ds)-1].nextptr.text = new_le.node_address.text
# add the new node to the global ds list
ds.append(new_le)
for d in ds: action_widget.disp_sec.add_widget(d)
def insert_data_at(self, data_in_ll, pos):
global ds,dtype, head_of_list
if validate_data_type(dtype, data_in_ll) == False:
show_error_msg("invalid data")
try:
pos = int(pos.text)-1
data = int(data_in_ll.text)
except:
print 'invalid data or pos'
return
if pos > len(ds) or pos < 0: return
if pos == 0:
self.insert_data_front(data_in_ll)
return
action_widget.disp_sec.clear_widgets()
try:
action_widget.disp_sec.add_widget(head_of_list)
# create a new LL node
new_le = LLElementWidget()
#assign a random address
new_le.node_address.text = str(int(10000*random()))
#assign a value
new_le.val.text = data_in_ll
# next of the new node points to next of previous
new_le.nextptr.text = ds[pos-1].nextptr.text
ds[pos-1].nextptr.text = new_le.node_address.text
# add the new node to the global ds list
last = ds[pos:]
ds = ds[:pos]+[new_le]
ds = ds + last
for i in ds: action_widget.disp_sec.add_widget(i)
except: return
class LLDeletionWidget(BoxLayout):
def delete_data(self, pos):
global ds, head_of_list
try: pos = int(pos)-1
except: return
if pos <0 or pos > len(ds)-1: return
if len(ds) > 0:
action_widget.disp_sec.remove_widget(ds[pos])
if pos == 0: head_of_list.pointsto.text = ds[pos].nextptr.text
else: ds[pos-1].nextptr.text = ds[pos].nextptr.text
del ds[pos]
class LLReplacementWidget(BoxLayout):
def replace_node(self, pos):
global ds, head_of_list
try:pos = int(pos)-1
except:return
if pos < 0 or pos > len(ds) - 1:return
if len(ds) > 0:
new_node = LLElementWidget()
new_node.node_address.text = str(int(random()*1000))
new_node.nextptr.text = ds[pos].nextptr.text
if pos == 0: head_of_list.pointsto.text = ds[pos].node_address.text
else: ds[pos-1].nextptr.text = new_node.node_address.text
ds[pos] = new_node
action_widget.disp_sec.clear_widgets()
action_widget.disp_sec.add_widget(head_of_list)
for i in range(len(ds)):
action_widget.disp_sec.add_widget(ds[i])
class QueueElementWidget(BoxLayout):
pass
class QueueCreationWidget(BoxLayout):
def create_queue(self, size, data_type):
action_widget.disp_sec.clear_widgets()
global ds, dtype, dtypes
dtype = data_type
ds = []
if dtype not in dtypes:
show_error_msg("invalid data type")
return
for i in range(int(size)):
nqe = QueueElementWidget()
nqe.address.text = str(1000+i*4)
ds.append(nqe)
action_widget.disp_sec.add_widget(ds[i])
class QueueInsertionWidget(BoxLayout):
def insert_data(self, data):
global ds, q_tail, q_front, dtype
if not validate_data_type(dtype, data):
show_error_msg("invalid data")
return
sign = 1
if q_tail<0: sign = -1
q_tail +=1
q_tail = (len(ds)+q_tail)%len(ds)
if q_tail == q_front and sign>0:
show_error_msg("queue overflow")
return
ds[q_tail].val.text = data
class QueueDeletionWidget(BoxLayout):
def delete_data(self):
global ds, q_tail, q_front
if q_tail > 0:
ds[q_front].val.text = ''
if q_front == q_tail:
q_front =0
q_tail = -1
else: q_front +=1
class StackElementWidget(BoxLayout): pass
class StackCreationWidget(BoxLayout):
def create_stack(self, size, data_type):
global ds,dtype, stack_top, stack_size, dtypes
dtype = data_type
if dtype not in dtypes:
show_error_msg("invalid data type")
return
stack_size = int(size)
if stack_size>0:
action_widget.disp_sec.clear_widgets()
for i in range(stack_size):
sw = StackElementWidget()
sw.address.text = str(1000 + i*4)
ds.append(sw)
action_widget.disp_sec.add_widget(sw)
class StackInsertionWidget(BoxLayout):
def push(self, data):
global ds, stack_top, stack_size
if not validate_data_type(dtype, data):
show_error_msg("invalid data")
return
if stack_top == stack_size-1:
action_widget.error_messages.clear_widgets()
action_widget.error_messages.add_widget(
Label(text='stack overflow', color= [0,0,0,1]))
Clock.schedule_once(clearmsg, 2)
return
else:
stack_top +=1
ds[stack_top].val.text = data
class StackDeletionWidget(BoxLayout):
def pop(self):
global ds, stack_top
if stack_top == -1:
action_widget.error_messages.clear_widgets()
action_widget.error_messages.add_widget(
Label(text='stack underflow', color= [0,0,0,1]))
Clock.schedule_once(clearmsg, 2)
return
else:
ds[stack_top].val.text = ''
stack_top -=1
class HeapElementWidget(BoxLayout): pass
class HeapCreationWidget(BoxLayout):
def create_heap(self, size):
pass
class HeapInsertionWidget(BoxLayout): pass
class HeapDeletionWidget(BoxLayout): pass
class HeapReplacementWidget(BoxLayout): pass
class TreeElementWidget(RelativeLayout):
def __init__(self, *args, **kwargs):
super(TreeElementWidget, self).__init__(*args, **kwargs)
self.parent = kwargs["p"]
self._parent.text = str(self.parent)[:3]
self.lc = kwargs["lc"]
self._left_child.text = str(self.lc)[:3]
self.rc = kwargs["rc"]
self._right_child.text = str(self.rc)[:3]
self.data = kwargs["data"]
self._data.text = str(self.data)
class TreeContainerWidget(RelativeLayout):
pass
class TreeCreationWidget(BoxLayout):
def create_tree(self, size):
elements = [1,2,3,4,5,6]
root = TreeElementWidget(p=None, lc=None, rc=None, data=elements[0],
size_hint=(.4, .2), pos_hint={'center_x':.6, 'center_y':1})
leftchild = TreeElementWidget(p=None, lc=None, rc=None, data=elements[1],
size_hint=(0.4, 0.2), pos_hint={'center_x':.3, 'center_y': .5})
root.lc = leftchild
leftchild.parent = None
root._left_child.text = str(root.lc)[:3]
leftchild._parent.text = str(root)[:3]
tc = TreeContainerWidget(size=action_widget.disp_sec.size)
tc.add_widget(root)
tc.add_widget(leftchild)
action_widget.disp_sec.clear_widgets()
action_widget.disp_sec.add_widget(tc)
class TreeInsertionWidget(BoxLayout): pass
class TreeDeletionWidget(BoxLayout): pass
class TreeReplacementWidget(BoxLayout): pass
class GraphElementWidget(BoxLayout): pass
class GraphCreationWidget(BoxLayout):
def create_graph(self, size): pass
class GraphInsertionWidget(BoxLayout): pass
class GraphDeletionWidget(BoxLayout): pass
class GraphReplacementWidget(BoxLayout): pass
# menu button in the ds widget menu section
class MenuButton(Button):
def perform_action(self, action):
global basics_present, ds, current_ds, ds_in_background, ds_present
# clear the input section everytime a menu button is pressed
action_widget.oprtn_input.clear_widgets()
# if the basics content is displayed clear it.
if basics_present == True:
action_widget.disp_sec.clear_widgets()
basics_present = False
# if current_ds == 'Arrays':
# if basics menu is selected
if action == 'basics':
#clear everything before displaying the content
action_widget.disp_sec.clear_widgets()
# show the basics
action_widget.disp_sec.add_widget(Label(
text=insert_data.ds_data.get(
action_widget.ds_widget.text)[action], color=[0,0,0,1]))
# the basics section is on display and the ds is in background if present
basics_present = True
if ds_present: ds_in_background = True
# if create menu is selected
elif action == 'create':
# clear the display
action_widget.disp_sec.clear_widgets()
# the ds has not been created yet and if previously it was created it has been deleted
ds_present = False
# there is no ds present in background
ds_in_background = False
# basics section is not on display
basics_present = False
if current_ds == 'Arrays':
action_widget.oprtn_input.add_widget(ArrayCreationWidget())
elif current_ds == 'Linked List':
action_widget.oprtn_input.add_widget(LLCreationWidget())
elif current_ds == 'Queue':
action_widget.oprtn_input.add_widget(QueueCreationWidget())
elif current_ds == 'Stack':
action_widget.oprtn_input.add_widget(StackCreationWidget())
elif current_ds == 'Heap':
action_widget.oprtn_input.add_widget(HeapCreationWidget())
elif current_ds == 'Tree':
action_widget.oprtn_input.add_widget(TreeCreationWidget())
elif current_ds == 'Graph':
action_widget.oprtn_input.add_widget(GraphCreationWidget())
else:
# if ds is present and in the background
if ds_present == True and ds_in_background == True:
# display the ds
for d in range (ds):
action_widget.disp_sec.add_widget(d)
# the ds is no longer in the background
ds_in_background = False
basics_present = False
if action == 'insert':
if current_ds == 'Arrays':
action_widget.oprtn_input.add_widget(
ArrayInsertionWidget())
elif current_ds == 'Linked List':
action_widget.oprtn_input.add_widget(
LLInsertionWidget())
elif current_ds == 'Queue':
action_widget.oprtn_input.add_widget(
QueueInsertionWidget())
elif current_ds == 'Stack':
action_widget.oprtn_input.add_widget(
StackInsertionWidget())
elif current_ds == 'Heap':
action_widget.oprtn_input.add_widget(
HeapInsertionWidget())
elif current_ds == 'Tree':
action_widget.oprtn_input.add_widget(
TreeInsertionWidget())
elif current_ds == 'Graph':
action_widget.oprtn_input.add_widget(
GraphInsertionWidget())
elif action == 'delete':
if current_ds == 'Arrays':
arr_del_w = ArrayDeletionWidget()
action_widget.oprtn_input.add_widget(arr_del_w)
selected_opt =[option for option in ToggleButton.get_widgets(
'oprtn_type') if option.state == 'down'][0]
arr_del_w.delete_button.bind(
on_press = arr_del_w.delete_data)
elif current_ds == 'Linked List':
action_widget.oprtn_input.add_widget(LLDeletionWidget())
elif current_ds == 'Queue':
action_widget.oprtn_input.add_widget(QueueDeletionWidget())
elif current_ds == 'Stack':
action_widget.oprtn_input.add_widget(StackDeletionWidget())
elif current_ds == 'Heap':
action_widget.oprtn_input.add_widget(HeapDeletionWidget())
elif current_ds == 'Tree':
action_widget.oprtn_input.add_widget(TreeDeletionWidget())
elif current_ds == 'Graph':
action_widget.oprtn_input.add_widget(GraphDeletionWidget())
elif action == 'replace':
if current_ds == 'Arrays':
arr_repl_w = ArrayReplacementWidget()
action_widget.oprtn_input.add_widget(arr_repl_w)
arr_repl_w.repl_btn.bind(
on_press = partial(
arr_repl_w.replace_data,
[option for option in ToggleButton.get_widgets(
'oprtn_type_repl') if option.state == 'down'][0].
text))
elif current_ds == 'Linked List':
action_widget.oprtn_input.add_widget(
LLReplacementWidget())
elif current_ds == 'Queue':
action_widget.oprtn_input.add_widget(
QueueReplacementWidget())
elif current_ds == 'Stack':
action_widget.oprtn_input.add_widget(
StackReplacementWidget())
elif current_ds == 'Heap':
action_widget.oprtn_input.add_widget(
HeapReplacementWidget())
elif current_ds == 'Tree':
action_widget.oprtn_input.add_widget(
TreeReplacementWidget())
elif current_ds == 'Graph':
action_widget.oprtn_input.add_widget(
GraphReplacementWidget())
# widgets to display data structure details.
class DataStrWidget(BoxLayout):
def __init__(self, **kwargs): super(DataStrWidget, self).__init__(**kwargs)
# home page list button
class DSButton(ListItemButton): pass
# Root class of the app
class VisualRoot(BoxLayout):
# return home
def return_home(self):
self.clear_widgets()
self.add_widget(VisualRoot())
# function to respond to ds option selected
def go_to_the_ds(self, selected_ds):
global current_ds, action_widget
current_ds = selected_ds
self.clear_widgets()
w = DataStrWidget(size=Window.size)
action_widget = w
self.add_widget(w)
w.ds_widget.text = selected_ds
menu_box = BoxLayout(orientation='vertical')
menu_widget = []
menu_list = insert_data.menu_store.get(selected_ds)['menu']
for i in range(len(menu_list)):
menu_widget.append(MenuButton(
text=insert_data.menu_store.get(selected_ds)['menu'][i]))
menu_box.add_widget(menu_widget[i])
w.menu.add_widget(menu_box)
# function to select the operation on array
class FunWithDSApp(App): pass
if __name__ == '__main__':
Builder.load_file('array.kv')
Builder.load_file('linklist.kv')
Builder.load_file('queue.kv')
Builder.load_file('stack.kv')
Builder.load_file('heap.kv')
Builder.load_file('tree.kv')
Builder.load_file('graph.kv')
FunWithDSApp().run()