-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
703 lines (531 loc) · 17.9 KB
/
index.js
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// binary tree shot
// ive neevr made a class before lmfao
// this is a treenode
class treenode {
constructor(value, left, right) {
this.value = value;
this.left = left;
this.right = right;
}
get getvalue() {
return this.value;
}
get getleft(){
return this.left;
}
get getright(){
return this.right;
}
setvalue(val){
this.value = val;
}
setleft(l){
this.left = l;
}
setright(r){
this.right = r;
}
}
let treeholder = document.getElementById('treeholder');
let nodesadded = 0;
function drawnode(node,x,y){
const div = document.createElement('div');
if (!nodearr.includes(node)){
// add a new one
// nodearr[nodesadded] = node;
// console.log('pushed in '+node.getvalue+' with '+nodearr.length +' and '+ nodesadded);
// addedone = true;
}
let thisnum = nodearr.indexOf(node);
div.innerHTML = `
<input id='val${thisnum}' type='text' class='nodeval' value='${node.getvalue}' onchange="modvalue(${thisnum});">
`;
let theleft = node.getleft;
if (theleft == null || theleft.getvalue == 'deleted'){
div.innerHTML += ` <button id='leftadder${thisnum}' class='nodeadderleft' onclick='addleft(${thisnum});'>left</button>`
} else {
div.innerHTML += ` <button id='leftadder${thisnum}' class='nodeadderleft' style='opacity: 0;'>left</button>`
}
let theright = node.getright;
if (theright == null || theright.getvalue == 'deleted'){
div.innerHTML += `<button id='rightadder${thisnum}' class='nodeadderright' onclick='addright(${thisnum});'>right</button>`;
} else {
div.innerHTML += `<button id='rightadder${thisnum}' class='nodeadderright' style='opacity: 0;'>right</button>`;
}
if ((theright == null || theright.getvalue == 'deleted') && (theleft == null || theleft.getvalue == 'deleted') && node != root){
div.innerHTML += ` <button id='deletenode${thisnum}' onclick='delnode(${thisnum});' class='deletenode'>del</button>`
} else {
div.innerHTML += ` <button id='deletenode${thisnum}' class='deletenode' style='opacity: 0;'>del</button>`
}
div.style.marginLeft = x+'%';
div.style.marginTop = y+'px';
div.style.position = 'absolute';
div.id = 'thenode'+String(thisnum);
div.onclick = function() {document.getElementById(div.id).style.zIndex = rnzindex; rnzindex += 1;};
rnzindex += 1;
div.className = 'node';
// if (addedone){
// nodesadded += 1;
// }
treeholder.appendChild(div);
}
function modvalue(n){
let el = document.getElementById('val'+n);
nodearr[n].setvalue(el.value);
}
function delnode(n){
console.log(nodearr,n,nodearr[n].getvalue);
//nodearr[n].setvalue('deleted');
let thing = nodearr[n];
thing.setvalue('deleted');
nodearr[n] = null;
nodearr.splice(n,1);
//nodearr[n] = null;
// go through and find whos left or right this is
// console.log(findnode(root,nodearr[n]));
//delnodes.push(n);
console.log(root.getleft);
nodesadded -= 1;
redrawtree();
}
function redrawtree(){
console.log(nodearr);
treeholder.innerHTML = "";
connectors.innerHTML = "";
drawtree(root, 50, 0, 50);
}
function addleft(num){
console.log('addleft on '+num+' with '+nodearr);
let newnode = new treenode(0,null,null);
nodearr[num].setleft(newnode);
nodesadded += 1;
// add a new one
nodearr[nodesadded] = newnode;
console.log(nodearr);
redrawtree();
return nodesadded;
}
function addright(num){
let newnode = new treenode(0,null,null);
nodearr[num].setright(newnode);
nodesadded += 1;
// add a new one
nodearr[nodesadded] = newnode;
redrawtree();
}
// draw the tree recursive
function drawtree(root, x, y, prevx){
if (root.getvalue == 'deleted'){
root = null;
}
if (root != null && root.getvalue != 'deleted'){
drawnode(root,x,y);
let newy = y + 75;
let leftx;
let rightx;
if (x == 50){
leftx = 25;
rightx = 75;
} else {
// distance from middle Math.abs(50-x)
leftx = x-Math.abs(prevx-x)/2;
rightx = x+Math.abs(prevx-x)/2;
console.log(leftx);
}
// leftx = leftx-(5000/window.innerWidth);
// rightx = rightx-(5000/window.innerWidth);
const div = document.createElement('div');
let wid = Math.abs(leftx-x);
div.className = 'connector';
div.style.width = wid+'%';
div.style.height = '75px'
div.style.left = (leftx+(5000/window.innerWidth))+'%';
div.style.top = (y+150)+'px';
div.style.zIndex = -1;
// line for right
const div1 = document.createElement('div');
div1.className = 'connector';
div1.style.borderRight = '7px solid var(--contrast)';
div1.style.borderLeft = '0px solid white';
div1.style.width = wid+'%';
div1.style.height = '75px'
div1.style.left = (x+(5000/window.innerWidth))+'%';
div1.style.top = (y+150)+'px';
div1.style.zIndex = -1;
if (root.getleft != null && root.getleft.getvalue != 'deleted'){
document.getElementById('connectors').appendChild(div);
drawtree(root.getleft,leftx,newy, x);
}
if (root.getright != null && root.getright.getvalue != 'deleted'){
document.getElementById('connectors').appendChild(div1);
drawtree(root.getright,rightx,newy, x);
}
}
}
function traversepreorder(node){
if (node == null){
return "";
}
let st = "";
st += node.getvalue+" ";
st += traversepreorder(node.getleft)+" ";
st += traversepreorder(node.getright)+" ";
return st;
}
function traverseinorder(node){
if (node == null){
return "";
}
let st = "";
st += traverseinorder(node.getleft)+" ";
st += node.getvalue+" ";
st += traverseinorder(node.getright)+" ";
return st;
}
function traversepostorder(node){
if (node == null){
return "";
}
let st = "";
st += traversepostorder(node.getleft)+" ";
st += traversepostorder(node.getright)+" ";
st += node.getvalue+" ";
return st;
}
function checkall(){
// first run each
let preorder = traversepreorder(root).replaceAll('deleted','');
let inorder = traverseinorder(root).replaceAll('deleted','');
let postorder = traversepostorder(root).replaceAll('deleted','');
let sols = document.getElementById('solutions');
sols.innerHTML = `
<h1 style='font-size: 25px'>Solutions</h1>
<h1 style='font-size: 15px'>Pre order: ${preorder}</h1>
<h1 style='font-size: 15px'>In order: ${inorder}</h1>
<h1 style='font-size: 15px'>Post order: ${postorder}</h1>
<div class="close" onclick="closesolutions();">Close</div>
`
preorder = preorder.replaceAll(' ','');
inorder = inorder.replaceAll(' ','');
postorder = postorder.replaceAll(' ','');
let userpreorder = document.getElementById('preorder').value;
userpreorder = userpreorder.replaceAll(' ','').replaceAll(',','');
if (userpreorder.toUpperCase() == preorder.toUpperCase()){
document.getElementById('preorder').style.border = '3px solid rgba(0,245,0)';
} else {
document.getElementById('preorder').style.border = '3px solid rgba(245,0,0)';
}
let userinorder = document.getElementById('inorder').value;
userinorder = userinorder.replaceAll(' ','').replaceAll(',','');
if (userinorder.toUpperCase() == inorder.toUpperCase()){
document.getElementById('inorder').style.border = '3px solid rgba(0,245,0)';
} else {
document.getElementById('inorder').style.border = '3px solid rgba(245,0,0)';
}
let userpostorder = document.getElementById('postorder').value;
userpostorder = userpostorder.replaceAll(' ','').replaceAll(',','');
if (userpostorder.toUpperCase() == postorder.toUpperCase()){
document.getElementById('postorder').style.border = '3px solid rgba(0,245,0)';
} else {
document.getElementById('postorder').style.border = '3px solid rgba(245,0,0)';
}
}
function showsolutions(){
checkall();
//document.getElementById('solutions').style.display = 'block';
document.getElementById('solutions').style.opacity = 1;
document.getElementById('solutions').style.left = '25%';
}
function openprefs(){
document.getElementById('preferences').style.opacity = 1;
document.getElementById('preferences').style.left = '25%';
}
function openel(el){
document.getElementById(el).style.opacity = 1;
document.getElementById(el).style.left = '25%';
}
function toggletheme(override){
var r = document.querySelector(':root');
let endtime = new Date();
var timediff = endtime - lasttoggle;
lasttoggle = endtime;
if (timediff < 333 && !override){
return;
}
console.log('changeing from '+theme);
if (theme == 'dark'){
// make light
theme = 'light';
localStorage.setItem('bttheme','light');
document.getElementById('theme').textContent = "Theme: (light)";
r.style.setProperty('--bg', 'white');
r.style.setProperty('--contrast', 'black');
r.style.setProperty('--main', 'rgb(12, 34, 162)');
} else {
// make dark
theme = 'dark';
localStorage.setItem('bttheme','dark');
document.getElementById('theme').textContent = "Theme: (dark)";
r.style.setProperty('--bg', 'black');
r.style.setProperty('--contrast', 'white');
r.style.setProperty('--main', 'rgb(7, 176, 176)');
}
}
function toggleangle(override){
var r = document.querySelector(':root');
let endtime = new Date();
var timediff = endtime - lasttoggle;
lasttoggle = endtime;
if (timediff < 333 && !override){
console.log('returned');
return;
}
if (angle == 'cornered'){
// make light
angle = 'curved';
localStorage.setItem('btangle','curved');
document.getElementById('lines').textContent = "Lines: (Curved)";
r.style.setProperty('--br', '25px');
} else {
// make dark
angle = 'cornered';
localStorage.setItem('btangle','cornered');
document.getElementById('lines').textContent = "Lines: (Cornered)";
r.style.setProperty('--br', '0px');
}
}
function closeprefs(){
//document.getElementById('solutions').style.display = 'none';
document.getElementById('preferences').style.opacity = 0;
document.getElementById('preferences').style.left = '-100%';
}
function closeel(el){
//document.getElementById('solutions').style.display = 'none';
document.getElementById(el).style.opacity = 0;
document.getElementById(el).style.left = '-100%';
}
function closesolutions(){
//document.getElementById('solutions').style.display = 'none';
document.getElementById('solutions').style.opacity = 0;
document.getElementById('solutions').style.left = '-100%';
}
function closeinstructions(){
document.getElementById('instructions').style.left = '125%';
document.getElementById('instructions').style.opacity = 0;
}
async function glowtreepreorder(start){
if (start == null){
return;
}
let idx = nodearr.indexOf(start);
let nd = document.getElementById('thenode'+String(idx));
nd.style.boxShadow = '7px 7px 5px rgba(255, 255, 0, 0.7)';
await sleep(350);
await glowtreepreorder(start.getleft);
await sleep(350);
await glowtreepreorder(start.getright);
//enableplay();
}
async function glowtreeinorder(start){
if (start == null){
return;
}
let idx = nodearr.indexOf(start);
let nd = document.getElementById('thenode'+String(idx));
await sleep(350);
await glowtreeinorder(start.getleft);
nd.style.boxShadow = '7px 7px 5px rgba(255, 255, 0, 0.7)';
await sleep(350);
await glowtreeinorder(start.getright);
//enableplay();
}
function disableplay(){
document.getElementById('playpost').style.color = 'rgba(125,125,125,0.6)';
document.getElementById('playin').style.color = 'rgba(125,125,125,0.6)';
document.getElementById('playpre').style.color = 'rgba(125,125,125,0.6)';
disabled = true;
}
function enableplay(){
document.getElementById('playpost').style.color = 'yellow';
document.getElementById('playin').style.color = 'yellow';
document.getElementById('playpre').style.color = 'yellow';
disabled = false;
}
async function glowtreepostorder(start){
if (start == null){
return;
}
let idx = nodearr.indexOf(start);
let nd = document.getElementById('thenode'+String(idx));
await glowtreepostorder(start.getleft);
await sleep(350);
await glowtreepostorder(start.getright);
await sleep(350);
nd.style.boxShadow = '7px 7px 5px rgba(255, 255, 0, 0.7)';
//enableplay();
}
// rules
let bt1 = localStorage.getItem('binarytree');
if (bt1 == null){
localStorage.setItem('binarytree','opened');
document.getElementById('instructions').style.display = 'block';
document.getElementById('instructions').style.opacity = 1;
document.getElementById('instructions').style.left = '25%';
}
let theme = localStorage.getItem('bttheme');
let angle = localStorage.getItem('btangle');
let lasttoggle = new Date();
if (theme == null){
localStorage.setItem("bttheme",'dark');
} else if (theme == 'light'){
theme = 'dark';
toggletheme(true);
}
if (angle == null){
localStorage.setItem("btangle",'cornered');
} else if (angle == 'curved'){
angle = 'cornered';
toggleangle(true);
}
function getrandtree(){
// let mnode = new treenode('root', null, null);
let mnode = root;
let nleft = new treenode('left', null, null);
let nright = new treenode('right', null, null);
mnode.setleft(nleft);
nodesadded += 1;
// add a new one
nodearr[nodesadded] = nleft;
mnode.setright(nright);
nodesadded += 1;
// add a new one
nodearr[nodesadded] = nright;
let totalnodes = 1;
totalnodes += addlevel75(nleft, 0);
totalnodes += addlevel75(nright, 0);
return mnode;
}
function addlevel75(root, level){
if (level >= document.getElementById('numnodes').value-1){
return;
}
let newnode;
if (Math.floor(Math.random()*4) > 0){
newnode = new treenode(0, null, null);
root.setleft(newnode);
nodesadded += 1;
// add a new one
nodearr[nodesadded] = newnode;
addlevel75(newnode, level+1);
}
if (Math.floor(Math.random()*4) > 0){
newnode = new treenode(0, null, null);
root.setright(newnode);
nodesadded += 1;
// add a new one
nodearr[nodesadded] = newnode;
addlevel75(newnode, level+1);
}
}
// counts the nodes
function countnodes(root){
if (root == null){
return 0;
}
return (1 + countnodes(root.getleft) + countnodes(root.getright));
}
// get an arr with n random numbers
function genlist(n){
// lets try smth
let ttrs = []; // yes the audi
while (ttrs.length < n){
ttrs.push(Math.floor(Math.random()*100));
}
return ttrs; // yes bring it back pls audi
}
// abcdefghijklmnop
function genlistabc(n){
// lets try smth
let abc = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
let lucidair = []; // yes the audi
while (lucidair.length < n){
lucidair.push(abc[Math.floor(Math.random()*26)]);
}
return lucidair; //hehe
}
// just put in the facking nums
function putnums(root,arr){
if (arr.length == 0){
return;
}
//get nums of nodes on left
let leftcount = countnodes(root.getleft);
let rightcount = countnodes(root.getright);
// ideally leftcount+rightocount+1 shud = arr.length
// so u have the arr
let leftrange = arr.slice(0,leftcount);
let rightrange = arr.slice(leftcount+1,arr.length);
let thenum = arr[leftcount];
root.setvalue(thenum);
putnums(root.getleft, leftrange);
putnums(root.getright, rightrange);
return root;
}
// this is for a random tree
function randtree(){
// first of all make a tree
let theroot = getrandtree();
let ncount = countnodes(theroot);
// now gen an arr with those many nodes
let genedarr;
let tp = document.getElementById('treetype').value;
if (tp == 'alpha'){
type = 'alpha';
genedarr = genlistabc(ncount);
} else if (tp == 'numeric'){
type = 'numeric';
genedarr = genlist(ncount);
} else {
let rr = Math.floor(Math.random()*2);
if (rr == 0){
type = 'alpha';
genedarr = genlistabc(ncount);
} else {
type = 'numeric';
genedarr = genlist(ncount);
}
}
putnums(theroot,genedarr);
return theroot;
}
let root = new treenode('root',null,null);
// let left = new treenode('left',new treenode('leftleft',new treenode('leftleftleft',null,null),null),new treenode('leftright',new treenode('leftrightleft',null,null),null));
// let right = new treenode('right',null,null);
// let rightright = new treenode('rightright',null,new treenode('rightrightright',null,null));
// let rightleft = new treenode('rightleft',null,null);
// root.setleft(left);
// root.setright(right);
// right.setleft(rightleft);
// right.setright(rightright);
const sleep = ms => new Promise(res => setTimeout(res, ms));
let leftbuttons = [];
let delnodes = [];
let nodearr = [root,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
let disabled = false;
// intialx
let x = 50;
let y = 0;
let rnzindex = 10;
let stayingup = false;
drawtree(root, x, y, 50);
//glowtree(root);
// update the coubnt without interfereing with the stuff
(async () => {
fetch((`https://pst652.deta.dev/?UPDATECOUNTER=binarytree`))
.then(response => {
return response.json();
})
.then(data => {
console.log(data);
})
})();