| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Text; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | |
|
| | 7 | | namespace HeapLibrary |
| | 8 | | { |
| | 9 | | class HeapItem<T> |
| | 10 | | { |
| | 11 | |
|
| | 12 | | public int cost; |
| | 13 | | public T obj; |
| | 14 | |
|
| | 15 | | public HeapItem(int cost, T o) |
| | 16 | | { |
| | 17 | | this.cost = cost; |
| | 18 | | this.obj = o; |
| | 19 | | } |
| | 20 | |
|
| | 21 | | } |
| | 22 | |
|
| | 23 | | public class Heap<T> |
| | 24 | | { |
| | 25 | |
|
| 1 | 26 | | List<HeapItem<T>> heap = new List<HeapItem<T>>(); |
| | 27 | |
|
| 1 | 28 | | Dictionary<T, HeapItem<T>> items = new Dictionary<T, HeapItem<T>>(); |
| | 29 | |
|
| | 30 | | public bool Contains(T o) |
| 2 | 31 | | { |
| 2 | 32 | | return items.ContainsKey(o); |
| 2 | 33 | | } |
| | 34 | |
|
| | 35 | | public void Add(int cost, T o) |
| 2 | 36 | | { |
| 2 | 37 | | if (Contains(o)) |
| 0 | 38 | | { |
| 0 | 39 | | ChangeCost(cost, o); |
| 0 | 40 | | return; |
| | 41 | | } |
| | 42 | |
|
| 2 | 43 | | HeapItem<T> item = new HeapItem<T>(cost, o); |
| | 44 | |
|
| 2 | 45 | | heap.Add(item); |
| 2 | 46 | | items.Add(o, item); |
| | 47 | |
|
| 2 | 48 | | MoveUp(heap.Count() - 1); |
| 2 | 49 | | } |
| | 50 | |
|
| | 51 | | public void ChangeCost(int cost, T o) |
| 0 | 52 | | { |
| 0 | 53 | | for (int i = 0; i < heap.Count(); ++i) |
| 0 | 54 | | { |
| 0 | 55 | | HeapItem<T> item = heap[i]; |
| 0 | 56 | | if (ReferenceEquals(item.obj, o)) |
| 0 | 57 | | { |
| 0 | 58 | | if (item.cost == cost) return; |
| 0 | 59 | | if (item.cost > cost) |
| 0 | 60 | | { |
| 0 | 61 | | item.cost = cost; |
| 0 | 62 | | MoveDown(i); |
| 0 | 63 | | } |
| | 64 | | else |
| 0 | 65 | | { |
| 0 | 66 | | item.cost = cost; |
| 0 | 67 | | MoveUp(i); |
| 0 | 68 | | } |
| 0 | 69 | | return; |
| | 70 | | } |
| 0 | 71 | | } |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | public T Top() |
| 1 | 75 | | { |
| 1 | 76 | | if (heap.Count() == 0) return default(T); |
| 1 | 77 | | return heap[0].obj; |
| 1 | 78 | | } |
| | 79 | |
|
| | 80 | | public T Pop() |
| 1 | 81 | | { |
| 1 | 82 | | if (heap.Count() == 0) return default(T); |
| | 83 | |
|
| 1 | 84 | | T first = Top(); |
| 1 | 85 | | items.Remove(first); |
| | 86 | |
|
| 1 | 87 | | if (heap.Count() == 1) |
| 0 | 88 | | { |
| 0 | 89 | | heap.RemoveAt(0); |
| 0 | 90 | | return first; |
| | 91 | | } |
| | 92 | |
|
| 1 | 93 | | heap[0] = heap[heap.Count() - 1]; |
| 1 | 94 | | heap.RemoveAt(heap.Count() - 1); |
| | 95 | |
|
| 1 | 96 | | MoveDown(0); |
| | 97 | |
|
| 1 | 98 | | return first; |
| 1 | 99 | | } |
| | 100 | |
|
| | 101 | | private void MoveUp(int index) |
| 2 | 102 | | { |
| 3 | 103 | | if (index == 0) return; |
| 1 | 104 | | HeapItem<T> i1 = heap[index]; |
| 1 | 105 | | HeapItem<T> i2 = heap[index / 2]; |
| 1 | 106 | | if (i1.cost < i2.cost) |
| 0 | 107 | | { |
| 0 | 108 | | heap[index / 2] = i1; |
| 0 | 109 | | heap[index] = i2; |
| 0 | 110 | | MoveUp(index / 2); |
| 0 | 111 | | } |
| 2 | 112 | | } |
| | 113 | |
|
| | 114 | | private void MoveDown(int index) |
| 1 | 115 | | { |
| 1 | 116 | | if (index >= heap.Count()) return; |
| 1 | 117 | | HeapItem<T> i1 = heap[index]; |
| | 118 | |
|
| 1 | 119 | | HeapItem<T> i2 = (index * 2 < heap.Count() ? heap[index * 2] : null); |
| 1 | 120 | | HeapItem<T> i3 = (index * 2 + 1 < heap.Count() ? heap[index * 2 + 1] : null); |
| | 121 | |
|
| 1 | 122 | | if (i2 == null) |
| 0 | 123 | | { |
| 0 | 124 | | if (i3 == null) |
| 0 | 125 | | { |
| 0 | 126 | | return; |
| | 127 | | } |
| 0 | 128 | | if (i1.cost > i3.cost) |
| 0 | 129 | | { |
| 0 | 130 | | heap[index] = i2; |
| 0 | 131 | | heap[index * 2 + 1] = i1; |
| 0 | 132 | | MoveDown(index * 2 + 1); |
| 0 | 133 | | } |
| 0 | 134 | | return; |
| | 135 | | } |
| | 136 | |
|
| 1 | 137 | | if (i3 == null) |
| 1 | 138 | | { |
| 1 | 139 | | if (i1.cost > i2.cost) |
| 0 | 140 | | { |
| 0 | 141 | | heap[index] = i2; |
| 0 | 142 | | heap[index * 2] = i3; |
| 0 | 143 | | MoveDown(index * 2); |
| 0 | 144 | | } |
| 1 | 145 | | return; |
| | 146 | | } |
| | 147 | |
|
| 0 | 148 | | if (i1.cost <= i2.cost && i1.cost <= i2.cost) |
| 0 | 149 | | { |
| 0 | 150 | | return; |
| | 151 | | } |
| | 152 | |
|
| 0 | 153 | | if (i2.cost > i3.cost) |
| 0 | 154 | | { |
| 0 | 155 | | heap[index] = i3; |
| 0 | 156 | | heap[index * 2 + 1] = i1; |
| 0 | 157 | | MoveDown(index * 2 + 1); |
| 0 | 158 | | } |
| | 159 | | else |
| 0 | 160 | | { |
| 0 | 161 | | heap[index] = i2; |
| 0 | 162 | | heap[index * 2 + 1] = i1; |
| 0 | 163 | | MoveDown(index * 2); |
| 0 | 164 | | } |
| | 165 | |
|
| 1 | 166 | | } |
| | 167 | |
|
| | 168 | | } |
| | 169 | |
|
| | 170 | | } |