-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtreevis.js
More file actions
288 lines (205 loc) · 5.13 KB
/
btreevis.js
File metadata and controls
288 lines (205 loc) · 5.13 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
//binary sort
function Node(val, parent) {
this.val = val;
this.parent = parent;
this.left = {};
this.right = {};
this.checked = false;
}
function isEmpty(obj) {
if ((Object.keys(obj).length) == 0) {
return true;
} else {
return false;
}
}
function binarySort(newnum, currentnode) {
//newnum only has to be a value (int)
//currentnode has to be a node (obj)
//for first node, if no "currentnode" exists, create a new node with the value of new node
if (!currentnode) {
return keynode = new Node(newnum);
}
if (newnum > currentnode.val) {
//greater, so to the right
if (isEmpty(currentnode.right) == false) {
//run again
binarySort(newnum, currentnode.right)
}
else if (isEmpty(currentnode.right) == true) {
//make new node here
currentnode.right = new Node(newnum, currentnode)
return nextnode = currentnode.right
}
}
else if (newnum < currentnode.val) {
//smaller, so to the left
if (isEmpty(currentnode.left) == false) {
//run again
binarySort(newnum, currentnode.left)
}
else if (isEmpty(currentnode.left) == true) {
//make new node here
currentnode.left = new Node(newnum, currentnode)
return nextnode = currentnode.left
}
}
}
// tester stuff
var testnode = new Node(1)
var counter = 0;
var unordered = [ 13, 47, 3, 2, 15, 21, 28, 5, 1, 853, 5351, 51256, 643, 4585, 24123, 421312, 754745, 5435, 21, 532, 643, 12, 6, 888, 432 ,351 ]
// necessary variables
var nextnode = {};
var keynode = {};
// runtime, sort to datastruct
for (i=0; i<unordered.length; i++) {
if (isEmpty(keynode) == false) {
binarySort(unordered[i], keynode)
} else if (isEmpty(keynode) == true) {
binarySort(unordered[i])
}
}
// how to read the data structure:
//
// try left until fail, read node,
// try right, if exists, repeat from there,
// if right !exist, go up parent untill find an unchecked node
//
// read algorithm
function runReadBinaryTree(keynode) {
ordered = [];
readBinaryTree(keynode);
return ordered
}
function readBinaryTree(currentnode) {
if (!currentnode.parent && currentnode.checked == true) {
return ordered
}
if (currentnode.checked == false) {
//if left is available and unchecked, go repeat the loop
if (isEmpty(currentnode.left) == false && currentnode.left.checked == false) {
readBinaryTree(currentnode.left)
}
// if left not available, read it and try right,
// if right exists and hasn't been checked, run function from there
// if right !exist, go up to next unchecked (unread) parent, right, and continue
else {
ordered.push(currentnode.val);
currentnode.checked = true;
if (isEmpty(currentnode.right) == false && currentnode.right.checked == false) {
readBinaryTree(currentnode.right)
}
else {
readBinaryTree(currentnode.parent)
}
}
}
else if (currentnode.checked == true) {
readBinaryTree(currentnode.parent)
}
}
var result = runReadBinaryTree(keynode);
console.log(result)
// draw functions and logic
var framecount = 0;
var gameOver = false;
var scale = 20;
var keypressed = 0;
function init() {
keypressed = 0;
gameOver = false;
var game = setInterval(function() {
draw();
input();
logic();
//game over
if (gameOver == true ) {
clearInterval(game);
init();
}
}, 1000/15);
}
function draw() {
//clear console
process.stdout.write('\033c');
var print = false;
//draw the screen
// i = y axis, j = x axis
for (i = 0; i < scale; i++) {
for (j = 0; j < scale; j++) {
print = false;
if (i == 0) {
process.stdout.write("# ")
}
else if (i == scale - 1) {
process.stdout.write("# ")
}
else if (j == 0) {
process.stdout.write("# ")
}
else if (j == scale - 1) {
process.stdout.write("# ")
}
else {
// if (i == ypos && j == xpos) {
// process.stdout.write("0 ")
// print = true;
// }
// else if (i == fypos && j == fxpos) {
// process.stdout.write("F ")
// print = true;
// }
// for (k=0;k<tailx.length;k++) {
// if (tailx[k] == j && taily[k] == i) {
// process.stdout.write("o ")
// print = true;
// break;
// }
// }
if (print == false) {
process.stdout.write(" ")
}
}
}
process.stdout.write('\n')
}
}
function input() {
var stdin = process.stdin;
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');
stdin.on('data', function(key){
if (key == '\u001B\u005B\u0041') {
//process.stdout.write('up');
if (keypressed != 2) {
keypressed = 4;
}
}
if (key == '\u001B\u005B\u0043') {
//process.stdout.write('right');
if (keypressed != 1) {
keypressed = 3;
}
}
if (key == '\u001B\u005B\u0042') {
//process.stdout.write('down');
if (keypressed != 4) {
keypressed = 2;
}
}
if (key == '\u001B\u005B\u0044') {
//process.stdout.write('left');
if (keypressed != 3) {
keypressed = 1;
}
}
if (key == '\u0003') { process.exit(); } // ctrl-c
});
}
function logic() {
}
//run time
init();
// so, depending on