-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinaryTree.js
676 lines (574 loc) · 15.7 KB
/
BinaryTree.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
/**
* @author Jakub Melezinek
* @class Binary tree implementation.
*
* @public
* @constructor
* @param {String} name The name of the tree
*/
btv.BinaryTree = function(name) {
/**
* The name of the tree.
*
* @private
* @type {String}
*/
this.name = "tree"; // default name
this.setName(name); // if parameter name is given
/**
* The root node of the tree.
*
* @private
* @type {btv.BinaryTreeNode}
*/
this.root = null;
}
/**
* @public
* @param {String} name
*/
btv.BinaryTree.prototype.setName = function(name) {
if(name != null) {
this.name = name.toString();
}
}
/**
* @public
* @returns {String}
*/
btv.BinaryTree.prototype.getName = function() {
return this.name;
}
/**
* @public
* @param {btv.BinaryTreeNode} root
* @throws Throws exception if the given node has a parent.
*/
btv.BinaryTree.prototype.setRoot = function(root) {
// It is not necessary. Tree can mean subtree too, but static btv.BinaryTree.getIndex(btv.BinaryTreeNode) still count index from the root of whole tree.
if(root !== null && !root.isRoot()) {
throw new btv.BinaryTreeException("The given node has a parent. Cannot be the root of a tree!");
}
// unset old root
if(this.getRoot() !== null) {
this.getRoot().setParent(null);
}
this.root = root;
}
/**
* @public
* @returns {btv.BinaryTreeNode}
*/
btv.BinaryTree.prototype.getRoot = function() {
return this.root;
}
/**
* @public
* @returns {Number}
*/
btv.BinaryTree.prototype.getCount = function() {
return this.getCountRec(this.getRoot());
}
/**
* @private
* @param {btv.BinaryTreeNode} node
* @returns {Number}
*/
btv.BinaryTree.prototype.getCountRec = function(node) {
if(node == null) {
return 0;
}
return 1 + this.getCountRec(node.getLeft()) + this.getCountRec(node.getRight());
}
/**
* @public
* @param {Number[]} array
*/
btv.BinaryTree.prototype.build = function(array) {
if(array.length <= 0) {
this.setRoot(null);
return;
}
var node = (new btv.BinaryTreeNode(array[0]));
this.setRoot(node);
this.buildLeftRec(array, 1, node);
this.buildRightRec(array, 2, node);
}
/**
* @private
* @param {Number[]} array
* @param {Number} index
* @param {btv.BinaryTreeNode} parent
*/
btv.BinaryTree.prototype.buildLeftRec = function(array, index, parent) {
if(index > array.length - 1) {
return;
}
var node = new btv.BinaryTreeNode(array[index]);
parent.setLeft(node);
this.buildLeftRec(array, index*2 + 1, node);
this.buildRightRec(array, index*2 + 2, node);
}
/**
* @private
* @param {Number[]} array
* @param {Number} index
* @param {btv.BinaryTreeNode} parent
*/
btv.BinaryTree.prototype.buildRightRec = function(array, index, parent) {
if(index > array.length - 1) {
return;
}
var node = new btv.BinaryTreeNode(array[index]);
parent.setRight(node);
this.buildLeftRec(array, index*2 + 1, node);
this.buildRightRec(array, index*2 + 2, node);
}
/**
* @public
* @param {btv.BinaryTreeNode} node1
* @param {btv.BinaryTreeNode} node2
* @returns {Boolean} Returns true if nodes was swapped succesfully.
*/
btv.BinaryTree.prototype.swapNodes = function(node1, node2) {
if(node1 === node2 || node1 == null || node2 == null) {
return false;
}
// siblings
if(node1.getParent() === node2.getParent()) {
var tmp;
if(node1.getParent().getLeft() === node1) { // n1 is left child
tmp = node1;
node2.getParent().setLeft(node2);
node2.getParent().setRight(tmp);
} else { // n2 is left child
tmp = node2;
node1.getParent().setLeft(node1);
node1.getParent().setRight(tmp);
}
return true;
}
// child
if(node1.getLeft() === node2 || node1.getRight() === node2) { // n1 is parent n2
return this.swapParentChild(node1, node2);
}
if(node2.getLeft() === node1 || node2.getRight() === node1) { // n2 is parent n1
return this.swapParentChild(node2, node1);
}
// descendant (but not a child)
var parent = node1.getParent();
while(parent != null) {
if(parent === node2) { // n1 is descendant of n2
return this.swapAncestorDescendant(node2, node1);
}
parent = parent.getParent();
}
parent = node2.getParent();
while(parent != null) {
if(parent === node1) { // n1 is descendant of n2
return this.swapAncestorDescendant(node1, node2);
}
parent = parent.getParent();
}
// 2 subtrees => n1 nor n2 are root
var node2Left = node2.getLeft();
var node2Right = node2.getRight();
// set node2's new children
node2.setLeft(node1.getLeft());
node2.setRight(node1.getRight());
// set node1's new children
node1.setLeft(node2Left);
node1.setRight(node2Right);
// now subtrees swapped
// swap parents - n1 nor n2 are root
var node1parent = node1.getParent();
var node2parent = node2.getParent();
if(node1parent.getLeft() === node1) {
node1parent.setLeft(node2);
} else {
node1parent.setRight(node2);
}
if(node2parent.getLeft() === node2) {
node2parent.setLeft(node1);
} else {
node2parent.setRight(node1);
}
return true;
}
/**
* @private
* @param {btv.BinaryTreeNode} parent
* @param {btv.BinaryTreeNode} child
* @returns {Boolean} Returns true. Doesn't chceck if given child is child of given parent.
*/
btv.BinaryTree.prototype.swapParentChild = function(parent, child) {
var parentLeft = parent.getLeft();
var parentRight = parent.getRight();
// set parent's new child
parent.setLeft(child.getLeft());
parent.setRight(child.getRight());
// set child's parent
if(parent.isRoot()) {
this.setRoot(child);
} else {
if(parent.getParent().getLeft() === parent) {
parent.getParent().setLeft(child);
} else {
parent.getParent().setRight(child)
}
}
// set child's children
if(child === parentLeft) {
child.setLeft(parent);
child.setRight(parentRight);
} else {
child.setLeft(parentLeft);
child.setRight(parent);
}
return true;
}
/**
* @private
* @param {btv.BinaryTreeNode} ancestor
* @param {btv.BinaryTreeNode} descendant
* @returns {Boolean} Returns true. Doesn't chceck if given child is child of given parent.
*/
btv.BinaryTree.prototype.swapAncestorDescendant = function(ancestor, descendant) {
var ancestortLeft = ancestor.getLeft();
var ancestorRight = ancestor.getRight();
// set ancestor's new children
ancestor.setLeft(descendant.getLeft());
ancestor.setRight(descendant.getRight());
var ancestorParent = ancestor.getParent();
var descendantParent = descendant.getParent();
var descendantIsLeftChild;
if(descendantParent.getLeft() === descendant) { // descendantParent still refer to ancestor, used below
descendantIsLeftChild = true;
} else {
descendantIsLeftChild = false;
}
// set descendant's new children
descendant.setLeft(ancestortLeft);
descendant.setRight(ancestorRight);
// now subtrees swapped
// swap parents
// descendant new parent
if(ancestor.isRoot()) {
if(descendantParent.getLeft() === descendant) {
descendantParent.setLeft(null);
} else {
descendantParent.setRight(null);
}
this.setRoot(descendant);
} else {
if(ancestorParent.getLeft() === ancestor) { // ancestorParent still refer to ancestor
ancestorParent.setLeft(descendant);
} else {
ancestorParent.setRight(descendant);
}
}
// ancestor new parent
if(descendantIsLeftChild) { // descendantParent do not refer to descendant, becouse descendant parent was set to ancestorParent
descendantParent.setLeft(ancestor);
} else {
descendantParent.setRight(ancestor);
}
}
/**
* @public
* @returns {btv.BinaryTreeNode[]}
*/
btv.BinaryTree.prototype.toInorderArray = function() {
var array = new Array();
if(this.root === null) {
return null;
}
btv.BinaryTree.toInorderArrayRec(this.root, array);
return array;
}
/**
* @private
* @param {btv.BinaryTreeNode} node
* @param {Array} [array] Optional, if given push node to that array, otherwise create new array.
* @returns {btv.BinaryTreeNode[]} Returns given or new array.
*/
btv.BinaryTree.toInorderArrayRec = function(node, array) {
if(node === null) {
return null;
}
if(array == null) { // null or undefined
array = new Array
}
btv.BinaryTree.toInorderArrayRec(node.getLeft(), array);
array.push(node);
btv.BinaryTree.toInorderArrayRec(node.getRight(), array);
return array;
}
/**
* Simulate array implementation.
*
* @public
* @returns {btv.BinaryTreeNode[]}
*/
btv.BinaryTree.prototype.toArray = function() {
var array = new Array();
if(this.getRoot() == null) {
return array;
}
// pomoci getNode(index)
var maxIndex = 0;
var nullCount = 0;
for(var i = 0; i <= maxIndex; i++) {
array[i] = this.getNode(i);
if(array[i] !== null) {
maxIndex = 2*i + 2; // have to check left and right child
nullCount = 0;
} else {
nullCount++;
}
}
array.length -= nullCount; // trim nulls at the end
return array;
}
/**
* Simulate array implementation.
*
* @public
* @param {Number} index
* @throws Throws exception if this tree doesn't have root node.
* @throws Throws exception if given index is negative.
* @returns {btv.BinaryTreeNode}
*/
btv.BinaryTree.prototype.getNode = function(index) {
if(this.getRoot() === null) {
throw new btv.BinaryTreeException("This tree doesn't have a root node.");
}
if(index < 0) {
throw new btv.BinaryTreeException("The index cannot be negative.");
}
var path = "";
while(index != 0) {
if(index % 2 == 1) { // odd => left child
index = (index-1)/2;
path += "L";
}else { // even => left child
index = (index-2)/2;
path += "R";
}
}
var node = this.getRoot();
for(var i = path.length; i > 0; i--) { // backwards
if(path.charAt(i-1) == "L") {
node = node.getLeft();
} else {
node = node.getRight();
}
if(node === null) {
return null;
}
}
return node;
}
/**
* Simulate array implementation.
*
* @public
* @static
* @param {btv.BinaryTreeNode} node
* @throws Throws exception if given node is null.
* @returns {Number} Index of given node.
*/
btv.BinaryTree.getIndex = function(node) {
if(node === null) {
throw new btv.BinaryTreeException("Given node is null.");
}
if(node.isRoot()) // root
return 0;
// recursion
var parentIndex = node.getParent().getIndex();
if(node.getParent().getLeft() === node) { // this is left child
return 2*parentIndex + 1;
}
else { // this is right child
return 2*parentIndex + 2;
}
}
/**
* @public
* @override
* @returns {String}
*/
btv.BinaryTree.prototype.toString = function() {
return "[object btv.BinaryTree" +
" <name: '" + this.getName() + "'" +
" root: " + this.getRoot() + ">]";
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* @author Jakub Melezinek
* @class Binary tree node implementation.
*
* @public
* @constructor
* @param {Number|String} value The value of the node
*/
btv.BinaryTreeNode = function(value) {
/**
* The value of this node.
*
* @private
* @type {Number|String}
*/
this.value = value;
/**
* The parent of this node.
*
* @private
* @type {btv.BinaryTreeNode}
*/
this.parent = null;
/**
* The left child of this node.
*
* @private
* @type {btv.BinaryTreeNode}
*/
this.left = null;
/**
* The right child of this node.
*
* @private
* @type {btv.BinaryTreeNode}
*/
this.right = null;
}
/**
* @public
* @returns {Number}
*/
btv.BinaryTreeNode.prototype.getValue = function() {
return this.value;
}
/**
* @public
* @see static function btv.BinaryTree#getIndex
* @returns {Number}
*/
btv.BinaryTreeNode.prototype.getIndex = function() {
return btv.BinaryTree.getIndex(this);
}
/**
* @public
* @returns {Boolean}
*/
btv.BinaryTreeNode.prototype.isRoot = function() {
if(this.getParent() === null) {
return true;
} else {
return false;
}
}
/**
* @public
* @returns {Boolean}
*/
btv.BinaryTreeNode.prototype.isLeaf = function() {
if(this.getLeft() === null && this.getRight() === null) {
return true;
} else {
return false;
}
}
/**
* For method setLeft(BinaryTreeNode) and setRight(BinaryTreeNode) only!
* Unset old reference oldParent to thisNode and
* set new reference thisNode to newParent,
* DO NOT set reference newParent to thisNode!
* Use setLeft(BinaryTreeNode) or setRight(BinaryTreeNode).
*
* @private
* @see btv.BinaryTreeNode#setLeft
* @see btv.BinaryTreeNode#setRight
* @param {btv.BinaryTreeNode} parent
*/
btv.BinaryTreeNode.prototype.setParent = function(parent) {
// unset old reference
if(!this.isRoot()) { // has parent
if(this.getParent().getLeft() === this) { // this is left child
this.getParent().left = null;
}
else {// this is right child
this.getParent().right = null;
}
}
// set new reference
this.parent = parent;
}
/**
* @public
* @returns {btv.BinaryTreeNode}
*/
btv.BinaryTreeNode.prototype.getParent = function() {
return this.parent;
}
/**
* Set left child and changes all needed references.
*
* @public
* @throws Throws an exception if this node is same as given node - try to set yourself as a child of yourself.
* @param {btv.BinaryTreeNode} left
*/
btv.BinaryTreeNode.prototype.setLeft = function(left) {
if(this === left) {
throw new btv.BinaryTreeNodeException("A node cannot be a child of yourself.");
}
if(left !== null) {
left.setParent(this);
}
if(this.getLeft() !== null) {
this.getLeft().setParent(null);
}
this.left = left;
}
/**
* @public
* @returns {btv.BinaryTreeNode}
*/
btv.BinaryTreeNode.prototype.getLeft = function() {
return this.left;
}
/**
* Set right child and changes all needed references.
*
* @public
* @throws Throws an exception if this node is same as given node - try to set yourself as a child of yourself.
* @param {btv.BinaryTreeNode} right
*/
btv.BinaryTreeNode.prototype.setRight = function(right) {
if(this === right) {
throw new btv.BinaryTreeNodeException("A node cannot be a child of yourself.");
}
if(right !== null) {
right.setParent(this);
}
if(this.getRight() !== null) {
this.getRight().setParent(null);
}
this.right = right;
}
/**
* @public
* @returns {btv.BinaryTreeNode}
*/
btv.BinaryTreeNode.prototype.getRight = function() {
return this.right;
}
/**
* @public
* @override
* @returns {String}
*/
btv.BinaryTreeNode.prototype.toString = function() {
return "[object btv.BinaryTreeNode <value: " + this.getValue() + ", index: " + this.getIndex() + ">]";
}