Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathGenericHeap.java
More file actions
Latest commit
149 lines (135 loc) · 3.89 KB
/
Copy pathGenericHeap.java
File metadata and controls
149 lines (135 loc) · 3.89 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
packagecom.thealgorithms.datastructures.heaps;
importjava.util.ArrayList;
importjava.util.HashMap;
/**
* A generic implementation of a max heap data structure.
*
* @param <T> the type of elements in this heap, must extend Comparable.
*/
publicclassGenericHeap<TextendsComparable<T>> {
privatefinalArrayList<T> data = newArrayList<>();
privatefinalHashMap<T, Integer> map = newHashMap<>();
/**
* Adds an item to the heap, maintaining the heap property.
*
* @param item the item to be added
*/
publicvoidadd(Titem) {
if (item == null) {
thrownewIllegalArgumentException("Cannot insert null into the heap.");
}
this.data.add(item);
map.put(item, this.data.size() - 1);
upHeapify(this.data.size() - 1);
}
/**
* Restores the heap property by moving the item at the given index upwards.
*
* @param ci the index of the current item
*/
privatevoidupHeapify(intci) {
intpi = (ci - 1) / 2;
if (ci > 0 && isLarger(this.data.get(ci), this.data.get(pi)) > 0) {
swap(pi, ci);
upHeapify(pi);
}
}
/**
* Returns the number of elements in the heap.
*
* @return the size of the heap
*/
publicintsize() {
returnthis.data.size();
}
/**
* Checks if the heap is empty.
*
* @return true if the heap is empty, false otherwise
*/
publicbooleanisEmpty() {
returnthis.size() == 0;
}
/**
* Removes and returns the maximum item from the heap.
*
* @return the maximum item
*/
publicTremove() {
if (isEmpty()) {
thrownewIllegalStateException("Heap is empty");
}
this.swap(0, this.size() - 1);
Trv = this.data.remove(this.size() - 1);
map.remove(rv);
downHeapify(0);
returnrv;
}
/**
* Restores the heap property by moving the item at the given index downwards.
*
* @param pi the index of the current item
*/
privatevoiddownHeapify(intpi) {
intlci = 2 * pi + 1;
intrci = 2 * pi + 2;
intmini = pi;
if (lci < this.size() && isLarger(this.data.get(lci), this.data.get(mini)) > 0) {
mini = lci;
}
if (rci < this.size() && isLarger(this.data.get(rci), this.data.get(mini)) > 0) {
mini = rci;
}
if (mini != pi) {
this.swap(pi, mini);
downHeapify(mini);
}
}
/**
* Retrieves the maximum item from the heap without removing it.
*
* @return the maximum item
*/
publicTget() {
if (isEmpty()) {
thrownewIllegalStateException("Heap is empty");
}
returnthis.data.getFirst();
}
/**
* Compares two items to determine their order.
*
* @param t the first item
* @param o the second item
* @return a positive integer if t is greater than o, negative if t is less, and zero if they are equal
*/
privateintisLarger(Tt, To) {
returnt.compareTo(o);
}
/**
* Swaps two items in the heap and updates their indices in the map.
*
* @param i index of the first item
* @param j index of the second item
*/
privatevoidswap(inti, intj) {
Tith = this.data.get(i);
Tjth = this.data.get(j);
this.data.set(i, jth);
this.data.set(j, ith);
map.put(ith, j);
map.put(jth, i);
}
/**
* Updates the priority of the specified item by restoring the heap property.
*
* @param item the item whose priority is to be updated
*/
publicvoidupdatePriority(Titem) {
if (!map.containsKey(item)) {
thrownewIllegalArgumentException("Item not found in the heap");
}
intindex = map.get(item);
upHeapify(index);
}
}