class OSet {
has( key ) {
return !this.ik( this.root, key )
}
ik(p, key) {
if (p) {
if (p.key === key) return false
else {
this.up = p
if ( p.key < key ) {
this.rd = true
return this.ik( p.r, key )
} else {
this.rd = false
return this.ik( p.l, key )
}
}
} else return { key }
}
add( key ) {
if ( this.root ) {
var uo
if ( uo = this.ik( this.root, key )) {
if ( this.rd ) this.up.r = uo
else this.up.l = uo
return true
} else return false
} else {
this.root = { key }
}
}
ib( p, f ) {
if ( p ) {
this.ib( p.l, f )
f( p.key, this.i++ )
this.ib( p.r, f )
}
}
forEach( f ) {
this.i = 0
this.ib( this.root, f )
}
}
const m = new OSet()
const t = [7, 4, 1, 2, 14, 16, 6, 12, 5, 8, 18, 0, 5.5, 13]
t.forEach( v => m.add( v ) )
console.log( m )
m.forEach( console.log )
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
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