-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathHeap.cs
211 lines (172 loc) · 5.34 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Special thanks to my brother Denis.
// My brother's github: https://github.com/dbrizov
namespace System.Collections.Generic
{
/// <summary>
/// A binary max heap by default (array implementation). Note that it's NOT stable.
/// </summary>
/// <typeparam name="T"></typeparam>
public class Heap<T>
{
private const int INITIAL_CAPACITY = 4;
private T[] m_array;
private int m_lastItemIndex;
private IComparer<T> m_comparer;
public Heap()
: this(INITIAL_CAPACITY, Comparer<T>.Default)
{
}
public Heap(int capacity)
: this(capacity, Comparer<T>.Default)
{
}
public Heap(Comparison<T> comparison)
: this(Comparer<T>.Create(comparison))
{
}
public Heap(IComparer<T> comparer)
: this(INITIAL_CAPACITY, comparer)
{
}
public Heap(int capacity, Comparison<T> comparison)
: this(capacity, Comparer<T>.Create(comparison))
{
}
public Heap(int capacity, IComparer<T> comparer)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity), "Non-negative number required.");
}
if (comparer == null)
{
throw new ArgumentNullException(nameof(comparer));
}
m_array = new T[capacity];
m_lastItemIndex = -1;
m_comparer = comparer;
}
public Heap(IEnumerable<T> collection)
: this(collection, Comparer<T>.Default)
{
}
public Heap(IEnumerable<T> collection, Comparison<T> comparison)
: this(collection, Comparer<T>.Create(comparison))
{
}
public Heap(IEnumerable<T> collection, IComparer<T> comparer)
: this(comparer)
{
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
foreach (var item in collection)
{
Add(item);
}
}
public int Count
{
get => (m_lastItemIndex + 1);
}
public bool IsEmpty
{
get => (Count == 0);
}
public void Add(T item)
{
if (m_lastItemIndex == m_array.Length - 1)
{
Resize();
}
m_lastItemIndex++;
m_array[m_lastItemIndex] = item;
HeapifyUp(m_lastItemIndex);
}
public T Remove()
{
if (m_lastItemIndex == -1)
{
throw new InvalidOperationException("The heap is empty.");
}
T removedItem = m_array[0];
m_array[0] = m_array[m_lastItemIndex];
m_lastItemIndex--;
HeapifyDown(0);
return removedItem;
}
public T Peek()
{
if (m_lastItemIndex == -1)
{
throw new InvalidOperationException("The heap is empty.");
}
return m_array[0];
}
public void Clear()
{
m_lastItemIndex = -1;
}
/// <summary>
/// Heapify up (max by default).
/// </summary>
/// <param name="index">The index of the (sub)root to start from.</param>
private void HeapifyUp(int index)
{
if (index == 0)
{
return;
}
int childIndex = index;
int parentIndex = (index - 1) / 2;
if (m_comparer.Compare(m_array[childIndex], m_array[parentIndex]) > 0)
{
// Swap the parent and the child.
Swap(ref m_array[childIndex], ref m_array[parentIndex]);
HeapifyUp(parentIndex);
}
}
/// <summary>
/// Heapify down (max by default).
/// </summary>
/// <param name="index">The index of the (sub)root to start from.</param>
private void HeapifyDown(int index)
{
int leftChildIndex = index * 2 + 1;
int rightChildIndex = index * 2 + 2;
int greaterItemIndex = index;
if (leftChildIndex <= m_lastItemIndex &&
m_comparer.Compare(m_array[leftChildIndex], m_array[greaterItemIndex]) > 0)
{
greaterItemIndex = leftChildIndex;
}
if (rightChildIndex <= m_lastItemIndex &&
m_comparer.Compare(m_array[rightChildIndex], m_array[greaterItemIndex]) > 0)
{
greaterItemIndex = rightChildIndex;
}
if (greaterItemIndex != index)
{
// Swap the parent with the largest of the child items.
Swap(ref m_array[index], ref m_array[greaterItemIndex]);
HeapifyDown(greaterItemIndex);
}
}
private void Resize()
{
T[] newArray = new T[m_array.Length * 2];
for (int i = 0; i < m_array.Length; i++)
{
newArray[i] = m_array[i];
}
m_array = newArray;
}
private void Swap(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}
}
}