3
3
import java .util .ArrayList ;
4
4
import java .util .HashMap ;
5
5
6
+ /**
7
+ * A generic implementation of a max heap data structure.
8
+ *
9
+ * @param <T> the type of elements in this heap, must extend Comparable.
10
+ */
6
11
public class GenericHeap <T extends Comparable <T >> {
7
12
8
- ArrayList <T > data = new ArrayList <>();
9
- HashMap <T , Integer > map = new HashMap <>();
13
+ private final ArrayList <T > data = new ArrayList <>();
14
+ private final HashMap <T , Integer > map = new HashMap <>();
10
15
16
+ /**
17
+ * Adds an item to the heap, maintaining the heap property.
18
+ *
19
+ * @param item the item to be added
20
+ */
11
21
public void add (T item ) {
12
22
this .data .add (item );
13
- map .put (item , this .data .size () - 1 ); //
23
+ map .put (item , this .data .size () - 1 );
14
24
upHeapify (this .data .size () - 1 );
15
25
}
16
26
27
+ /**
28
+ * Restores the heap property by moving the item at the given index upwards.
29
+ *
30
+ * @param ci the index of the current item
31
+ */
17
32
private void upHeapify (int ci ) {
18
33
int pi = (ci - 1 ) / 2 ;
19
- if (isLarger (this .data .get (ci ), this .data .get (pi )) > 0 ) {
34
+ if (ci > 0 && isLarger (this .data .get (ci ), this .data .get (pi )) > 0 ) {
20
35
swap (pi , ci );
21
36
upHeapify (pi );
22
37
}
23
38
}
24
39
40
+ /**
41
+ * Displays the contents of the heap.
42
+ */
25
43
public void display () {
26
44
System .out .println (this .data );
27
45
}
28
46
47
+ /**
48
+ * Returns the number of elements in the heap.
49
+ *
50
+ * @return the size of the heap
51
+ */
29
52
public int size () {
30
53
return this .data .size ();
31
54
}
32
55
56
+ /**
57
+ * Checks if the heap is empty.
58
+ *
59
+ * @return true if the heap is empty, false otherwise
60
+ */
33
61
public boolean isEmpty () {
34
62
return this .size () == 0 ;
35
63
}
36
64
65
+ /**
66
+ * Removes and returns the maximum item from the heap.
67
+ *
68
+ * @return the maximum item
69
+ */
37
70
public T remove () {
71
+ if (isEmpty ()) {
72
+ throw new IllegalStateException ("Heap is empty" );
73
+ }
38
74
this .swap (0 , this .size () - 1 );
39
75
T rv = this .data .remove (this .size () - 1 );
40
- downHeapify (0 );
41
76
map .remove (rv );
77
+ downHeapify (0 );
42
78
return rv ;
43
79
}
44
80
81
+ /**
82
+ * Restores the heap property by moving the item at the given index downwards.
83
+ *
84
+ * @param pi the index of the current item
85
+ */
45
86
private void downHeapify (int pi ) {
46
87
int lci = 2 * pi + 1 ;
47
88
int rci = 2 * pi + 2 ;
@@ -58,15 +99,35 @@ private void downHeapify(int pi) {
58
99
}
59
100
}
60
101
102
+ /**
103
+ * Retrieves the maximum item from the heap without removing it.
104
+ *
105
+ * @return the maximum item
106
+ */
61
107
public T get () {
62
- return this .data .get (0 );
108
+ if (isEmpty ()) {
109
+ throw new IllegalStateException ("Heap is empty" );
110
+ }
111
+ return this .data .getFirst ();
63
112
}
64
113
65
- // t has higher property then return +ve
114
+ /**
115
+ * Compares two items to determine their order.
116
+ *
117
+ * @param t the first item
118
+ * @param o the second item
119
+ * @return a positive integer if t is greater than o, negative if t is less, and zero if they are equal
120
+ */
66
121
private int isLarger (T t , T o ) {
67
122
return t .compareTo (o );
68
123
}
69
124
125
+ /**
126
+ * Swaps two items in the heap and updates their indices in the map.
127
+ *
128
+ * @param i index of the first item
129
+ * @param j index of the second item
130
+ */
70
131
private void swap (int i , int j ) {
71
132
T ith = this .data .get (i );
72
133
T jth = this .data .get (j );
@@ -76,9 +137,16 @@ private void swap(int i, int j) {
76
137
map .put (jth , i );
77
138
}
78
139
140
+ /**
141
+ * Updates the priority of the specified item by restoring the heap property.
142
+ *
143
+ * @param item the item whose priority is to be updated
144
+ */
79
145
public void updatePriority (T item ) {
146
+ if (!map .containsKey (item )) {
147
+ throw new IllegalArgumentException ("Item not found in the heap" );
148
+ }
80
149
int index = map .get (item );
81
- // because we enter lesser value then old vale
82
150
upHeapify (index );
83
151
}
84
152
}
0 commit comments