File tree Expand file tree Collapse file tree 1 file changed +7
-7
lines changed
Expand file tree Collapse file tree 1 file changed +7
-7
lines changed Original file line number Diff line number Diff line change 33import java .util .ArrayList ;
44import java .util .Comparator ;
55
6- public class genericheap <T > {
6+ public class genericheap <T > { // create a generic heap class <T> , where T can be of any type.
77
88 private ArrayList <T > data = new ArrayList <>();
99 private Comparator <T > ctor ;
1010
11- public genericheap (Comparator <T > ctor ) {
11+ public genericheap (Comparator <T > ctor ) { // constructor to initialize the generic comparator
1212 this .ctor =ctor ;
1313 }
1414
15- public int size () {
15+ public int size () { // returns the size of the arraylist data
1616 return data .size ();
1717 }
1818
19- public boolean isEmpty () {
19+ public boolean isEmpty () { // checks whether the list is empty or not :: return true or false for the same
2020 return data .isEmpty ();
2121 }
2222
23- public void display () {
23+ public void display () { //displays the list
2424 System .out .println (this .data );
2525 }
2626
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
2828 data .add (integer );
2929 upheapify (data .size () - 1 );
3030 }
@@ -53,7 +53,7 @@ private boolean isLarger(int i, int j) {
5353 }
5454 }
5555
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
5757 T ith = data .get (ci );
5858 T jth =data .get (pi );
5959 data .set (ci , jth );
You can’t perform that action at this time.
0 commit comments