-
Notifications
You must be signed in to change notification settings - Fork 13
/
huffman_tree_node.ts
47 lines (41 loc) · 988 Bytes
/
huffman_tree_node.ts
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
import { ICompare } from '@datastructures-js/priority-queue';
interface IHuffmanNode {
value: string | null;
weight: number;
left: IHuffmanNode | null;
right: IHuffmanNode | null;
isLeaf(): boolean;
}
class HuffmanNode implements IHuffmanNode {
public value: string | null;
public weight: number;
public left: HuffmanNode | null;
public right: HuffmanNode | null;
constructor(
weight: number,
value: string | null = null,
left: HuffmanNode | null = null,
right: HuffmanNode | null = null
) {
this.value = value;
this.weight = weight;
this.left = left;
this.right = right;
}
public isLeaf(): boolean {
return this.left !== undefined && this.right !== undefined;
}
}
const compareNode: ICompare<IHuffmanNode> = (
a: IHuffmanNode,
b: IHuffmanNode
) => {
if (a.weight < b.weight) {
return -1;
}
if (a.weight === b.weight) {
return 0;
}
return 1;
};
export { IHuffmanNode, HuffmanNode, compareNode };