-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.ts
82 lines (73 loc) · 2.12 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
class Heap {
heap: Array<number>
constructor(arr: Array<number>) {
this.heap = this.buildHeap(arr)
}
private swap(arr: Array<number>, i: number, j: number) {
let temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
private buildHeap(arr: Array<number>) {
// builds a heap from an array of elements
for(let i = Math.floor((arr.length-1)/2); i >= 0; i--) {
this.heapify(arr, i)
}
return arr
}
insert(element: number) {
// insert an element into the heap
this.heap.push(element)
this.heap = this.buildHeap(this.heap)
}
delete(index: number) {
// delete an element from the heap
this.heap.splice(index, 1)
this.heap = this.buildHeap(this.heap)
}
private heapify(arr: Array<number>, index: number) {
// used when an inserted element unbalances the heap and it loses the heap properties
// bottom-up approach
let largest = index
let left = this.getLeft(index)
let right = this.getRight(index)
if ((left <= arr.length) && (arr[left] > arr[index])){
largest = left
}
if ((right <= arr.length) && (arr[right] > arr[largest])){
largest = right
}
if (largest != index){
this.swap(arr, index, largest)
this.heapify(arr, largest)
}
}
getMax() {
// returns the max element in the heap
return this.heap[0]
}
pop() {
// returns the max element in the heap and deletes it from the heap
let max = this.heap[0]
this.delete(0)
this.heap = this.buildHeap(this.heap)
return max
}
private getRight(index: number) {
return 2 * index + 2
}
private getLeft(index: number) {
return 2 * index + 1
}
// HEAP SORT
heapSort() {
// sorts the heap in ascending order
let tmp = [...this.heap]
let sorted = []
while(this.heap.length > 0) {
sorted.push(this.pop())
}
this.heap = tmp
return sorted.reverse()
}
}