Skip to content

Commit bde18ef

Browse files
committed
Update MultiRedBlackTree.ts
1 parent 82f0c70 commit bde18ef

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

finding-mk-average/MultiRedBlackTree.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@ import { Direction } from "https://deno.land/std@0.173.0/collections/binary_sear
33
import { RedBlackNode } from "https://deno.land/std@0.173.0/collections/red_black_node.ts";
44
import RedBlackTreeExtended from "../dinner-plate-stacks/RedBlackTree.ts";
55

6+
67
export class MultiRedBlackTree<T> extends RedBlackTreeExtended<T> {
78
hash: (v: T) => any;
89
constructor(
910
compare: (a: T, b: T) => number = ascend,
10-
hash: (v: T) => any = (v) => v,
11+
hash: (v: T) => any = (v) => v
1112
) {
1213
super(compare);
1314
this.hash = hash;
1415
}
1516
value2count = new Map<any, number>();
1617

1718
getCount(value: T): number {
18-
return this.value2count.get(this.hash(value)) ?? 1;
19+
return this.value2count.get(this.hash(value)) ?? 0;
1920
}
2021
setCount(value: T, count: number) {
2122
return this.value2count.set(this.hash(value), count);
2223
}
24+
hasCount(value: T) {
25+
return (this.value2count.get(this.hash(value)) ?? 0) > 0;
26+
}
2327
root: RedBlackNode<T> | null = null;
2428
getRoot(): RedBlackNode<T> | null {
2529
return this.root;
@@ -29,9 +33,9 @@ export class MultiRedBlackTree<T> extends RedBlackTreeExtended<T> {
2933
}
3034
remove(value: T): boolean {
3135
// console.log("remove", value);
32-
const node = this.findNode(value);
36+
// const node = this.findNode(value);
3337

34-
if (!node) {
38+
if (!this.hasCount(value)) {
3539
return false;
3640
} else {
3741
// node.count--;
@@ -47,34 +51,34 @@ export class MultiRedBlackTree<T> extends RedBlackTreeExtended<T> {
4751
}
4852
}
4953
insert(value: T): boolean {
50-
const node = this.findNode(value);
54+
// const node = this.findNode(value);
5155

52-
if (node) {
56+
if (this.hasCount(value)) {
5357
// node.count++;
5458
this.setCount(value, (this.getCount(value) ?? 1) + 1);
5559
// console.log("increment", node);
5660
return true;
5761
} else {
58-
// this.insertGetNode(value);
59-
super.insert(value);
62+
this.insertGetNode(value);
63+
// super.insert(value);
64+
this.setCount(value, 1);
6065
}
6166
return true;
6267
}
6368
insertGetNode(value: T): RedBlackNode<T> | null {
6469
let node = this.insertNode(
6570
RedBlackNode,
66-
value,
71+
value
6772
) as RedBlackNode<T> | null;
6873
if (node) this.setCount(value, 1);
6974

7075
if (node) {
7176
while (node.parent?.red) {
7277
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";
7882
const uncle: RedBlackNode<T> | null =
7983
parent.parent![uncleDirection] ?? null;
8084

0 commit comments

Comments
 (0)