@@ -3,23 +3,27 @@ import { Direction } from "https://deno.land/std@0.173.0/collections/binary_sear
3
3
import { RedBlackNode } from "https://deno.land/std@0.173.0/collections/red_black_node.ts" ;
4
4
import RedBlackTreeExtended from "../dinner-plate-stacks/RedBlackTree.ts" ;
5
5
6
+
6
7
export class MultiRedBlackTree < T > extends RedBlackTreeExtended < T > {
7
8
hash : ( v : T ) => any ;
8
9
constructor (
9
10
compare : ( a : T , b : T ) => number = ascend ,
10
- hash : ( v : T ) => any = ( v ) => v ,
11
+ hash : ( v : T ) => any = ( v ) => v
11
12
) {
12
13
super ( compare ) ;
13
14
this . hash = hash ;
14
15
}
15
16
value2count = new Map < any , number > ( ) ;
16
17
17
18
getCount ( value : T ) : number {
18
- return this . value2count . get ( this . hash ( value ) ) ?? 1 ;
19
+ return this . value2count . get ( this . hash ( value ) ) ?? 0 ;
19
20
}
20
21
setCount ( value : T , count : number ) {
21
22
return this . value2count . set ( this . hash ( value ) , count ) ;
22
23
}
24
+ hasCount ( value : T ) {
25
+ return ( this . value2count . get ( this . hash ( value ) ) ?? 0 ) > 0 ;
26
+ }
23
27
root : RedBlackNode < T > | null = null ;
24
28
getRoot ( ) : RedBlackNode < T > | null {
25
29
return this . root ;
@@ -29,9 +33,9 @@ export class MultiRedBlackTree<T> extends RedBlackTreeExtended<T> {
29
33
}
30
34
remove ( value : T ) : boolean {
31
35
// console.log("remove", value);
32
- const node = this . findNode ( value ) ;
36
+ // const node = this.findNode(value);
33
37
34
- if ( ! node ) {
38
+ if ( ! this . hasCount ( value ) ) {
35
39
return false ;
36
40
} else {
37
41
// node.count--;
@@ -47,34 +51,34 @@ export class MultiRedBlackTree<T> extends RedBlackTreeExtended<T> {
47
51
}
48
52
}
49
53
insert ( value : T ) : boolean {
50
- const node = this . findNode ( value ) ;
54
+ // const node = this.findNode(value);
51
55
52
- if ( node ) {
56
+ if ( this . hasCount ( value ) ) {
53
57
// node.count++;
54
58
this . setCount ( value , ( this . getCount ( value ) ?? 1 ) + 1 ) ;
55
59
// console.log("increment", node);
56
60
return true ;
57
61
} else {
58
- // this.insertGetNode(value);
59
- super . insert ( value ) ;
62
+ this . insertGetNode ( value ) ;
63
+ // super.insert(value);
64
+ this . setCount ( value , 1 ) ;
60
65
}
61
66
return true ;
62
67
}
63
68
insertGetNode ( value : T ) : RedBlackNode < T > | null {
64
69
let node = this . insertNode (
65
70
RedBlackNode ,
66
- value ,
71
+ value
67
72
) as RedBlackNode < T > | null ;
68
73
if ( node ) this . setCount ( value , 1 ) ;
69
74
70
75
if ( node ) {
71
76
while ( node . parent ?. red ) {
72
77
let parent : RedBlackNode < T > = node . parent ! ;
73
- const parentDirection : Direction = parent
74
- . directionFromParent ( ) ! ;
75
- const uncleDirection : Direction = parentDirection === "right"
76
- ? "left"
77
- : "right" ;
78
+ const parentDirection : Direction =
79
+ parent . directionFromParent ( ) ! ;
80
+ const uncleDirection : Direction =
81
+ parentDirection === "right" ? "left" : "right" ;
78
82
const uncle : RedBlackNode < T > | null =
79
83
parent . parent ! [ uncleDirection ] ?? null ;
80
84
0 commit comments