-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinHeap.java
119 lines (110 loc) · 2.7 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
package madzip;
import java.util.NoSuchElementException;
/**
* The MinHeap class is a generic implementation of a min-heap data structure.
*
* @param <T> The type of the elements in the heap.
* @author Salman Khattak
* @version 12/6/2022
*/
public class MinHeap<T extends Comparable<T>> {
private int size;
public final T[] items;
/**
* Creates a new MinHeap object.
*
* @param capacity the capacity of the heap.
*/
public MinHeap(int capacity) {
items = (T[]) new Comparable[capacity];
}
/**
* Returns the size of the heap.
*
* @return the size of the heap.
*/
public int size() {
return size;
}
/**
* Returns the capacity of the heap.
*
* @return the capacity of the heap.
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Returns the capacity of the heap.
*
* @param element the index of the element.
*/
public void add(T element) {
if (size == items.length) {
throw new IllegalStateException("Heap is full");
}
items[size] = element;
heapifyUp(size++);
}
/**
* Returns the capacity of the heap.
*
* @return the capacity of the heap.
*/
public T remove() {
if (isEmpty()) {
throw new NoSuchElementException("Heap is empty");
}
T smallest = items[0];
items[0] = items[--size];
heapifyDown(0);
return smallest;
}
/**
* Heapifies up the element at the given index.
*
* @param index the index of the element to heapify up.
*/
private void heapifyUp(int index) {
int parentIndex;
while (index > 0 && (parentIndex = (index - 1) / 2) >= 0) {
if (items[index].compareTo(items[parentIndex]) < 0) {
swap(index, parentIndex);
} else {
break;
}
index = parentIndex;
}
}
/**
* Heapifies down the element at the given index.
*
* @param index the index of the element to heapify down.
*/
private void heapifyDown(int index) {
int smallerChildIndex;
while ((smallerChildIndex = 2 * index + 1) < size) {
int rightChildIndex = smallerChildIndex + 1;
if (rightChildIndex < size && items[rightChildIndex].compareTo(items[smallerChildIndex]) < 0) {
smallerChildIndex = rightChildIndex;
}
if (items[index].compareTo(items[smallerChildIndex]) > 0) {
swap(index, smallerChildIndex);
} else {
break;
}
index = smallerChildIndex;
}
}
/**
* Swaps the elements at the given indices.
*
* @param index1 the index of the first element.
* @param index2 the index of the second element.
*/
private void swap(int index1, int index2) {
T temp = items[index1];
items[index1] = items[index2];
items[index2] = temp;
}
}