1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
forsale [732]
3 years ago
5

Please write down a java program for below instruction.Min-Heap: Construct a Min_Heap from the following set of integers. Rememb

er to use successive adds to arrive at your final Heap. (You do not need to show all the intermediate steps, however, showing those can help in case you make an error somewhere down the line.)1, 19, 7, 5, 6, 42, 21, 13, 56, 78, 29, 3, 14
Remove_Min: Show the result of two successive Remove_Min operations on the heap formed in part a.Perform the following operations on the Min_Heap: add(45)
As you must have noticed the set of integers provided to both the BST and Heap problems were the same. However, the structures formed are vastly different.

What can you say about the relative heights (and therefore the worst-case cost of operations) of the BST as compared to the Heap?

What conclusions are you able to draw regarding the relative efficiency of the 2 data structures? (Remember that log213 = 3.7)
Computers and Technology
1 answer:
Leona [35]3 years ago
4 0

Answer:

Following are the program in the Java Programming Language.

//define class

class Heap{

 //set private integer data type array variable

  private int a[];

 //set two private integer data type variables

  private int s,capacity;

 //define private void function for down heap

  private void downheap(int indx) {

    //set if conditional statement

   if(indx>=s)return;

   //set integer type variable and initialize

   int lft=2*indx+1;

   int rgt=2*indx+2;

   int min=indx;

   //set the if-else if conditional statement  

   if(rgt<=s&&a[rgt]<a[indx])

     min=rgt;

   else if(lft<=s&&a[lft]<a[min])

     min=lft;

   //check index is not equal to min    

   if(indx!=min){

     //perform swapping

     int temp=a[indx];

     a[indx]=a[min];

     a[min]=temp;

     downheap(min);

   }  

  }

  //define private void type function for upheap

  private void upheap(int indx) {

      if(indx==0)return;

      int parent=(indx-1)/2;

      if(a[indx]<a[parent]) {

          int temp=a[indx];

          a[indx]=a[parent];

          a[parent]=temp;

          upheap(parent);

      }

  }

 

 public Heap(int q) {

   a=new int [q];

   s=0;

   capacity=q;

 }

 public int _size () {

   return s;

 }

 public int minLookup() {

   if(s>0)return a[0];

   return 0;

 }

 public int remove() {

   int data=a[0];

   a[0]=a[s-1];

   s--;

   downheap(0);

   return data;

 }

 

 public void add (int data) {

   if(s>=capacity)return;

   a[s++] = data;

   upheap(s-1);  

 }

 

 public void print_heap() {

   System.out.print("Heap : ");

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

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

   }

   System.out.println();

 }

}

//define class to define main method

public class Main {

 //main method

 public static void main(String srgs[]) {

   //cretae class object

   Heap heap = new Heap(20);

   //call function through object with argument list

   heap.add(1);

   heap.add(19);

   heap.add(7);

   heap.add(5);

   heap.add(6);

   heap.add(42);

   heap.add(21);

   heap.add(13);

   heap.add(56);

   heap.add(78);

   heap.add(29);

   heap.add(3);

   heap.add(14);

   heap.print_heap();

   //print the Output and remove the minimum element

   System.out.println("Minimum element : "+heap.remove());

   heap.print_heap();

   System.out.println("minimum element : "+heap.remove());

   heap.add(45);

   heap.print_heap();

  }

}

<u>Output:</u>

Heap : 1 5 3 13 6 7 21 19 56 78 29 42 14

Minimum element : 1

Heap : 3 5 7 13 6 14 21 19 56 78 29 42

Minimum element : 3

Heap : 7 5 21 13 6 14 42 19 56 78 29 45

Explanation:

Here, we define a class "Heap" inside the class.

  • Set private integer data type array variable.
  • set private two integer data type variables.
  • Then, we define private void type function "downheap()" for down heap and pass an integer data type argument list in its parameter "indx".
  • Then, we define private void type function "upheap()" for upper heap and pass an integer data type argument list in its parameter "indx".
  • Define constructor of the class for the capacity pf the array list.
  • Define void type function "add()" for adding elements in heap.
  • Define void type function "print_heap()" to print the heap.

Finally, we define class to define main function and we create the object of the class "Heap"  then, call all the functions through the class object abd print the output.

You might be interested in
A foreach statement can be used in place of the for statement whenever code looping through an array requires "access" to the co
Ede4ka [16]

Answer:

False

Explanation:

Foreach is useful when we want to do something for each element rather than repeating something for  n times

Answer is False

5 0
3 years ago
The Internet may best be compared to a/an
MA_775_DIABLO [31]
Internet is better than television because we can only movies or shows on television but we can watch anything we want on internet whether its relating to science stuff or some enjoying playful pranks.
8 0
3 years ago
Explain the process of a for loop and how it is used in code
marshall27 [118]

Ex:

FOR Count= 1 TO 50

.......................

Body

......................

NEXT

Next keeps incrementing count by 1

Hope it helps

4 0
3 years ago
Consider the following class definitions.public class Bike{private int numWheels = 2;// No constructor defined}public class EBik
nadya68 [22]

Answer:

The answer is "Option A".

Explanation:

Please find the complete program in the attached file.

The given program includes a super-class Bike having a private integer numWheels parameter. The class EBike inherits the class Bike, EBike comprises one parameter function Object() which utilizes its variable number of the private integer Battery level of the battery parameter and the wrong choice can be defined as follows:

In choice B, it is wrong since there is no constructor with a single argument in the Bike class.

In choice C, it is wrong since there no need to call the base class constructor with the super keyword.

In choice, D is wrong because there no need to create a constructor of the base class.

In choice, E is wrong because it does not require the second EBike constructor parameter.

4 0
3 years ago
What do you think is the most important factor affecting the collection of digital data and what impact do you think that this f
borishaifa [10]

<u>Answer:</u>

<u>Privacy concerns.</u>

<u>Explanation:</u>

Privacy concerns have played an important role in how we collect digital data. For example, privacy activists believe that an individual has the right to know how his or her information is being used and that such an individual also has the right to withhold such information.

Such issues have affected the ability of law enforcement agencies to investigate criminal activities. For example, an individual who is accused of a crime may claim a <em>right to withhold his personal information, such as his mobile device, and thus he refuses to give out such information.</em>

4 0
3 years ago
Other questions:
  • One of the essential skills that software development engineers must have is the proficiency in at least one of the modern compu
    6·1 answer
  • Which element of the word program window contains buttons for saving a document and for undoing, redoing, and repeating a change
    5·1 answer
  • Why are high-quality transformers wound with large diameter wire?
    8·1 answer
  • Setting up network encryption involves
    5·1 answer
  • When reading words with a scanner object, a word is defined as ____?
    15·1 answer
  • During side show mode, hitting the B key will do which one of these?
    7·1 answer
  • Why is failure important when you are designing a solution to a problem?
    6·2 answers
  • Which term describes the surrounding area of this image?
    10·1 answer
  • After you have located a program name from the Start menu, in order to create a shortcut on the desktop.
    8·1 answer
  • What is the difference between packaged and tailored software ​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!