Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 61094b3

Browse files
committed
Update Heap.java
1 parent 02867cf commit 61094b3

File tree

1 file changed

+105
-0
lines changed
  • DataAndAlgoL/Chpt7BinaryHeapsPriorityQs

1 file changed

+105
-0
lines changed

‎DataAndAlgoL/Chpt7BinaryHeapsPriorityQs/Heap.java‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,109 @@ public void percolateDown(int i){
7979

8080
percolateDown(max); //recursive call untill all nodes are correct location.
8181
}
82+
83+
//Deleting an element from the heap, we only need to delete the element from the root
84+
/*
85+
* After deleting the root element, copy the last element of the heap and delete that last element.
86+
* After replacing the last element, tree may not satisfy heap property and hence we need to make it a heap again
87+
* by calling percolate down function
88+
*/
89+
90+
int DeleteMax(){
91+
if(this.count==0){
92+
return -1;
93+
}
94+
95+
int data= this.array[0];
96+
this.array[0]= this.array[this.count-1];
97+
this.count--; //reduces heap size
98+
percolateDown(0);
99+
return data;
100+
}
101+
102+
//Inserting an element is simliar to heapifying and deleting, but instead of percolating down we use percolate up function
103+
/*
104+
* increase the heap size
105+
* keep new element at end of the heap
106+
* heapify the element from bottom to top (root)
107+
*/
108+
public void Insert(int data){
109+
int i;
110+
if(this.count ==this.capacity){
111+
resizeHeap();
112+
}
113+
114+
this.count++; //increasing the heap size to hold this new item
115+
i= this.count-1;
116+
117+
while(i>=0 && data > this.array[(i-2)/2]){
118+
this.array[i] =this.array[(i-1)/2];
119+
i= i-1/2;
120+
}
121+
122+
this.array[i]=data;
123+
}
124+
125+
public void resizeHeap(){
126+
int[] array_old= new int[this.capacity];
127+
System.arraycopy(this.array,0,array_old, this.count-1, capacity);
128+
this.array= new int[this.capacity*2];
129+
if(this.array== null){
130+
System.out.println("Memory error");
131+
return;
132+
}
133+
134+
for(int i=0; i< this.capacity; i++){
135+
this.array[i]=array_old[i];
136+
}
137+
138+
this.capacity*=2;
139+
array_old=null;
140+
}
141+
142+
public void DestroyHeap(){
143+
this.count=0;
144+
this.array=null;
145+
}
146+
147+
//in case we already have an array we have to heapify the array so it can become a heap
148+
/*
149+
*
150+
*/
151+
152+
public void BuildHeap(Heap h, int[] A, int n){
153+
if(h== null){
154+
return;
155+
}
156+
157+
while(n > this.capacity){
158+
h.resizeHeap();
159+
}
160+
161+
for(int i=0; i< n; i++){
162+
h.array[i] = A[i];
163+
}
164+
165+
this.count=n;
166+
for(int i= (n-1)/2; i >=0; i--){
167+
h.percolateDown(i);
168+
}
169+
}
170+
171+
172+
//HEAP SORT -> INSERTS ALL ELEMENTS FROM UNSORTED ARRAY INTO HEAP, THEN REMOVES THE ROOT ELEMENTS UNTIL THE HEAP IS EMPTY
173+
public void heapSort(int[] A, int n){
174+
Heap h= new Heap(n,0);
175+
int old_size= h.count;
176+
int temp;
177+
178+
for(int i= n-1; i>0; i--){ //h.array[0] is the largest element
179+
temp=h.array[0];
180+
h.array[0]= h.array[h.count-1];
181+
h.count--;
182+
h.percolateDown(0);
183+
}
184+
185+
h.count= old_size;
186+
}
82187
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /