Skip to content

Commit f85701d

Browse files
comments added
1 parent 8c4651c commit f85701d

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

Data Structure/Heap/CustomMaxHeap.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
public class CustomMaxHeap <T extends Comparable<T>> {
66
ArrayList<T> list;
7+
//constructor
78
public CustomMaxHeap(){
89
list = new ArrayList<>();
910
}
11+
//function to insert into max heap
1012
public void insert(T val){
1113
list.add(val);
1214
upHeapify(list.size()-1);
1315
}
16+
//function to peek into max heap and get the root
1417
public T peek() throws Exception {
1518
if(list.size()==0){
1619
throw new Exception("Heap is empty.Cannot peek");
@@ -22,43 +25,56 @@ public T peek() throws Exception {
2225
public String toString() {
2326
return list.toString();
2427
}
28+
//function to return no of elements in the heap
2529
public int size(){
2630
return list.size();
2731
}
32+
//function to remove the element from the heap
2833
public T remove(){
2934
if(list.size()==0){return null;}
3035
if(list.size()==1){return list.remove(0);}
3136
T val = list.get(0);
3237
T temp = val;
38+
//swap root elment with the last element
3339
list.set(0,list.get(list.size()-1));
3440
list.set(list.size()-1,temp);
41+
//remove the last element
3542
list.remove(list.size()-1);
3643
downHeapify(0);
3744
return val;
3845
}
46+
//function to downheapify
3947
public void downHeapify(int index){
48+
4049
int maximum = index;
41-
int left =getLeftChildIndex(index) ;
50+
//getting left child index
51+
int left =getLeftChildIndex(index);
52+
//getting right child index
4253
int right =getRightChildIndex(index);
43-
54+
//if left child value is greater than current maximum index value set maximum to left child index
4455
if(left < list.size() && list.get(left).compareTo(list.get(maximum) ) > 0){
4556
maximum = left ;
4657
}
58+
//if right child value is greater than current maximum index value set maximum to right child index
4759
if(right< list.size() && list.get(right).compareTo(list.get(maximum) ) > 0){
4860
maximum = right;
4961
}
62+
//swap maximum index value to current index value if maximum !=index
5063
if(maximum !=index){
5164
T temp = list.get(maximum);
5265
list.set(maximum,list.get(index));
5366
list.set(index,temp);
5467
downHeapify(maximum);
5568
}
5669
}
70+
//function to upheapify
5771
public void upHeapify(int index){
5872
if(index==0){
5973
return;
6074
}
75+
//getting parent index
6176
int parentindex = getParentIndex(index);
77+
//if the parent value is smaller than current node value swap them since parent has to be maximum
6278
if(list.get(parentindex).compareTo(list.get(index)) < 0){
6379
T temp = list.get(parentindex);
6480
list.set(parentindex,list.get(index));
@@ -67,28 +83,37 @@ public void upHeapify(int index){
6783
}
6884

6985
}
86+
//function to get the index of the parent
7087
public int getParentIndex(int index){
7188
return (index -1)/2;
7289
}
90+
//function to get the index of the left child
7391
public int getLeftChildIndex(int index){
7492
return 2*index + 1;
7593
}
76-
94+
//function to get the index of the right child
7795
public int getRightChildIndex(int index){
7896
return 2*index + 2;
7997
}
8098
public static void main(String[] args) throws Exception {
99+
//Initialize the heap
81100
CustomMaxHeap<Integer> maxHeap = new CustomMaxHeap<>();
101+
//inserting into the heap
82102
maxHeap.insert(5);
83103
maxHeap.insert(500);
84104
maxHeap.insert(10);
85105
maxHeap.insert(100);
86106
maxHeap.insert(15);
87107
maxHeap.insert(20);
108+
//display the heap
88109
System.out.println(maxHeap);
110+
//get the element at the top
89111
System.out.println(maxHeap.peek());
112+
//remove the max element and print it
90113
System.out.println(maxHeap.remove());
114+
//display the heap after removing max element
91115
System.out.println(maxHeap);
116+
//display the size of the heap
92117
System.out.println(maxHeap.size());
93118
}
94119
}

0 commit comments

Comments
 (0)