Skip to content

Commit 82f0c70

Browse files
committed
value2count
1 parent 4b548dc commit 82f0c70

File tree

2 files changed

+62
-51
lines changed

2 files changed

+62
-51
lines changed

finding-mk-average/MultiRedBlackTree.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
import { ascend } from "https://deno.land/std@0.173.0/collections/_comparators.ts";
12
import { Direction } from "https://deno.land/std@0.173.0/collections/binary_search_node.ts";
23
import { RedBlackNode } from "https://deno.land/std@0.173.0/collections/red_black_node.ts";
34
import RedBlackTreeExtended from "../dinner-plate-stacks/RedBlackTree.ts";
45

56
export class MultiRedBlackTree<T> extends RedBlackTreeExtended<T> {
6-
static node2count = new WeakMap<RedBlackNode<any>, number>();
7+
hash: (v: T) => any;
8+
constructor(
9+
compare: (a: T, b: T) => number = ascend,
10+
hash: (v: T) => any = (v) => v,
11+
) {
12+
super(compare);
13+
this.hash = hash;
14+
}
15+
value2count = new Map<any, number>();
716

8-
static getCount(node: RedBlackNode<any>): number {
9-
return MultiRedBlackTree.node2count.get(node) ?? 1;
17+
getCount(value: T): number {
18+
return this.value2count.get(this.hash(value)) ?? 1;
19+
}
20+
setCount(value: T, count: number) {
21+
return this.value2count.set(this.hash(value), count);
1022
}
1123
root: RedBlackNode<T> | null = null;
1224
getRoot(): RedBlackNode<T> | null {
@@ -16,21 +28,20 @@ export class MultiRedBlackTree<T> extends RedBlackTreeExtended<T> {
1628
return super.findNode(value) as RedBlackNode<T> | null;
1729
}
1830
remove(value: T): boolean {
31+
// console.log("remove", value);
1932
const node = this.findNode(value);
2033

2134
if (!node) {
2235
return false;
2336
} else {
2437
// node.count--;
25-
MultiRedBlackTree.node2count.set(
26-
node,
27-
(MultiRedBlackTree.node2count.get(node) ?? 1) - 1,
28-
);
29-
console.log("decrement", node);
38+
this.setCount(value, (this.getCount(value) ?? 1) - 1);
39+
// console.log("decrement", node);
3040
// console.trace();
31-
if ((MultiRedBlackTree.node2count.get(node) ?? 1) <= 0) {
41+
if ((this.getCount(value) ?? 1) <= 0) {
3242
// if (node.count <= 0) {
33-
this.removeTreeNode(node);
43+
// this.removeTreeNode(node);
44+
super.remove(value);
3445
}
3546
return true;
3647
}
@@ -40,14 +51,12 @@ export class MultiRedBlackTree<T> extends RedBlackTreeExtended<T> {
4051

4152
if (node) {
4253
// node.count++;
43-
MultiRedBlackTree.node2count.set(
44-
node,
45-
(MultiRedBlackTree.node2count.get(node) ?? 1) + 1,
46-
);
47-
console.log("increment", node);
54+
this.setCount(value, (this.getCount(value) ?? 1) + 1);
55+
// console.log("increment", node);
4856
return true;
4957
} else {
50-
this.insertGetNode(value);
58+
// this.insertGetNode(value);
59+
super.insert(value);
5160
}
5261
return true;
5362
}
@@ -56,7 +65,7 @@ export class MultiRedBlackTree<T> extends RedBlackTreeExtended<T> {
5665
RedBlackNode,
5766
value,
5867
) as RedBlackNode<T> | null;
59-
if (node) MultiRedBlackTree.node2count.set(node, 1);
68+
if (node) this.setCount(value, 1);
6069

6170
if (node) {
6271
while (node.parent?.red) {

finding-mk-average/index.ts

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,39 @@ class MKAverage {
1616
}
1717

1818
addElement(num: number) {
19-
if (this.queue.length === this.m) {
20-
const willBeDelete: number = this.queue[0];
21-
this.queue.shift();
22-
console.log("deleteElement", willBeDelete);
23-
this.total -= willBeDelete;
24-
this.tree.remove(willBeDelete);
25-
const root = this.tree.getRoot();
26-
if (!root) throw Error("null root");
27-
{
28-
const temp: [number, number][] = [];
29-
for (const node of InOrderIterator(root)) {
30-
temp.push([node.value, MultiRedBlackTree.getCount(node)]);
31-
}
32-
console.log(JSON.stringify(temp));
33-
}
34-
}
35-
3619
this.queue.push(num);
3720

3821
this.tree.insert(num);
3922
this.total += num;
4023
this.count++;
4124
// debugger;
42-
console.log("addElement", num);
25+
// console.log("addElement", num);
4326
const root = this.tree.getRoot();
4427
if (!root) throw Error("null root");
45-
{
46-
const temp: [number, number][] = [];
47-
for (const node of InOrderIterator(root)) {
48-
temp.push([node.value, MultiRedBlackTree.getCount(node)]);
49-
}
50-
console.log(JSON.stringify(temp));
28+
// {
29+
// console.log(root);
30+
// const temp: [number, number][] = [];
31+
// for (const node of InOrderIterator(root)) {
32+
// temp.push([node.value, this.tree.getCount(node.value)]);
33+
// }
34+
// console.log(JSON.stringify(temp));
35+
// }
36+
if (this.queue.length === this.m + 1) {
37+
const willBeDelete: number = this.queue[0];
38+
this.queue.shift();
39+
// console.log("deleteElement", willBeDelete);
40+
this.total -= willBeDelete;
41+
this.tree.remove(willBeDelete);
42+
const root = this.tree.getRoot();
43+
if (!root) throw Error("null root");
44+
// {
45+
// console.log(root);
46+
// const temp: [number, number][] = [];
47+
// for (const node of InOrderIterator(root)) {
48+
// temp.push([node.value, this.tree.getCount(node.value)]);
49+
// }
50+
// console.log(JSON.stringify(temp));
51+
// }
5152
}
5253
}
5354

@@ -57,29 +58,30 @@ class MKAverage {
5758
let ret = this.total;
5859
const root = this.tree.getRoot();
5960
if (!root) throw Error("null root");
60-
console.log("calculateMKAverage");
61-
{
62-
const temp: [number, number][] = [];
63-
for (const node of InOrderIterator(root)) {
64-
temp.push([node.value, MultiRedBlackTree.getCount(node)]);
65-
}
66-
console.log(JSON.stringify(temp));
67-
}
61+
// console.log("calculateMKAverage");
62+
// {
63+
// console.log(root);
64+
// const temp: [number, number][] = [];
65+
// for (const node of InOrderIterator(root)) {
66+
// temp.push([node.value, this.tree.getCount(node.value)]);
67+
// }
68+
// console.log(JSON.stringify(temp));
69+
// }
6870

6971
// console.log(root)
7072
let k = this.k;
7173

7274
for (const node of InOrderIterator(root)) {
7375
// console.log(node)
74-
const min = Math.min(k, MultiRedBlackTree.getCount(node));
76+
const min = Math.min(k, this.tree.getCount(node.value));
7577
ret -= node.value * min;
7678
k -= min;
7779
if (k === 0) break;
7880
}
7981
k = this.k;
8082
for (const node of reverseInOrderIterator(root)) {
8183
// debugger;
82-
const min = Math.min(k, MultiRedBlackTree.getCount(node));
84+
const min = Math.min(k, this.tree.getCount(node.value));
8385
ret -= node.value * min;
8486
k -= min;
8587
if (k === 0) break;

0 commit comments

Comments
 (0)