Java Reference
In-Depth Information
33 }
34
35
//
Store the new element in the vacant slot
36 elements.set(index, newElement);
37 }
38
39 /**
40
Gets the minimum element stored in this heap.
41 @return
the minimum element
42 */
43
public
Comparable peek()
44 {
45
return
elements.get(
1
);
46 }
47
48 /**
49
Removes the minimum element from this heap.
50 @return
the minimum element
51 */
52
public
Comparable remove()
53 {
54 Comparable minimum = elements.get(
1
);
55
56
//
Remove last element
57
int
lastIndex = elements.size() -
1
;
58 Comparable last =
elements.remove(lastIndex);
59
60
if
(lastIndex >
1
)
61 {
62 elements.set(
1
, last);
63 fixHeap();
64 }
65
66
return
minimum;
67 }
68
69 /**
70
Turns the tree back into a heap, provided only the root
71
node violates the heap condition.
72 */
746
747