UNPKG

1.07 kBJavaScriptView Raw
1import { AbstractSet } from "../interface/AbstractSet";
2import { BasicBinaryTree } from "../tree/basic-binary-tree/BasicBinaryTree";
3import { RedBlackTree } from "../tree/red-black-tree/RedBlackTree";
4export class TreeSet extends AbstractSet {
5 constructor(compareKey) {
6 super();
7 this.size = 0;
8 this.tree = new RedBlackTree(compareKey);
9 }
10 add(item) {
11 if (this.tree.contains(item)) {
12 return this;
13 }
14 this.tree.insert(item);
15 this.size++;
16 return this;
17 }
18 entries() {
19 return BasicBinaryTree.inTraversal(this.tree.Root);
20 }
21 has(item) {
22 return this.tree.contains(item);
23 }
24 remove(item) {
25 if (!this.tree.contains(item)) {
26 return false;
27 }
28 this.size--;
29 return this.tree.remove(item);
30 }
31 diff(set) {
32 return super.diff(set);
33 }
34 union(set) {
35 return super.union(set);
36 }
37 intersect(set) {
38 return super.intersect(set);
39 }
40 get Size() {
41 return this.size;
42 }
43}