Question

In: Computer Science

Write pseudo code for Min_Heap_Delete(A,i) which deletes the item at position i in Min_Heap A. Assume...

Write pseudo code for Min_Heap_Delete(A,i) which deletes the item at position i in Min_Heap A. Assume that A is a one-dimensional array. Hint:Think about “bubbling up” and “bubbling down” and the operations that do these, then give the best upper bound you can on the worst case running time of your method, in ordered notations.

Solutions

Expert Solution

import java.util.*;

import java.io.*;

public class Main {

public static void main(String[] args) throws IOException {

Scanner sc = new Scanner(System.in);

int t=sc.nextInt();

while(t-->0) {

int n = sc.nextInt();

int k = sc.nextInt();

MinHeap minHeap = new MinHeap(n);

for (int i = 0; i < n; i++) {

minHeap.insert(sc.nextInt());

}

minHeap.deleteKey(k);

minHeap.printHeap(sc);

System.out.println();

}

}

}

class MinHeap

{

private int[]Heap;

private int size;

private int maxSize;

private static final int FRONT = 1;

MinHeap(int maxSize)

{

this.maxSize = maxSize;

size=0;

Heap = new int[this.maxSize+1];

Heap[0] = Integer.MIN_VALUE;

}

private int parent(int pos) {

return pos / 2;

}

private int leftChild(int pos)

{

return (2*pos);

}

private int rightChild(int pos)

{

return (2*pos)+1;

}

private boolean isLeaf(int pos)

{

if(pos>=(size/2) && pos<=size)

return true;

return false;

}

private void swap(int fpos, int spos)

{

int temp;

temp = Heap[fpos];

Heap[fpos]=Heap[spos];

Heap[spos]=temp;

}

private void minHeapify(int pos)

{

int left = leftChild(pos);

int right = rightChild(pos);

int largest = pos;

if(left<=size && Heap[left]<Heap[largest])

largest=left;

if(right<=size && Heap[right]<Heap[largest])

largest = right;

if(largest!=pos)

{

swap(pos, largest);

minHeapify(largest);

}

}

void insert(int element)

{

if(size>=maxSize)

return;

Heap[++size]=element;

int i=size;

while(Heap[i]<Heap[parent(i)])

{

swap(i,parent(i));

i=parent(i);

}

}

private void build_heap()

{

int j=(int)Math.floor(size/2.0);

for(int i=j;i>=1;i--){

minHeapify(i);

}

}

public void heapSort(Writer wr) throws IOException {

build_heap();

int length=size;

for(int i=size;i>=2;i--)

{

swap(1,i);

size--;

minHeapify(1);

}

size=length;

// printHeap(wr);

}

public int remove()

{

int popped = Heap[FRONT];

Heap[FRONT] = Heap[size];

size=size-1;

minHeapify(FRONT);

return popped;

}

public void deleteKey(int i)

{

decreaseKey(i, Integer.MIN_VALUE);

remove();

}

private void decreaseKey(int i, int new_val) {

Heap[i]=new_val;

while(i !=0 && Heap[parent(i)]>Heap[i])

{

swap(i,parent(i));

i=parent(i);

}

}

void printHeap(Scanner sc) throws IOException {

for(int i=1;i<=size;i++)

{

//wr.write(Heap[i]+" ");

System.out.print(Heap[i]+" ");

}

}

}


Related Solutions

Write PSEUDO CODE! for Min_Heap_Delete(A,i) which deletes the item at position i in Min_Heap A. Assume...
Write PSEUDO CODE! for Min_Heap_Delete(A,i) which deletes the item at position i in Min_Heap A. Assume that A is a one-dimensional array. Hint:Think about “bubbling up” and “bubbling down” and the operations that do these, then give the best upper bound you can on the worst case running time of your method, in ordered notations.
Synchronize producer and consumer threads that use 10-item buffer for communication. Write pseudo code for both...
Synchronize producer and consumer threads that use 10-item buffer for communication. Write pseudo code for both threads. Assume that you can use void Buffer::store(Item item) and Item Buffer::get() methods, but you need to explicitly ensure that you never store more than 10 items in the buffer (to prevent overflow).
A customer in a grocery store is purchasing three items. Write the pseudo code that will:...
A customer in a grocery store is purchasing three items. Write the pseudo code that will: • Ask the user to enter the name of the first item purchased. Then ask the user to enter the cost of the first item purchased. Make your program user friendly. If the user says the first item purchased is milk, then ask: “What is the cost of milk.” [This should work no matter what item is entered by the user. I might buy...
Write a member function that deletes all repetitions from the bag. In your implementation, assume that...
Write a member function that deletes all repetitions from the bag. In your implementation, assume that items can be compared for equality using ==. Use the following header for the function: void remove_repetitions() Here is a brief outline of an algorithm: - A node pointer p steps through the bag - For each Item, define a new pointer q equal to p - While the q is not the last Item in the bag ◼ If the next Item has...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
Write a Pseudo Code to send an Array of 20 elements from 8051 to the computer...
Write a Pseudo Code to send an Array of 20 elements from 8051 to the computer via serial port at maximum baud rate possible with XTAL=11.0592MHz.
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java program.
What will be the expected output of the following pseudo code? Write exactly what would display...
What will be the expected output of the following pseudo code? Write exactly what would display when you execute the statements. Module main() Declare Integer a = 5 Declare Integer b = 2 Declare Integer c = 3 Declare Integer result = 0 Display "The value of result is" Display result Set result = a + b * c - a Display "Changed value is: ", result End Module
(Artificial Intelligence) Write a pseudo code for the following: Regular Hill Climbing with steepest ascent
(Artificial Intelligence) Write a pseudo code for the following: Regular Hill Climbing with steepest ascent
For the following program descriptions, write step by step pseudo code that shows you understand the...
For the following program descriptions, write step by step pseudo code that shows you understand the problem and what it takes to solve it. The first one is done for you as an example. Please answer the questions in the same format as the example problem below so it is the same. Example #1 Problem A customer is purchasing five items. Design a program where you collect the amount of each item, calculate the subTotal of the items, the tax...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT