33public class CustomMinHeap <T extends Comparable <T >> {
44
55 ArrayList <T > list ;
6+ //constructor
67 public CustomMinHeap (){
78 list = new ArrayList <>();
89 }
10+ //function to insert into min heap
911 public void insert (T val ){
1012 list .add (val );
1113 upHeapify (list .size ()-1 );
1214 }
15+ //function to get the index of the parent
1316 public int getParentIndex (int index ){
1417 return (index -1 )/2 ;
1518 }
19+ //function to get the index of the left child
1620 public int getLeftChildIndex (int index ){
1721 return 2 *index + 1 ;
1822 }
23+ //function to return no of elements in the heap
1924 public int size (){
2025 return list .size ();
2126 }
27+ //function to upheapify
2228 public void upHeapify (int index ){
2329 if (index ==0 ){
2430 return ;
2531 }
32+ //getting parent index
2633 int parentindex = getParentIndex (index );
34+ //if the parent value is greater than current node value swap them since parent has to be minimum
2735 if (list .get (parentindex ).compareTo (list .get (index )) > 0 ){
2836 T temp = list .get (parentindex );
2937 list .set (parentindex ,list .get (index ));
@@ -32,39 +40,49 @@ public void upHeapify(int index){
3240 }
3341
3442 }
35-
43+
44+ //function to remove the element from the heap
3645 public T remove (){
3746 if (list .size ()==0 ){return null ;}
3847 if (list .size ()==1 ){return list .remove (0 );}
3948 T val = list .get (0 );
4049 T temp = val ;
50+ //swap root elment with the last element
4151 list .set (0 ,list .get (list .size ()-1 ));
4252 list .set (list .size ()-1 ,temp );
53+ //remove the last element
4354 list .remove (list .size ()-1 );
4455 downHeapify (0 );
4556 return val ;
4657 }
58+ //function to downheapify
4759 public void downHeapify (int index ){
4860 int minimum = index ;
61+ //getting left child index
4962 int left =getLeftChildIndex (index ) ;
63+ //getting right child index
5064 int right =getRightChildIndex (index );
51-
65+ //if left child value is smaller than current minimum index value set minimum to left child index
5266 if (left < list .size () && list .get (left ).compareTo (list .get (minimum ) ) < 0 ){
5367 minimum = left ;
5468 }
69+ //if right child value is smaller than current minimum index value set minimum to right child index
5570 if (right < list .size () && list .get (right ).compareTo (list .get (minimum ) ) < 0 ){
5671 minimum = right ;
5772 }
73+ //swap minimum index value to current index value if minimum !=index
5874 if (minimum !=index ){
5975 T temp = list .get (minimum );
6076 list .set (minimum ,list .get (index ));
6177 list .set (index ,temp );
6278 downHeapify (minimum );
6379 }
6480 }
81+ //function to get the index of the right child
6582 public int getRightChildIndex (int index ){
6683 return 2 *index + 2 ;
6784 }
85+ //function to peek into min heap and get the root
6886 public T peek () throws Exception {
6987 if (list .size ()==0 ){
7088 throw new Exception ("Heap is empty.Cannot peek" );
@@ -76,17 +94,25 @@ public String toString() {
7694 return list .toString ();
7795 }
7896 public static void main (String [] args ) throws Exception {
97+ //Initialize the heap
7998 CustomMinHeap <Integer > minHeap = new CustomMinHeap <>();
99+ //inserting into the heap
80100 minHeap .insert (5 );
81101 minHeap .insert (500 );
82102 minHeap .insert (10 );
83103 minHeap .insert (100 );
84104 minHeap .insert (15 );
85105 minHeap .insert (20 );
106+ //display the heap
86107 System .out .println (minHeap );
108+ //get the element at the top
87109 System .out .println (minHeap .peek ());
110+ //remove the min element and print it
88111 System .out .println (minHeap .remove ());
112+ //display the heap after removing min element
89113 System .out .println (minHeap );
114+ //display the size of the heap
115+ System .out .println (minHeap .size ());
90116
91117 }
92118}
0 commit comments