-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVectorHeap.java
More file actions
78 lines (71 loc) · 1.35 KB
/
VectorHeap.java
File metadata and controls
78 lines (71 loc) · 1.35 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
import java.util.PriorityQueue;
import java.util.Vector;
// TODO: Auto-generated Javadoc
/**
* The Class VectorHeap.
*
* @param <E> the element type
*/
public class VectorHeap < E extends Comparable<E>> extends PriorityQueue<E>{
/** The data. */
protected Vector<E> data; // the data, kept in heap order
/**
* Instantiates a new vector heap.
*/
public VectorHeap()
// post: constructs a new priority queue
{
data = new Vector<E>();
}
/**
* Instantiates a new vector heap.
*
* @param v the v
*/
public VectorHeap(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);
}
}