-
Notifications
You must be signed in to change notification settings - Fork 1
/
Heap.ts
107 lines (90 loc) · 2.85 KB
/
Heap.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Fork from https://github.com/yangshun/lago
// https://github.com/yangshun/lago/blob/master/src/data-structures/Heap.ts
export default class Heap {
private heap: number[] = [];
static readonly HeapType = { Min: 'Min', Max: 'Max' } as const;
private compare: (value1: number, value2: number) => boolean
constructor (type: typeof Heap.HeapType.Min | typeof Heap.HeapType.Max) {
this.compare =
type === Heap.HeapType.Min
? (value1: number, value2: number) => value1 < value2
: (value1: number, value2: number) => value1 > value2
}
get size (): number {
return this.heap.length
}
isEmpty (): boolean {
return this.size === 0
}
private getRightChildIndex (index: number): number {
return 2 * index + 2
}
private getLeftChildIndex (index: number): number {
return 2 * index + 1
}
private getParentIndex (index: number): number {
return Math.floor((index - 1) / 2)
}
private hasLeftChild (index: number): boolean {
return this.getLeftChildIndex(index) < this.heap.length
}
private hasRightChild (index: number): boolean {
return this.getRightChildIndex(index) < this.heap.length
}
private hasParent (index: number): boolean {
return this.getParentIndex(index) >= 0
}
private swap (index1: number, index2: number): void {
[this.heap[index1], this.heap[index2]] = [
this.heap[index2],
this.heap[index1],
]
}
peek (): number | null {
return this.isEmpty() ? null : this.heap[0]
}
extract (): number | null {
if (this.isEmpty()) return null
this.swap(0, this.size - 1)
const result = this.heap.pop()!
this.heapifyDown()
return result
}
insert (value: number): void {
this.heap.push(value)
this.heapifyUp()
}
private heapifyUp (): void {
let index = this.heap.length - 1
let parentIndex = this.getParentIndex(index)
while (
this.hasParent(index) &&
this.compare(this.heap[index], this.heap[parentIndex])
) {
this.swap(index, parentIndex)
index = parentIndex
parentIndex = this.getParentIndex(index)
}
}
private heapifyDown (): void {
let index = 0
let leftChildIndex = this.getLeftChildIndex(index)
let rightChildIndex = this.getRightChildIndex(index)
while (
(this.hasLeftChild(index) &&
!this.compare(this.heap[index], this.heap[leftChildIndex])) ||
(this.hasRightChild(index) &&
!this.compare(this.heap[index], this.heap[rightChildIndex]))
) {
const comparableIndex =
this.hasRightChild(index) &&
!this.compare(this.heap[leftChildIndex], this.heap[rightChildIndex])
? rightChildIndex
: leftChildIndex
this.swap(index, comparableIndex)
index = comparableIndex
leftChildIndex = this.getLeftChildIndex(index)
rightChildIndex = this.getRightChildIndex(index)
}
}
}