-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinHeap.java
146 lines (129 loc) · 3.83 KB
/
MinHeap.java
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
/**
* *
* * assign1.java – Assignment1
* * @author: Jeremiah Smith, Juyong Kim
* * @student Number: c3238179 c3244203
* * @version: 016/10/2018
* * Description: Saves values from the DijkstraAlgorithm in a min heap
* */
public class MinHeap
{
//variables
private int capacity;
private int currentSize;
private Node[] node;
private int [] indexes; //used to decrease the duration
private String optimisationCriteria;
//constructor
public MinHeap(int capacity, String optimisationCriteria)
{
this.capacity = capacity;
this.optimisationCriteria = optimisationCriteria;
node = new Node[capacity + 1];
indexes = new int[capacity];
currentSize = 0;
}
//inserts nodes/stations into the minHeap
public void insert(Node x)
{
int idx = currentSize;
node[idx] = x;
indexes[x.getVertex().getID()] = idx;
bubbleUp(idx);
currentSize++;
}
//pushes value up to where it belongs
public void bubbleUp(int pos)
{
int parentIdx = pos/2;
int currentIdx = pos;
while (currentIdx > 0 && currentIdx < node.length && node[parentIdx].compareTo(node[currentIdx]) > 0)//node[parentIdx].getComparator() > node[currentIdx].getComparator())
{
Node currentNode = node[currentIdx];
Node parentNode = node[parentIdx];
//swap the positions
indexes[currentNode.getVertex().getID()] = parentIdx;
indexes[parentNode.getVertex().getID()] = currentIdx;
swap(currentIdx,parentIdx);
currentIdx = parentIdx;
parentIdx = parentIdx/2;
}
}
//basically bubble down
public void sinkDown(int k)
{
int smallest = k;
int leftChildIdx = 2 * k;
int rightChildIdx = 2 * k+1;
if (leftChildIdx > 0 && leftChildIdx < node.length && node[smallest].compareTo(node[leftChildIdx]) > 0)
{
smallest = leftChildIdx;
}
if (rightChildIdx > 0 && rightChildIdx < node.length && node[smallest].compareTo(node[rightChildIdx]) > 0)
{
smallest = rightChildIdx;
}
if (smallest != k)
{
Node smallestNode = node[smallest];
Node kNode = node[k];
//swap the positions
indexes[smallestNode.getVertex().getID()] = k;
indexes[kNode.getVertex().getID()] = smallest;
swap(k, smallest);
sinkDown(smallest);
}
}
//function used to compare two nodes values for minheap manipulation
public boolean compareTo(Node parent, Node current)
{
if (current.getComparator() < parent.getComparator())
return true;
else if (current.getComparator() == parent.getComparator())
if (current.getChanges() < parent.getChanges())
return true;
return false;
}
//returns the smallest Node
public Node extractMin()
{
Node min = node[0];
Node lastNode = node[currentSize-1];
// update the indexes[] and move the last node to the top
indexes[lastNode.getVertex().getID()] = 0;
node[0] = lastNode;
node[currentSize] = new Node();//null;
sinkDown(0);
currentSize--;
return min;
}
//swaps 2 positions
public void swap(int a, int b)
{
Node temp = node[a];
node[a] = node[b];
node[b] = temp;
}
//checks if empty through the size
public boolean isEmpty()
{
return currentSize == 0;
}
//getters
public int heapSize()
{
return currentSize;
}
public int getIndex(int index)
{
return indexes[index];
}
public Node getNode(int index)
{
return node[index];
}
public int size()
{
return currentSize;
}
}