The replaceKey method replaces the first occurrence of the oldKey with the newKey in the binary heap. It searches for the oldKey in the heap and if found, replaces it with the newKey and calls the buildHeap method to restore the min-heap property. If oldKey is not found, it prints a message and returns without changing the heap.
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Write a method replaceKey in the BinaryHeap class with the following .docx
1. Write a method replaceKey in the BinaryHeap class with the
following signature: public void replaceKey(Integer oldKey,
Integer newKey) The method will replace the first occurrence of
oldKey with the newKey, and restore the Min-Heap property
after the change. If the oldKey does not exist in the heap, the
method prints an appropriate message and returns without
changing the heap. Suppose our binary heap object (bh) has the
following keys: Then the method call: bh.replaceKey (new
Integer(10), new Integer(3)) should change the keys to:
Solution
replaceKey Method:
/**
* replace oldKey with newKey
*/
public void replaceKey(AnyType oldKey,AnyType newKey)
{
int i,flag=0;//flag for key exits or not
for(i=1;i<size;i++)
{
AnyType temp=(AnyType) heap[i];//convert heap[i] vlaue into
AnyType
if(oldKey.compareTo(temp)==0)//check oldKey==temp or no
{
2. flag=1;//set flag one if key exist
break;
}
}
if(flag==0)
{
System.out.println("old key is not exist in the heap.");
}
else
{
heap[i]=(AnyType)newKey;//assign newKey to the oldKey
position
buildHeap(); // call build Heap methode
}
}
output:
Heap before repalcement : 5 10 9 11 15 10 12 13 14 16 18
Heap after replacement 10 witn 3,the heap is: 3 5 9 11 15 10 12
13 14 16 18
program:
//Heap.java
import java.util.*;
@SuppressWarnings("unchecked")
public class Heap<AnyType extends Comparable<AnyType>>
{
3. private static final int CAPACITY = 2;
private int size; // Number of elements in heap
private AnyType[] heap; // The heap array
public Heap()
{
size = 0;
heap = (AnyType[]) new Comparable[CAPACITY];
}
/**
* Construct the binary heap given an array of items.
*/
public Heap(AnyType[] array)
{
size = array.length;
heap = (AnyType[]) new Comparable[array.length+1];
System.arraycopy(array, 0, heap, 1, array.length);//we do not
use 0 index
buildHeap();
}
/**
* runs at O(size)
*/
private void buildHeap()
{
for (int k = size/2; k > 0; k--)
5. int i,flag=0;//flag for key exits or not
for(i=1;i<size;i++)
{
AnyType temp=(AnyType) heap[i];//convert heap[i] vlaue into
AnyType
if(oldKey.compareTo(temp)==0)//check oldKey==temp or no
{
flag=1;//set flag one if key exist
break;
}
}
if(flag==0)
{
System.out.println("old key is not exist in the heap.");
}
else
{
heap[i]=(AnyType)newKey;//assign newKey to the oldKey
position
buildHeap(); // call build Heap methode
}
}
/**
* Inserts a new item
6. */
public void insert(AnyType x)
{
if(size == heap.length - 1) doubleSize();
//Insert a new item to the end of the array
int pos = ++size;
//Percolate up
for(; pos > 1 && x.compareTo(heap[pos/2]) < 0; pos = pos/2 )
heap[pos] = heap[pos/2];
heap[pos] = x;
}
private void doubleSize()
{
AnyType [] old = heap;
heap = (AnyType []) new Comparable[heap.length * 2];
System.arraycopy(old, 1, heap, 1, size);
}
public String toString()
{
String out = "";
for(int k = 1; k <= size; k++) out += heap[k]+" ";
return out;
}
public static void main(String[] args)
{
7. //create an integer array
Integer[] b = {5,10,9,11,15,10,12,13,14,16,18};
//create Integer heap
Heap<Integer> heap = new Heap<Integer>(b);
System.out.println("Heap before repalcement : "+heap);
heap.replaceKey(new Integer(10),new Integer(3));
System.out.println("Heap after replacement 10 witn 3,the heap
is: "+heap);
}
}