-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.dart
104 lines (85 loc) · 1.89 KB
/
heap.dart
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
import 'dart:math';
void main() {
BinMaxHeap bmh = new BinMaxHeap();
bmh.insert(1);
bmh.insert(2);
bmh.insert(3);
bmh.insert(4);
bmh.insert(5);
bmh.insert(6);
print(bmh.extract());
print(bmh.extract());
print(bmh.extract());
print(bmh.extract());
print(bmh.extract());
print(bmh.extract());
bmh.insert(1);
bmh.insert(2);
bmh.insert(3);
bmh.insert(4);
bmh.insert(5);
bmh.insert(6);
}
class BinMaxHeap {
final List<int> elems = new List();
void insert(int e) {
elems.add(e);
_siftUp(elems.length - 1);
print(elems);
}
int extract() {
int val = elems[0];
int lastElem = elems.removeLast();
if (elems.length > 0) {
elems[0] = lastElem;
_siftDown(0);
print(elems);
}
return val;
}
void _siftDown(int nodeIdx) {
if (_lChildIdx(nodeIdx) >= elems.length) {
return;
}
int maxChildIdx = _maxChildIdx(nodeIdx);
if (elems[maxChildIdx] > elems[nodeIdx]) {
_swap(nodeIdx, maxChildIdx);
_siftDown(maxChildIdx);
}
}
void _siftUp(int nodeIdx) {
if (nodeIdx == 0) {
return;
}
int parentIdx = _parentIdx(nodeIdx);
int node = elems[nodeIdx];
int parent = elems[parentIdx];
if (parent < node) {
_swap(nodeIdx, parentIdx);
_siftUp(parentIdx);
}
}
int _parentIdx(int idx) => ((idx - 1) / 2).floor();
int _lChildIdx(int idx) => idx * 2 + 1;
int _rChildIdx(int idx) => idx * 2 + 2;
void _swap(int aIdx, int bIdx) {
int aVal = elems[aIdx];
int bVal = elems[bIdx];
elems[aIdx] = bVal;
elems[bIdx] = aVal;
}
int _maxChildIdx(int idx) {
if (_rChildIdx(idx) >= elems.length) {
return _lChildIdx(idx);
} else {
if (elems[_lChildIdx(idx)] > elems[_rChildIdx(idx)]) {
return _lChildIdx(idx);
} else {
return _rChildIdx(idx);
}
}
}
}
class HeapType {
Function comparator;
}