/
PriorityQueue.cs
111 lines (87 loc) · 3.1 KB
/
PriorityQueue.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
using System;
using System.Collections.Generic;
using System.Text;
namespace TSP
{
class PriorityQueue
{
private List<Node> heap; //binary heap
public PriorityQueue()
{
this.heap = new List<Node>();
}
public void insert(Node node)
{
heap.Add(node);
bubbleUp(heap.Count - 1);
}
//moving up smaller values towards top of heap
private void bubbleUp(int startIndex)
{
int childIndex = startIndex;
while (childIndex > 0)//bubble up if not top node
{
//compare child to parent and act accordingly
int parentIndex = (childIndex - 1) / 2;
if (heap[childIndex].lowerBound < heap[parentIndex].lowerBound)
{
//swap child and parent while updating index array
Node temp = heap[parentIndex];
heap[parentIndex] = heap[childIndex];
heap[childIndex] = temp;
childIndex = parentIndex;
}
else//if nothing needs updating, break
{
break;
}
}
}
//return element with lowest index
public Node deleteMin()
{
//save top node to be returned after heap stuff if adjusted
Node toReturn = heap[0];
//grab last thing in heap and put it as the first thing
int lastIndex = heap.Count - 1;
heap[0] = heap[lastIndex];
heap.RemoveAt(lastIndex);
lastIndex--;
//adjust heap as neccessary
bubbleDown();
return toReturn;
}
//move first node down as necessary
private void bubbleDown()
{
int parentIndex = 0;
while (true)
{
int childIndex = parentIndex * 2 + 1;//leftChildIndex
if (childIndex > heap.Count - 1) { break; }
//if right child is less than left child, use right child. Otherwise, just use left child.
int rightChildIndex = childIndex + 1;
if (rightChildIndex < heap.Count && heap[rightChildIndex].lowerBound < heap[childIndex].lowerBound)
{
childIndex = rightChildIndex;
}
//swap parent and child if child is less than parent
if (heap[childIndex].lowerBound < heap[parentIndex].lowerBound)
{
Node temp = heap[parentIndex];
heap[parentIndex] = heap[childIndex];
heap[childIndex] = temp;
parentIndex = childIndex;
}
else//break if nothing changes
{
break;
}
}
}
public int getSize()
{
return heap.Count;
}
}
}