A Way For Learning

Kth Smallest Element using Max Heap

No comments
Implement a priority queue to find the k th smallest element using max heap.

------------------------------------Program----------------------------------------------------------------
import java.util.*;
class NoSmallestElementFound extends Exception
{
NoSmallestElementFound(String s)
{
super(s);
}

}
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   max 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   max heap
{
int temp = arr[parent];
arr[parent] =arr[i];
arr[i] = temp;
i = parent;   //traversing up
parent = (i-1)/2;
}
}

public void delete_max()
{
int maxele=0;
if(size!=0)
{
maxele = arr[0];
 arr[0] = arr[size-1]; //copy last emelent to front
 if(!(size<=3))
    arr[size-1] =-99999;
 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]==-9999))
{
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_max()
{
int max = 0;
if(size!=0)
{
max = arr[0];
}
return max;
}

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 getSmallest(int m) throws NoSmallestElementFound //throws NoSmallestElementFound
{
if(m<=size){
int tsize = size;
int u=0;
int[] q= new int[100];
while(u!=tsize-m)
{
q[u]= extract_max();
u++;
delete_max();
}
int p= extract_max();
for(int k=0;k<u;k++)
insert(q[k]);
return p;
}
else
{
throw new NoSmallestElementFound("No Smallest 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.getSmallest(1);
    System.out.println(s);  
}    
}

No comments :

Post a Comment