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

JAVA CODE:

import java.util.*;
import java.io.*;

public class Main {

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

Scanner sc = new Scanner(System.in);
System.out.println("Enter t:");
int t=sc.nextInt();

while(t-->0) {
System.out.println("Enter n and k:");
int n = sc.nextInt();

int k = sc.nextInt();

Min_Heap Min_Heap = new Min_Heap(n);
System.out.println("Enter n values to insert_into_Heap into heap:");
for (int i = 0; i < n; i++) {

Min_Heap.insert_into_Heap(sc.nextInt());

}

Min_Heap.Min_Heap_Delete(k);

Min_Heap.print_the_Heap(sc);

System.out.println();

}

}

}

class Min_Heap

{

private int[]Heap;

private int size;

private int maxSize;

private static final int FRONT = 1;

Min_Heap(int maxSize)

{

this.maxSize = maxSize;

size=0;

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

Heap[0] = Integer.MIN_VALUE;

}

private int parent_Node(int position) {

return position / 2;

}

private int left_Child(int position)

{

return (2*position);

}

private int right_Child(int position)

{

return (2*position)+1;

}

private boolean Is_Leaf(int position)

{

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

return true;

return false;

}

private void Swap(int fposition, int sposition)

{

int temp;

temp = Heap[fposition];

Heap[fposition]=Heap[sposition];

Heap[sposition]=temp;

}

private void Min_Heapify(int position)

{

int left = left_Child(position);

int right = right_Child(position);

int largest = position;

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

largest=left;

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

largest = right;

if(largest!=position)

{

Swap(position, largest);

Min_Heapify(largest);

}

}

void insert_into_Heap(int data)

{

if(size>=maxSize)

return;

Heap[++size]=data;

int i=size;

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

{

Swap(i,parent_Node(i));

i=parent_Node(i);

}

}

private void build_Heap()

{

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

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

Min_Heapify(i);

}

}

public void heap_Sort(Writer wr) throws IOException {

build_Heap();

int length=size;

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

{

Swap(1,i);

size--;

Min_Heapify(1);

}

size=length;

// print_the_Heap(wr);

}

public int Remove()

{

int popped = Heap[FRONT];

Heap[FRONT] = Heap[size];

size=size-1;

Min_Heapify(FRONT);

return popped;

}

public void Min_Heap_Delete(int i)

{

decrease_Key(i, Integer.MIN_VALUE);

Remove();

}

private void decrease_Key(int i, int new_val) {

Heap[i]=new_val;

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

{

Swap(i,parent_Node(i));

i=parent_Node(i);

}

}
//Min_Heap_Delete
void print_the_Heap(Scanner sc) throws IOException {
System.out.println("The output after deleting the k-th index from heap will be: ");

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

{

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

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

}

}

}

OUTPUT SCREENSHOT:

Here I have given the inputs as:

t=1

n =5 and k=4

and the values inserted were

1 2 3 4 5

So since k is 4 will be deleted from the heap as you can see the output.

Hope you like the code and don't forget to give UPVOTE as it means a lot..THANKS:)


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).
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write...
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write an algorithm to calculate the recursive Matrix multiplication (or write with pseudo code) 3. Find the time complexity of your pseudo code and analyze the differences
I need this in PSEUDO CODE: This looks long, but only because I have to give...
I need this in PSEUDO CODE: This looks long, but only because I have to give you my answer to the first part. First part of the questions (already answered) GDOT has contacted you to help write code to control the cross walk signals in Georgia. You must create a Crosswalk Signal class with three hidden attributes (Walk, Hurry and Wait), two constructors (a default that sets all lights to on and an overloaded that sets Hurry to on for...
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...
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...
Given a BST and a sum, write pseudo code to determine if the tree has a...
Given a BST and a sum, write pseudo code to determine if the tree has a root- to-leaf path such that adding up all the values along the path equals the given sum. Given the below BST and sum = 49, the array is (8, 4, 10, 1, 0, 3, 9, 15, 16). Return true, as there exist a root-to-leaf path 8− > 10− > 15− > 16 which sum is 49.
Write an algorithm in pseudo code to find one element and delete it in a doubly...
Write an algorithm in pseudo code to find one element and delete it in a doubly linked list. Your algorithm will print the original list, request the user to put in an element to be deleted, then print the final list after the deletion is done. If the element doesn’t exist in the list, print "XXX is not in the list" where "XXX" should be the one you received from the user. Use the following as your test cases to...
Write a pseudo code program for a goal-based agent. The goal of the agent is to...
Write a pseudo code program for a goal-based agent. The goal of the agent is to find the exit of a labyrinth. The agent is not omniscient The agent can sense if it is next to a wall (in front, left or right) The agent can turn 90 degrees to the right or left The agent can drive 1unit forward The maze is constructed of paths that are 1 unit across (wide) Show a maze of your choosing and illustrate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT