-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMinHeap.cs
143 lines (122 loc) · 3.2 KB
/
MinHeap.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
using System;
using System.Collections;
using System.Collections.Generic;
public class MinHeap<T> : IEnumerable<T> where T : class, IComparable<T>
{
#region Fields
private IComparer<T> Comparer;
private List<T> Items = new List<T>();
#endregion
#region Properties
public int Count
{
get { return Items.Count; }
}
#endregion
#region Constructors
public MinHeap()
: this(Comparer<T>.Default)
{
}
public MinHeap(IComparer<T> comp)
{
Comparer = comp;
}
#endregion
#region Methods
/// <summary>
/// Removes all items from the collection.
/// </summary>
public void Clear()
{
Items.Clear();
}
/// <summary>
/// Sets the capacity to the actual number of elements in the BinaryHeap,
/// if that number is less than a threshold value.
/// </summary>
/// <remarks>
/// The current threshold value is 90% (.NET 3.5), but might change in a future release.
/// </remarks>
public void TrimExcess()
{
Items.TrimExcess();
}
/// <summary>
/// Inserts an item onto the heap.
/// </summary>
/// <param name="newItem">The item to be inserted.</param>
public void Insert(T newItem)
{
int i = Count;
Items.Add(newItem);
while (i > 0 && Comparer.Compare(Items[(i - 1) / 2], newItem) > 0)
{
Items[i] = Items[(i - 1) / 2];
i = (i - 1) / 2;
}
Items[i] = newItem;
}
/// <summary>
/// Return the root item from the collection, without removing it.
/// </summary>
/// <returns>Returns the item at the root of the heap.</returns>
public T Peek()
{
if (Items.Count == 0)
{
throw new InvalidOperationException("The heap is empty.");
}
return Items[0];
}
/// <summary>
/// Removes and returns the root item from the collection.
/// </summary>
/// <returns>Returns the item at the root of the heap.</returns>
public T RemoveRoot()
{
if (Items.Count == 0)
{
throw new InvalidOperationException("The heap is empty.");
}
// Get the first item
T rslt = Items[0];
// Get the last item and bubble it down.
T tmp = Items[Items.Count - 1];
Items.RemoveAt(Items.Count - 1);
if (Items.Count > 0)
{
int i = 0;
while (i < Items.Count / 2)
{
int j = (2 * i) + 1;
if ((j < Items.Count - 1) && (Comparer.Compare(Items[j], Items[j + 1]) > 0))
{
++j;
}
if (Comparer.Compare(Items[j], tmp) >= 0)
{
break;
}
Items[i] = Items[j];
i = j;
}
Items[i] = tmp;
}
return rslt;
}
#endregion
#region IEnumerable implementation
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
foreach (var i in Items)
{
yield return i;
}
}
public IEnumerator GetEnumerator()
{
return Items.GetEnumerator();
}
#endregion
}