-
Notifications
You must be signed in to change notification settings - Fork 2
/
heap.ts
171 lines (139 loc) · 3.83 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
MAX HEAP DATA STRUCTURE for sorting in O(nlgn)
The (binary) heap data structure is an array object that we can view as a
nearly complete binary tree. Each
node of the tree corresponds to an element of the array.
The tree is completely filled on all levels except possibly the lowest, which is filled from the
left up to a point. An array A that represents a heap is an object with two attributes: A:length,
which (as usual) gives the number of elements in the array, and
A:heap-size, which represents how many elements in the heap are stored within
array A. That is, although A[1.... A.heapSize] may contain numbers, only the elements
in A[1.... A.heapSize], where 0 <= A.heap-size <= A.length, are valid elements of the heap.
The root of the tree is A[1], and given the index i of a node, we
can easily compute the indices of its parent, left child, and right child.
example : https://www.hackerearth.com/practice/data-structures/trees/heapspriority-queues/tutorial/
*/
export default class Heap<T>{
private heap: T[];
private heapSize: number;
constructor(){
this.heap=[];
this.heapSize=0;
}
// getters
protected getValue(index: number) {
return this.heap[index - 1];
}
protected getHeight() {
return this.heapSize;
}
// setters
protected setValue(index: number, value: T) {
this.heap[index - 1] = value;
}
protected increaseHeight() {
this.heapSize = this.heapSize + 1;
}
protected decreaseHeight() {
this.heapSize = this.heapSize - 1;
}
protected heapifyRoot() {
this.replace(1, this.getHeight());
this.decreaseHeight();
this.heapify(1);
}
/**
*returns parent index
*
* @param {number} index
* @returns
* @memberof Heap
*/
protected parent(index: number) {
return Math.floor(index / 2)
}
/**
*returns left child
*
* @param {number} index
* @returns
* @memberof Heap
*/
private left(index: number) {
return 2 * index;
}
/**
*returns right child
*
* @param {number} index
* @returns
* @memberof Heap
*/
private right(index: number) {
return (2 * index) + 1;
}
/**
*replaces values in specified indexes
*
* @private
* @param {*} index1
* @param {*} index2
* @memberof Heap
*/
protected replace(index1, index2) {
const temp = this.getValue(index1);
this.setValue(index1, this.getValue(index2));
this.setValue(index2, temp);
}
/**
*makes necessary correction to turn the tree into max heap
*
* @private
* @param {number} index
* @memberof Heap
*/
private heapify(index: number) {
let l = this.left(index);
let r = this.right(index);
let largest;
if (!(l > this.heapSize) && this.getValue(l) > this.getValue(index)) {
largest = l;
} else {
largest = index;
}
if (!(r > this.heapSize) && this.getValue(r) > this.getValue(largest)) {
largest = r;
}
if (largest !== index) {
this.replace(largest, index)
this.heapify(largest);
}
}
/**
*creates heap
*
* @private
* @memberof Heap
*/
private build_heap() {
for (let index = Math.floor(this.heapSize / 2); index > 0; index--) {
this.heapify(index);
}
}
/**
*sorts the given array
*
* @param {T[]} arr
* @returns
* @memberof Heap
*/
public sort(arr: T[]) {
this.heap = arr;
this.heapSize = arr.length;
this.build_heap();
for (let index = this.heapSize; index > 1; index--) {
this.heapifyRoot();
}
return this.heap;
}
}