File tree 1 file changed +7
-7
lines changed
1 file changed +7
-7
lines changed Original file line number Diff line number Diff line change 3
3
import java .util .ArrayList ;
4
4
import java .util .Comparator ;
5
5
6
- public class genericheap <T > {
6
+ public class genericheap <T > { // create a generic heap class <T> , where T can be of any type.
7
7
8
8
private ArrayList <T > data = new ArrayList <>();
9
9
private Comparator <T > ctor ;
10
10
11
- public genericheap (Comparator <T > ctor ) {
11
+ public genericheap (Comparator <T > ctor ) { // constructor to initialize the generic comparator
12
12
this .ctor =ctor ;
13
13
}
14
14
15
- public int size () {
15
+ public int size () { // returns the size of the arraylist data
16
16
return data .size ();
17
17
}
18
18
19
- public boolean isEmpty () {
19
+ public boolean isEmpty () { // checks whether the list is empty or not :: return true or false for the same
20
20
return data .isEmpty ();
21
21
}
22
22
23
- public void display () {
23
+ public void display () { //displays the list
24
24
System .out .println (this .data );
25
25
}
26
26
27
- public void add (T integer ) {
27
+ public void add (T integer ) { // in this function we have added the <t> type object into the arraylist and called upheapify
28
28
data .add (integer );
29
29
upheapify (data .size () - 1 );
30
30
}
@@ -53,7 +53,7 @@ private boolean isLarger(int i, int j) {
53
53
}
54
54
}
55
55
56
- private void swap (int ci , int pi ) {
56
+ private void swap (int ci , int pi ) { // swap function written like this because of the generic property
57
57
T ith = data .get (ci );
58
58
T jth =data .get (pi );
59
59
data .set (ci , jth );
You can’t perform that action at this time.
0 commit comments