semantic-typescript 0.5.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/asynchronous/collector.d.ts +231 -0
  2. package/dist/asynchronous/collector.js +800 -0
  3. package/dist/asynchronous/semantic.d.ts +257 -0
  4. package/dist/asynchronous/semantic.js +1853 -0
  5. package/dist/factory.d.ts +71 -37
  6. package/dist/factory.js +443 -262
  7. package/dist/guard.d.ts +24 -14
  8. package/dist/guard.js +73 -19
  9. package/dist/hook.d.ts +6 -6
  10. package/dist/hook.js +2 -2
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.js +2 -2
  13. package/dist/optional.d.ts +2 -2
  14. package/dist/symbol.d.ts +19 -10
  15. package/dist/symbol.js +19 -10
  16. package/dist/synchronous/collector.d.ts +232 -0
  17. package/dist/{collector.js → synchronous/collector.js} +160 -151
  18. package/dist/{semantic.d.ts → synchronous/semantic.d.ts} +111 -120
  19. package/dist/{semantic.js → synchronous/semantic.js} +299 -337
  20. package/dist/utility.d.ts +7 -1
  21. package/dist/utility.js +1 -0
  22. package/package.json +1 -1
  23. package/readme.cn.md +158 -697
  24. package/readme.de.md +163 -432
  25. package/readme.es.md +163 -433
  26. package/readme.fr.md +162 -444
  27. package/readme.jp.md +162 -442
  28. package/readme.kr.md +161 -430
  29. package/readme.md +157 -1009
  30. package/readme.ru.md +161 -426
  31. package/readme.tw.md +161 -436
  32. package/dist/collector.d.ts +0 -236
  33. package/dist/main.d.ts +0 -1
  34. package/dist/main.js +0 -4
  35. package/dist/map.d.ts +0 -76
  36. package/dist/map.js +0 -245
  37. package/dist/node.d.ts +0 -182
  38. package/dist/node.js +0 -918
  39. package/dist/set.d.ts +0 -19
  40. package/dist/set.js +0 -65
  41. package/dist/tree.d.ts +0 -82
  42. package/dist/tree.js +0 -257
package/dist/node.js DELETED
@@ -1,918 +0,0 @@
1
- import { isObject, isOptional } from "./guard";
2
- import { useCompare } from "./hook";
3
- import { Optional } from "./optional";
4
- import { LinearNodeSymbol, NodeSymbol, RedBlackNodeSymbol } from "./symbol";
5
- import { invalidate, validate } from "./utility";
6
- ;
7
- export class AbstractNode {
8
- Node = NodeSymbol;
9
- constructor() {
10
- }
11
- *ancestors() {
12
- let parent = this.parent();
13
- if (parent.isPresent()) {
14
- let value = parent.get();
15
- yield value;
16
- yield* value.ancestors();
17
- }
18
- }
19
- *descendants() {
20
- for (let child of this.children()) {
21
- yield child;
22
- yield* child.descendants();
23
- }
24
- }
25
- root() {
26
- let parent = this.parent();
27
- if (parent.isPresent()) {
28
- return parent.get().root();
29
- }
30
- return this;
31
- }
32
- *siblings() {
33
- let parent = this.parent();
34
- if (parent.isPresent()) {
35
- for (let sibling of parent.get().children()) {
36
- if (sibling !== this && !Object.is(sibling, this)) {
37
- yield sibling;
38
- }
39
- }
40
- }
41
- }
42
- previousSibling() {
43
- let parent = this.parent();
44
- if (parent.isPresent()) {
45
- let siblings = Array.from(parent.get().children());
46
- let index = siblings.indexOf(this);
47
- if (index > 0) {
48
- return Optional.of(siblings[index - 1]);
49
- }
50
- }
51
- return Optional.empty();
52
- }
53
- nextSibling() {
54
- let parent = this.parent();
55
- if (parent.isPresent()) {
56
- let siblings = Array.from(parent.get().children());
57
- let index = siblings.indexOf(this);
58
- if (index < siblings.length - 1) {
59
- return Optional.of(siblings[index + 1]);
60
- }
61
- }
62
- return Optional.empty();
63
- }
64
- *preorder() {
65
- yield this;
66
- for (let child of this.children()) {
67
- yield* child.preorder();
68
- }
69
- }
70
- *inorder() {
71
- let children = Array.from(this.children());
72
- let middle = Math.floor(children.length / 2);
73
- if (children.length > 0) {
74
- for (let index = 0; index < middle; index++) {
75
- yield* children[index].inorder();
76
- }
77
- }
78
- yield this;
79
- for (let index = middle; index < children.length; index++) {
80
- yield* children[index].inorder();
81
- }
82
- }
83
- *postorder() {
84
- for (let child of this.children()) {
85
- yield* child.postorder();
86
- }
87
- yield this;
88
- }
89
- *breadthfirst() {
90
- let queue = [this];
91
- while (queue.length > 0) {
92
- let node = queue.shift();
93
- yield node;
94
- for (let child of node.children()) {
95
- queue.push(child);
96
- }
97
- }
98
- }
99
- *[Symbol.iterator]() {
100
- yield* this.preorder();
101
- }
102
- depth() {
103
- let parent = this.parent();
104
- return parent.isPresent() ? 1n + parent.get().depth() : 0n;
105
- }
106
- height() {
107
- if (this.isLeaf()) {
108
- return -1n;
109
- }
110
- return [...this.children()].reduce((maximum, child) => {
111
- if (maximum < child.height()) {
112
- return child.height() + 1n;
113
- }
114
- return maximum + 1n;
115
- }, 0n);
116
- }
117
- width() {
118
- if (this.isLeaf()) {
119
- return 0n;
120
- }
121
- return [...this.children()].reduce((sum, child) => {
122
- return sum + child.width() + 1n;
123
- }, 0n);
124
- }
125
- level() {
126
- let parent = this.parent();
127
- if (parent.isPresent()) {
128
- return parent.get().level() + 1n;
129
- }
130
- return 0n;
131
- }
132
- isLeaf() {
133
- return [...this.children()].length === 0;
134
- }
135
- isRoot() {
136
- return this.parent().isEmpty();
137
- }
138
- }
139
- ;
140
- export class LinearNode extends AbstractNode {
141
- previous;
142
- next;
143
- element;
144
- LinearNode = LinearNodeSymbol;
145
- constructor(element, previous, next) {
146
- super();
147
- this.element = Optional.of(element);
148
- this.previous = previous || Optional.empty();
149
- this.next = next || Optional.empty();
150
- }
151
- *ancestors() {
152
- let parent = this.parent();
153
- if (parent.isPresent()) {
154
- yield parent.get();
155
- yield* parent.get().ancestors();
156
- }
157
- }
158
- *descendants() {
159
- if (validate(this.next) && this.next.isPresent()) {
160
- yield this.next.get();
161
- yield* this.next.get().descendants();
162
- }
163
- }
164
- root() {
165
- let parent = this.parent();
166
- if (parent.isPresent()) {
167
- return parent.get().root();
168
- }
169
- return this;
170
- }
171
- *siblings() {
172
- let root = this.root();
173
- if (validate(root)) {
174
- yield root;
175
- let next = this.nextSibling();
176
- if (validate(next) && next.isPresent()) {
177
- yield* next.get().siblings();
178
- }
179
- }
180
- }
181
- *children() {
182
- if (validate(this.next) && this.next.isPresent()) {
183
- yield this.next.get();
184
- yield* this.next.get().children();
185
- }
186
- }
187
- parent() {
188
- return validate(this.previous) ? this.previous : Optional.empty();
189
- }
190
- *[Symbol.iterator]() {
191
- let root = this.root();
192
- if (validate(root)) {
193
- yield root;
194
- }
195
- while (validate(root) && validate(root.next) && root.next.isPresent()) {
196
- root = root.next.get();
197
- yield root;
198
- }
199
- }
200
- getElement() {
201
- return validate(this.element) ? this.element : Optional.empty();
202
- }
203
- setElement(element) {
204
- if (validate(element)) {
205
- this.element = Optional.of(element);
206
- }
207
- }
208
- getPrevious() {
209
- return validate(this.previous) ? this.previous : Optional.empty();
210
- }
211
- setPrevious(previous) {
212
- if (validate(previous)) {
213
- if (isOptional(previous)) {
214
- this.previous = previous;
215
- }
216
- else if (isObject(previous)) {
217
- this.previous = Optional.of(previous);
218
- }
219
- }
220
- }
221
- getNext() {
222
- return validate(this.next) ? this.next : Optional.empty();
223
- }
224
- setNext(next) {
225
- if (validate(next)) {
226
- if (isOptional(next)) {
227
- this.next = next;
228
- }
229
- else if (isObject(next)) {
230
- this.next = Optional.of(next);
231
- }
232
- }
233
- }
234
- linkPrevious(previous) {
235
- if (validate(previous)) {
236
- this.setPrevious(previous);
237
- previous.setNext(this);
238
- }
239
- }
240
- unlinkPrevious() {
241
- if (validate(this.previous) && this.previous.isPresent()) {
242
- if (validate(this.next) && this.next.isPresent()) {
243
- this.previous.get().setNext(this.next.get());
244
- }
245
- else {
246
- this.previous.get().setNext(Optional.empty());
247
- }
248
- }
249
- }
250
- linkNext(next) {
251
- if (validate(next)) {
252
- this.setNext(next);
253
- next.setPrevious(this);
254
- }
255
- }
256
- unlinkNext() {
257
- if (validate(this.next) && this.next.isPresent()) {
258
- if (validate(this.previous) && this.previous.isPresent()) {
259
- this.next.get().setPrevious(this.previous.get());
260
- }
261
- else {
262
- this.next.get().setPrevious(Optional.empty());
263
- }
264
- }
265
- }
266
- }
267
- ;
268
- export class BinaryNode extends AbstractNode {
269
- BinaryNode = Symbol("BinaryNode");
270
- ancestor;
271
- left;
272
- right;
273
- element;
274
- constructor(element, ancestor, left, right) {
275
- super();
276
- this.element = Optional.of(element);
277
- this.ancestor = ancestor || Optional.empty();
278
- this.left = left || Optional.empty();
279
- this.right = right || Optional.empty();
280
- }
281
- *ancestors() {
282
- let parent = this.parent();
283
- if (parent.isPresent()) {
284
- yield parent.get();
285
- yield* parent.get().ancestors();
286
- }
287
- }
288
- compare(other, comparator) {
289
- if (invalidate(this.element)) {
290
- if (invalidate(other) || invalidate(other.element)) {
291
- return 0n;
292
- }
293
- return -1n;
294
- }
295
- if (this.element.isPresent()) {
296
- comparator = comparator || useCompare;
297
- if (validate(other.element) && other.element.isPresent()) {
298
- return BigInt(comparator(this.element.get(), other.element.get()));
299
- }
300
- else {
301
- return 1n;
302
- }
303
- }
304
- return -1n;
305
- }
306
- *descendants() {
307
- if (validate(this.left) && this.left.isPresent()) {
308
- yield this.left.get();
309
- yield* this.left.get().descendants();
310
- }
311
- if (validate(this.right) && this.right.isPresent()) {
312
- yield this.right.get();
313
- yield* this.right.get().descendants();
314
- }
315
- }
316
- root() {
317
- let parent = this.parent();
318
- if (parent.isPresent()) {
319
- return parent.get().root();
320
- }
321
- return this.identity();
322
- }
323
- *siblings() {
324
- let parent = this.parent();
325
- if (parent.isPresent()) {
326
- let siblings = Array.from(parent.get().children());
327
- let index = siblings.indexOf(this);
328
- if (index > 0) {
329
- yield siblings[index - 1];
330
- }
331
- if (index < siblings.length - 1) {
332
- yield siblings[index + 1];
333
- }
334
- }
335
- }
336
- *children() {
337
- if (validate(this.left) && this.left.isPresent()) {
338
- yield this.left.get();
339
- }
340
- if (validate(this.right) && this.right.isPresent()) {
341
- yield this.right.get();
342
- }
343
- }
344
- parent() {
345
- return validate(this.parent) ? this.ancestor : Optional.empty();
346
- }
347
- *[Symbol.iterator]() {
348
- yield* this.preorder();
349
- }
350
- *preorder() {
351
- yield this;
352
- if (validate(this.left) && this.left.isPresent()) {
353
- yield* this.left.get().preorder();
354
- }
355
- if (validate(this.right) && this.right.isPresent()) {
356
- yield* this.right.get().preorder();
357
- }
358
- }
359
- *inorder() {
360
- if (validate(this.left) && this.left.isPresent()) {
361
- yield* this.left.get().inorder();
362
- }
363
- yield this;
364
- if (validate(this.right) && this.right.isPresent()) {
365
- yield* this.right.get().inorder();
366
- }
367
- }
368
- *postorder() {
369
- if (validate(this.left) && this.left.isPresent()) {
370
- yield* this.left.get().postorder();
371
- }
372
- if (validate(this.right) && this.right.isPresent()) {
373
- yield* this.right.get().postorder();
374
- }
375
- yield this.identity();
376
- }
377
- *breadthfirst() {
378
- let queue = [this.identity()];
379
- while (queue.length > 0) {
380
- let node = queue.shift();
381
- yield node;
382
- if (validate(node.left) && node.left.isPresent()) {
383
- queue.push(node.left.get());
384
- }
385
- if (validate(node.right) && node.right.isPresent()) {
386
- queue.push(node.right.get());
387
- }
388
- }
389
- }
390
- getElement() {
391
- return validate(this.element) ? this.element : Optional.empty();
392
- }
393
- setElement(element) {
394
- if (validate(element)) {
395
- this.element = Optional.of(element);
396
- }
397
- }
398
- getLeft() {
399
- return validate(this.left) ? this.left : Optional.empty();
400
- }
401
- setLeft(left) {
402
- if (validate(left)) {
403
- if (isOptional(left)) {
404
- this.left = left;
405
- }
406
- else if (isObject(left)) {
407
- this.left = Optional.of(left);
408
- }
409
- }
410
- }
411
- getRight() {
412
- return validate(this.right) ? this.right : Optional.empty();
413
- }
414
- setRight(right) {
415
- if (validate(right)) {
416
- if (isOptional(right)) {
417
- this.right = right;
418
- }
419
- else if (isObject(right)) {
420
- this.right = Optional.of(right);
421
- }
422
- }
423
- }
424
- getAncestor() {
425
- return validate(this.ancestor) ? this.ancestor : Optional.empty();
426
- }
427
- setAncestor(ancestor) {
428
- if (validate(ancestor)) {
429
- if (isOptional(ancestor)) {
430
- this.ancestor = ancestor;
431
- }
432
- else if (isObject(ancestor)) {
433
- this.ancestor = Optional.of(ancestor);
434
- }
435
- }
436
- }
437
- linkLeft(left) {
438
- if (validate(left)) {
439
- this.setLeft(left);
440
- left.setAncestor(this);
441
- }
442
- }
443
- unlinkLeft() {
444
- if (validate(this.left) && this.left.isPresent()) {
445
- let left = this.left;
446
- if (validate(left) && left.isPresent()) {
447
- left.get().setAncestor(Optional.empty());
448
- }
449
- return left;
450
- }
451
- this.left = Optional.empty();
452
- return this.left;
453
- }
454
- linkRight(right) {
455
- if (validate(right)) {
456
- this.setRight(right);
457
- right.setAncestor(this);
458
- }
459
- }
460
- unlinkRight() {
461
- if (validate(this.right) && this.right.isPresent()) {
462
- let right = this.right;
463
- if (validate(right) && right.isPresent()) {
464
- right.get().setAncestor(Optional.empty());
465
- }
466
- return right;
467
- }
468
- this.right = Optional.empty();
469
- return this.right;
470
- }
471
- isLeftChild() {
472
- if (validate(this.ancestor)) {
473
- return this.ancestor.map((parent) => {
474
- let left = parent.getLeft();
475
- if (validate(left) && left.isPresent()) {
476
- return (left.get() === this) || Object.is(left, this);
477
- }
478
- return false;
479
- }).get(false);
480
- }
481
- return false;
482
- }
483
- isRightChild() {
484
- if (validate(this.ancestor)) {
485
- return this.ancestor.map((parent) => {
486
- let right = parent.getRight();
487
- if (validate(right) && right.isPresent()) {
488
- return (right.get() === this) || Object.is(right, this);
489
- }
490
- return false;
491
- }).get(false);
492
- }
493
- return false;
494
- }
495
- detach() {
496
- let parent = this.parent();
497
- if (parent.isPresent()) {
498
- if (this.isLeftChild()) {
499
- parent.get().unlinkLeft();
500
- }
501
- if (this.isRightChild()) {
502
- parent.get().unlinkRight();
503
- }
504
- }
505
- }
506
- isBanlanced() {
507
- if (this.isLeaf()) {
508
- return true;
509
- }
510
- if (this.left.isPresent() && this.right.isPresent()) {
511
- return this.left.get().height() === this.right.get().height();
512
- }
513
- return false;
514
- }
515
- isFull() {
516
- if (this.isLeaf()) {
517
- return true;
518
- }
519
- if (this.left.isPresent() && this.right.isPresent()) {
520
- return this.left.get().isFull() && this.right.get().isFull();
521
- }
522
- return false;
523
- }
524
- isComplete() {
525
- if (this.isLeaf()) {
526
- return true;
527
- }
528
- if (this.left.isPresent() && this.right.isPresent()) {
529
- return this.left.get().isComplete() && this.right.get().isComplete();
530
- }
531
- return false;
532
- }
533
- isPerfect() {
534
- if (this.isLeaf()) {
535
- return true;
536
- }
537
- if (this.left.isPresent() && this.right.isPresent()) {
538
- return this.left.get().isPerfect() && this.right.get().isPerfect();
539
- }
540
- return false;
541
- }
542
- invert(deep = false) {
543
- if (deep === true) {
544
- if (validate(this.left) && this.left.isPresent()) {
545
- this.left.get().invert(deep);
546
- }
547
- if (validate(this.right) && this.right.isPresent()) {
548
- this.right.get().invert(deep);
549
- }
550
- }
551
- else {
552
- let left = this.left;
553
- let right = this.right;
554
- this.left = right;
555
- this.right = left;
556
- }
557
- }
558
- }
559
- ;
560
- export class RedBlackNode extends BinaryNode {
561
- RedBlackNode = RedBlackNodeSymbol;
562
- color;
563
- constructor(element, color = "RED", ancestor, left, right) {
564
- super(element, ancestor || Optional.empty(), left || Optional.empty(), right || Optional.empty());
565
- this.color = color;
566
- }
567
- identity() {
568
- return this;
569
- }
570
- getElement() {
571
- return validate(this.element) ? this.element : Optional.empty();
572
- }
573
- setElement(element) {
574
- if (validate(element)) {
575
- this.element = Optional.of(element);
576
- }
577
- }
578
- getLeft() {
579
- return validate(this.left) ? this.left : Optional.empty();
580
- }
581
- getRight() {
582
- return validate(this.right) ? this.right : Optional.empty();
583
- }
584
- getColor() {
585
- return this.color;
586
- }
587
- setColor(color) {
588
- this.color = color;
589
- }
590
- isRed() {
591
- return this.color === "RED";
592
- }
593
- isBlack() {
594
- return this.color === "BLACK";
595
- }
596
- turnRed() {
597
- this.color = "RED";
598
- }
599
- turnBlack() {
600
- this.color = "BLACK";
601
- }
602
- toggle() {
603
- if (this.isRed()) {
604
- this.turnBlack();
605
- }
606
- else {
607
- this.turnRed();
608
- }
609
- }
610
- compareAndLink(other, comparator) {
611
- if (invalidate(this.element)) {
612
- if (invalidate(other) || invalidate(other.element)) {
613
- return;
614
- }
615
- this.linkLeft(other);
616
- return;
617
- }
618
- if (validate(other) && validate(other.element)) {
619
- comparator = comparator || useCompare;
620
- if (this.compare(other, comparator) < 0) {
621
- if (validate(this.left) && this.left.isPresent()) {
622
- this.left.get().compareAndLink(other, comparator);
623
- }
624
- else {
625
- this.linkLeft(other);
626
- this.fix();
627
- }
628
- }
629
- else {
630
- if (validate(this.right) && this.right.isPresent()) {
631
- this.right.get().compareAndLink(other, comparator);
632
- }
633
- else {
634
- this.linkRight(other);
635
- this.fix();
636
- }
637
- }
638
- }
639
- }
640
- rotate() {
641
- if (this.isRed() && this.parent().isPresent() && this.parent().get().isRed()) {
642
- let grandparent = this.parent().get().parent();
643
- if (validate(grandparent) && grandparent.isPresent()) {
644
- if (this.parent().get() === grandparent.get().left.get()) {
645
- grandparent.get().rotateRight();
646
- }
647
- else {
648
- grandparent.get().rotateLeft();
649
- }
650
- }
651
- }
652
- }
653
- rotateLeft() {
654
- if (validate(this.right) && this.right.isPresent()) {
655
- let newRoot = this.right.get();
656
- this.linkRight(newRoot);
657
- let parent = this.parent();
658
- if (validate(parent) && parent.isPresent()) {
659
- if (this.isLeftChild()) {
660
- parent.get().linkLeft(newRoot);
661
- }
662
- if (this.isRightChild()) {
663
- parent.get().linkRight(newRoot);
664
- }
665
- }
666
- newRoot.linkLeft(this);
667
- return newRoot;
668
- }
669
- return this;
670
- }
671
- rotateRight() {
672
- if (validate(this.left) && this.left.isPresent()) {
673
- let newRoot = this.left.get();
674
- this.linkLeft(newRoot);
675
- let parent = this.parent();
676
- if (validate(parent) && parent.isPresent()) {
677
- if (this.isLeftChild()) {
678
- parent.get().linkLeft(newRoot);
679
- }
680
- if (this.isRightChild()) {
681
- parent.get().linkRight(newRoot);
682
- }
683
- }
684
- newRoot.linkRight(this);
685
- return newRoot;
686
- }
687
- return this;
688
- }
689
- fix() {
690
- let current = this;
691
- let parent = current.parent();
692
- let grandparent = Optional.empty();
693
- let uncle = Optional.empty();
694
- while (parent.isPresent() && parent.get().isRed()) {
695
- grandparent = parent.get().parent();
696
- if (grandparent.isEmpty()) {
697
- break;
698
- }
699
- if (parent.get().isLeftChild()) {
700
- uncle = grandparent.get().right;
701
- if (uncle.isPresent() && uncle.get().isRed()) {
702
- parent.get().turnBlack();
703
- uncle.get().turnBlack();
704
- grandparent.get().turnRed();
705
- current = grandparent.get();
706
- }
707
- else {
708
- if (current === parent.get().right.orElse(null)) {
709
- current = parent.get();
710
- current.rotateLeft();
711
- parent = current.parent();
712
- }
713
- parent.get().turnBlack();
714
- grandparent.get().turnRed();
715
- grandparent.get().rotateRight();
716
- }
717
- }
718
- else {
719
- uncle = grandparent.get().left;
720
- if (uncle.isPresent() && uncle.get().isRed()) {
721
- parent.get().turnBlack();
722
- uncle.get().turnBlack();
723
- grandparent.get().turnRed();
724
- current = grandparent.get();
725
- }
726
- else {
727
- if (current === parent.get().left.orElse(null)) {
728
- current = parent.get();
729
- current.rotateRight();
730
- parent = current.parent();
731
- }
732
- parent.get().turnBlack();
733
- grandparent.get().turnRed();
734
- grandparent.get().rotateLeft();
735
- }
736
- }
737
- parent = current.parent();
738
- }
739
- let root = current.root();
740
- root.turnBlack();
741
- }
742
- uncle() {
743
- let parent = this.parent();
744
- if (invalidate(parent) || parent.isEmpty()) {
745
- return Optional.empty();
746
- }
747
- let grandparent = parent.get().parent();
748
- if (invalidate(grandparent) || grandparent.isEmpty()) {
749
- return Optional.empty();
750
- }
751
- return parent.flat((value) => {
752
- if (value.isLeftChild()) {
753
- return grandparent.get().right;
754
- }
755
- return grandparent.get().left;
756
- });
757
- }
758
- findMinimum() {
759
- let current = this;
760
- while (current.left.isPresent()) {
761
- current = current.left.get();
762
- }
763
- return current;
764
- }
765
- findMaximum() {
766
- let current = this;
767
- while (current.right.isPresent()) {
768
- current = current.right.get();
769
- }
770
- return current;
771
- }
772
- }
773
- ;
774
- export class AverageLevelNode extends BinaryNode {
775
- constructor(element, ancestor, left, right) {
776
- super(element, ancestor || Optional.empty(), left || Optional.empty(), right || Optional.empty());
777
- }
778
- *[Symbol.iterator]() {
779
- yield* this.preorder();
780
- }
781
- identity() {
782
- return this;
783
- }
784
- getElement() {
785
- return validate(this.element) ? this.element : Optional.empty();
786
- }
787
- setElement(element) {
788
- if (validate(element)) {
789
- this.element = Optional.of(element);
790
- }
791
- }
792
- getLeft() {
793
- return validate(this.left) ? this.left : Optional.empty();
794
- }
795
- getRight() {
796
- return validate(this.right) ? this.right : Optional.empty();
797
- }
798
- }
799
- ;
800
- export class BinarySearchNode extends AbstractNode {
801
- left;
802
- right;
803
- element;
804
- constructor(element, left, right) {
805
- super();
806
- this.left = left || Optional.empty();
807
- this.right = right || Optional.empty();
808
- this.element = Optional.of(element);
809
- }
810
- *ancestors() {
811
- let parent = this.parent();
812
- if (parent.isPresent()) {
813
- yield parent.get();
814
- yield* parent.get().ancestors();
815
- }
816
- }
817
- *descendants() {
818
- if (validate(this.left) && this.left.isPresent()) {
819
- yield this.left.get();
820
- yield* this.left.get().descendants();
821
- }
822
- if (validate(this.right) && this.right.isPresent()) {
823
- yield this.right.get();
824
- yield* this.right.get().descendants();
825
- }
826
- }
827
- root() {
828
- let parent = this.parent();
829
- if (parent.isPresent()) {
830
- return parent.get().root();
831
- }
832
- return this;
833
- }
834
- *siblings() {
835
- let parent = this.parent();
836
- if (parent.isPresent()) {
837
- let siblings = Array.from(parent.get().children());
838
- let index = siblings.indexOf(this);
839
- if (index > 0) {
840
- yield siblings[index - 1];
841
- }
842
- if (index < siblings.length - 1) {
843
- yield siblings[index + 1];
844
- }
845
- }
846
- }
847
- *children() {
848
- if (validate(this.left) && this.left.isPresent()) {
849
- yield this.left.get();
850
- yield* this.left.get().children();
851
- }
852
- if (validate(this.right) && this.right.isPresent()) {
853
- yield this.right.get();
854
- yield* this.right.get().children();
855
- }
856
- }
857
- parent() {
858
- return Optional.empty();
859
- }
860
- *[Symbol.iterator]() {
861
- yield* this.inorder();
862
- }
863
- *inorder() {
864
- if (validate(this.left) && this.left.isPresent()) {
865
- yield* this.left.get().inorder();
866
- }
867
- yield this;
868
- if (validate(this.right) && this.right.isPresent()) {
869
- yield* this.right.get().inorder();
870
- }
871
- }
872
- *preorder() {
873
- yield this;
874
- if (validate(this.left) && this.left.isPresent()) {
875
- yield* this.left.get().preorder();
876
- }
877
- if (validate(this.right) && this.right.isPresent()) {
878
- yield* this.right.get().preorder();
879
- }
880
- }
881
- *postorder() {
882
- if (validate(this.left) && this.left.isPresent()) {
883
- yield* this.left.get().postorder();
884
- }
885
- if (validate(this.right) && this.right.isPresent()) {
886
- yield* this.right.get().postorder();
887
- }
888
- yield this;
889
- }
890
- *breadthfirst() {
891
- let queue = [this];
892
- while (queue.length > 0) {
893
- let node = queue.shift();
894
- yield node;
895
- if (validate(node.left) && node.left.isPresent()) {
896
- queue.push(node.left.get());
897
- }
898
- if (validate(node.right) && node.right.isPresent()) {
899
- queue.push(node.right.get());
900
- }
901
- }
902
- }
903
- getElement() {
904
- return validate(this.element) ? this.element : Optional.empty();
905
- }
906
- setElement(element) {
907
- if (validate(element)) {
908
- this.element = Optional.of(element);
909
- }
910
- }
911
- getLeft() {
912
- return validate(this.left) ? this.left : Optional.empty();
913
- }
914
- getRight() {
915
- return validate(this.right) ? this.right : Optional.empty();
916
- }
917
- }
918
- ;