-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathheap.ts
109 lines (94 loc) · 2.82 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
108
109
/*
* root : i = 0
* parent(i): (i-1)/2
* left(i): 2*i + 1
* right(i): 2*i + 2
* 2^n , n 为level, 2^n为最小size
*
* */
export class Heap {
items: number[]
constructor(items: number[] = []) {
this.items = items
}
build(): Heap {
for (let i = Math.floor(this.items.length / 2); i >= 0; i--) {
this.heapifyDown(i)
}
return this
}
getParentIndex(childIndex: number): number {
return Math.floor((childIndex - 1) / 2);
}
hasParent(childIndex: number): boolean {
return this.getParentIndex(childIndex) >= 0
}
getLeftChild(parentIndex: number): number {
return parentIndex * 2 + 1
}
getRightChild(parentIndex: number): number {
return parentIndex * 2 + 2
}
swap(indexA: number, indexB: number) {
if (indexA < this.items.length && indexB < this.items.length) {
let tmp = this.items[indexA]
this.items[indexA] = this.items[indexB]
this.items[indexB] = tmp
}
}
pairIsNotCorrectOrder(firstItem: number, secondItem: number): boolean {
throw new Error("not implements")
}
heapifyUp(childIndex: number) {
let parentIndex = this.getParentIndex(childIndex)
if (parentIndex >= 0 && this.pairIsNotCorrectOrder(this.items[childIndex], this.items[parentIndex])) {
this.swap(childIndex, parentIndex)
this.heapifyUp(parentIndex)
}
}
heapifyDown(parentIndex: number) {
let lIndex = this.getLeftChild(parentIndex)
let rIndex = this.getRightChild(parentIndex)
let maxIndex = parentIndex
if (lIndex < this.items.length && this.pairIsNotCorrectOrder(this.items[lIndex], this.items[maxIndex])) {
maxIndex = lIndex
}
if (rIndex < this.items.length && this.pairIsNotCorrectOrder(this.items[rIndex], this.items[maxIndex])) {
maxIndex = rIndex
}
if (maxIndex !== parentIndex) {
this.swap(maxIndex, parentIndex)
return this.heapifyDown(maxIndex)
}
}
add(item: number) {
// insert next space
this.items.push(item)
// heapifyUp
this.heapifyUp(this.items.length - 1)
}
remove() {
// swap last child to root
this.swap(0, this.items.length - 1)
// remove the root
this.items.pop()
// heapifyDown
this.heapifyDown(0)
return this
}
find(item: number): number[] {
const foundIndices = []
for (let i = 0; i < this.items.length; i++) {
if (this.items[i] === item) {
foundIndices.push(i)
}
}
return foundIndices
}
peek() {
if (this.items.length === 0) {
return null;
}
return this.items[0];
}
}