k th largest element using Min Heap
Implement a priority queue to find the k th largest element using min heap.
----------------------------------Program------------------------------
import java.util.*;
public class PriorityQueue {
int[] arr ;
int size;
int max;
public PriorityQueue()
{
size = 0;
max = 20;
arr = new int[max];
}
public void insert(int e)//E e)
{
if(size==max)
{
System.out.println("full");
}
else
{
arr[size++] = e;
heapify(); //
}
}
public void heapify() //heapfiy from down to up
{
int i = size-1;
int parent = (i-1)/2;
while(arr[parent]> arr[i]) //heap min heap
{
int temp = arr[parent];
arr[parent] =arr[i];
arr[i] = temp;
i = parent; //traversing up
parent = (i-1)/2;
}
}
public void heapify(int index) //heapfiy from down to up //HEAPIFY UP
{
int i = index;
int parent = (i-1)/2;
while(arr[parent]> arr[i]) //heap min heap
{
int temp = arr[parent];
arr[parent] =arr[i];
arr[i] = temp;
i = parent; //traversing up
parent = (i-1)/2;
}
}
public void delete_min()
{
int min=0;
if(size!=0)
{
min = arr[0];
arr[0] = arr[size-1]; //copy last emelent to front
if(!(size<=3))
arr[size-1] =0;
else
arr[size-1] =99999;
size--; //ignore last element
}
heapifyDown(); // incomplete code
}
public void heapifyDown() //heapify min heap from root node..after deleteion of root node new lel,ement is root node
{
for(int parent = 0 ; parent< ((size)/2 ) ;parent++) //only parents
{
int right = parent * 2 +2;
int left = parent *2 +1;
if(arr[left]< arr[parent] && (arr[left] <=arr[right] || arr[right]==0))
{
int temp = arr[left];
arr[left] = arr[parent];
arr[parent] = temp;
parent = left-1; // -1 to compensate parent++ in for loop
}
else if(arr[right]< arr[parent] && (arr[left] >= arr[right] ))
{
int temp = arr[right];
arr[right] = arr[parent];
arr[parent] = temp;
parent = right-1; // -1 to compensate parent++ in for loop
}
else
break;
}
}
public void heapifyDown(int index) //heapfiy min heap from a particular index
{
int tempParent = getParent(index);
for(int parent = tempParent ; parent< (size)/2 ;) //only parents
{
int right = parent * 2 +2;
int left = parent *2 +1;
if(arr[left]< arr[parent] && (arr[left] <=arr[right] || arr[right]==0))
{
int temp = arr[left];
arr[left] = arr[parent];
arr[parent] = temp;
parent = left;
}
else if(arr[right]< arr[parent] && (arr[left] >= arr[right] ))
{
int temp = arr[right];
arr[right] = arr[parent];
arr[parent] = temp;
parent = right;
}
else
break;
}
}
private int getParent(int index) //get parent index for a child
{
return (index-1)/2;
}
private int right(int index)
{
return (index*2) + 2;
}
private int left(int index)
{
return (index*2) + 1;
}
public boolean isChild(int index)
{
if(index >= ( size/2) +1 )
return true;
else
return false;
}
public void swap(int index1, int index2)
{
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
public int extract_min()
{
int min = 0;
if(size!=0)
{
min = arr[0];
}
return min;
}
public void list()
{
for(int i =0 ;i<size;i++)
{
System.out.print(arr[i]);
if(i!=size-1)
{
System.out.print(" ");
}
}
System.out.println("");
}
public void modify(int index, int value)
{
for(int i =0 ;i<size;i++)
{
if(i+1==index)
{
int temp = arr[i];
arr[i]=value;
if(value>temp)
heapifyDown(i);
else if(value<temp)
heapify(i); //smaller value there for heapify up
break;
}
}
}
public int getLargest(int m) throws Exception
{
if(m<=size){
int tsize = size;
int u=0;
int[] q= new int[100];
while(u!=tsize-m)
{
q[u]= extract_min();
u++;
delete_min();
}
int p= extract_min();
for(int k=0;k<u;k++)
insert(q[k]);
return p;
}
else
{
throw new Exception("No largest element found");
}
}
public static void main(String[] args) throws Exception {
PriorityQueue pq = new PriorityQueue();
pq.insert(1);
pq.insert(2);
pq.insert(3);
pq.insert(4);
pq.insert(5);
int s=pq.getLargest(1);
System.out.println(s);
}
}
----------------------------------Program------------------------------
import java.util.*;
public class PriorityQueue {
int[] arr ;
int size;
int max;
public PriorityQueue()
{
size = 0;
max = 20;
arr = new int[max];
}
public void insert(int e)//E e)
{
if(size==max)
{
System.out.println("full");
}
else
{
arr[size++] = e;
heapify(); //
}
}
public void heapify() //heapfiy from down to up
{
int i = size-1;
int parent = (i-1)/2;
while(arr[parent]> arr[i]) //heap min heap
{
int temp = arr[parent];
arr[parent] =arr[i];
arr[i] = temp;
i = parent; //traversing up
parent = (i-1)/2;
}
}
public void heapify(int index) //heapfiy from down to up //HEAPIFY UP
{
int i = index;
int parent = (i-1)/2;
while(arr[parent]> arr[i]) //heap min heap
{
int temp = arr[parent];
arr[parent] =arr[i];
arr[i] = temp;
i = parent; //traversing up
parent = (i-1)/2;
}
}
public void delete_min()
{
int min=0;
if(size!=0)
{
min = arr[0];
arr[0] = arr[size-1]; //copy last emelent to front
if(!(size<=3))
arr[size-1] =0;
else
arr[size-1] =99999;
size--; //ignore last element
}
heapifyDown(); // incomplete code
}
public void heapifyDown() //heapify min heap from root node..after deleteion of root node new lel,ement is root node
{
for(int parent = 0 ; parent< ((size)/2 ) ;parent++) //only parents
{
int right = parent * 2 +2;
int left = parent *2 +1;
if(arr[left]< arr[parent] && (arr[left] <=arr[right] || arr[right]==0))
{
int temp = arr[left];
arr[left] = arr[parent];
arr[parent] = temp;
parent = left-1; // -1 to compensate parent++ in for loop
}
else if(arr[right]< arr[parent] && (arr[left] >= arr[right] ))
{
int temp = arr[right];
arr[right] = arr[parent];
arr[parent] = temp;
parent = right-1; // -1 to compensate parent++ in for loop
}
else
break;
}
}
public void heapifyDown(int index) //heapfiy min heap from a particular index
{
int tempParent = getParent(index);
for(int parent = tempParent ; parent< (size)/2 ;) //only parents
{
int right = parent * 2 +2;
int left = parent *2 +1;
if(arr[left]< arr[parent] && (arr[left] <=arr[right] || arr[right]==0))
{
int temp = arr[left];
arr[left] = arr[parent];
arr[parent] = temp;
parent = left;
}
else if(arr[right]< arr[parent] && (arr[left] >= arr[right] ))
{
int temp = arr[right];
arr[right] = arr[parent];
arr[parent] = temp;
parent = right;
}
else
break;
}
}
private int getParent(int index) //get parent index for a child
{
return (index-1)/2;
}
private int right(int index)
{
return (index*2) + 2;
}
private int left(int index)
{
return (index*2) + 1;
}
public boolean isChild(int index)
{
if(index >= ( size/2) +1 )
return true;
else
return false;
}
public void swap(int index1, int index2)
{
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
public int extract_min()
{
int min = 0;
if(size!=0)
{
min = arr[0];
}
return min;
}
public void list()
{
for(int i =0 ;i<size;i++)
{
System.out.print(arr[i]);
if(i!=size-1)
{
System.out.print(" ");
}
}
System.out.println("");
}
public void modify(int index, int value)
{
for(int i =0 ;i<size;i++)
{
if(i+1==index)
{
int temp = arr[i];
arr[i]=value;
if(value>temp)
heapifyDown(i);
else if(value<temp)
heapify(i); //smaller value there for heapify up
break;
}
}
}
public int getLargest(int m) throws Exception
{
if(m<=size){
int tsize = size;
int u=0;
int[] q= new int[100];
while(u!=tsize-m)
{
q[u]= extract_min();
u++;
delete_min();
}
int p= extract_min();
for(int k=0;k<u;k++)
insert(q[k]);
return p;
}
else
{
throw new Exception("No largest element found");
}
}
public static void main(String[] args) throws Exception {
PriorityQueue pq = new PriorityQueue();
pq.insert(1);
pq.insert(2);
pq.insert(3);
pq.insert(4);
pq.insert(5);
int s=pq.getLargest(1);
System.out.println(s);
}
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment