-
Notifications
You must be signed in to change notification settings - Fork 8
/
Heap.cs
88 lines (71 loc) · 2.07 KB
/
Heap.cs
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
using System;
class Heap<T> where T : struct, IComparable<T> {
T[] heap;
int count;
public int Count => count;
public Heap() {
heap = Array.Empty<T>();
count = 0;
}
public void Clear() {
Array.Clear(heap, 0, count);
count = 0;
}
public void Put(T item) {
if (count == heap.Length)
Array.Resize(ref heap, heap.Length * 2 + 1);
heap[count++] = item;
UpHeap(count - 1);
}
public void PutAll(T[] items) {
if (count + items.Length >= heap.Length)
Array.Resize(ref heap, Math.Max(heap.Length * 2, count + items.Length));
for (int i = 0; i < items.Length; i++)
heap[count++] = items[i];
MakeHeap();
}
public T Take() {
if (count == 0)
throw new InvalidOperationException("Cannot take from empty heap!");
T item = heap[0];
heap[0] = heap[--count];
DownHeap(0);
return item;
}
void Swap(int a, int b) {
T tmp = heap[a];
heap[a] = heap[b];
heap[b] = tmp;
}
void UpHeap(int index) {
while (index > 0) {
int parent = (index - 1) / 2;
if (heap[index].CompareTo(heap[parent]) >= 0)
break;
Swap(index, parent);
index = parent;
}
}
void DownHeap(int index) {
while (index < count) {
int left = 2 * index + 1,
right = 2 * index + 2,
target = index;
if (left < count && heap[left].CompareTo(heap[target]) < 0)
target = left;
if (right < count && heap[right].CompareTo(heap[target]) < 0)
target = right;
if (target == index)
break;
Swap(index, target);
index = target;
}
}
void MakeHeap() {
for (int index = count - 1; index >= 0; index--) {
int parent = (index - 1) / 2;
if (heap[index].CompareTo(heap[parent]) < 0)
Swap(index, parent);
}
}
}