-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVectorHeap2.java
More file actions
198 lines (179 loc) · 3.7 KB
/
VectorHeap2.java
File metadata and controls
198 lines (179 loc) · 3.7 KB
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
import java.util.Vector;
// TODO: Auto-generated Javadoc
/**
* The Class VectorHeap2.
*
* @param <E> the element type
*/
public class VectorHeap2<E extends Comparable<E>> implements PriorityQueue<E>{
/** The data. */
protected Vector<E> data; // the data, kept in heap order
/**
* Instantiates a new vector heap2.
*/
public VectorHeap2()
// post: constructs a new priority queue
{
data = new Vector<E>();
}
/**
* Instantiates a new vector heap2.
*
* @param v the v
*/
public VectorHeap2(Vector<E> v)
// post: constructs a new priority queue from an unordered vector
{
int i;
data = new Vector<E>(v.size()); // we know ultimate size
for (i = 0; i < v.size(); i++)
{ // add elements to heap
add(v.get(i));
}
}
/**
* Parent.
*
* @param i the i
* @return the int
*/
protected static int parent(int i)
// pre: 0 <= i < size
// post: returns parent of node at location i
{
return (i-1)/2;
}
/**
* Left.
*
* @param i the i
* @return the int
*/
protected static int left(int i)
// pre: 0 <= i < size
// post: returns index of left child of node at location i
{
return 2*i+1;
}
/**
* Right.
*
* @param i the i
* @return the int
*/
protected static int right(int i)
// pre: 0 <= i < size
// post: returns index of right child of node at location i
{
return 2*(i+1);
}
/**
* Percolate up.
*
* @param leaf the leaf
*/
protected void percolateUp(int leaf)
// pre: 0 <= leaf < size
// post: moves node at index leaf up to appropriate position
{
int parent = parent(leaf);
E value = data.get(leaf);
while (leaf > 0 && (value.compareTo(data.get(parent)) < 0))
{
data.set(leaf,data.get(parent));
leaf = parent;
parent = parent(leaf);
}
data.set(leaf,value);
}
/**
* Push down root.
*
* @param root the root
*/
protected void pushDownRoot(int root)
// pre: 0 <= root < size
// post: moves node at index root down
// to appropriate position in subtree
{
int heapSize = data.size();
E value = data.get(root);
while (root < heapSize)
{
int childpos = left(root);
if (childpos < heapSize)
{
if ((right(root) < heapSize) && ((data.get(childpos+1)).compareTo (data.get(childpos)) < 0))
{
childpos++;
}
// Assert: childpos indexes smaller of two children
if ((data.get(childpos)).compareTo (value) < 0)
{
data.set(root,data.get(childpos));
root = childpos; // keep moving down
} else { // found right location
data.set(root,value);
return;
}
} else { // at a leaf! insert and halt
data.set(root,value);
return;
}
}
}
/* (non-Javadoc)
* @see PriorityQueue#add(java.lang.Comparable)
*/
public void add(E value)
// pre: value is non-null comparable
// post: value is added to priority queue
{
data.add(value);
percolateUp(data.size()-1);
}
/* (non-Javadoc)
* @see PriorityQueue#remove()
*/
@Override
public E remove() {
//pre: !isEmpty()
//post: removes and returns the minimum value in priority queue
E minVal = getFirst();
data.set(0,data.get(data.size()-1));
data.setSize(data.size()-1);
if(data.size()>1){pushDownRoot(0);};
return minVal;
}
/* (non-Javadoc)
* @see PriorityQueue#isEmpty()
*/
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see PriorityQueue#size()
*/
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
/* (non-Javadoc)
* @see PriorityQueue#clear()
*/
@Override
public void clear() {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see PriorityQueue#getFirst()
*/
@Override
public E getFirst() {
// TODO Auto-generated method stub
return data.get(0);
}
}