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
This needs to be written in Java.
Slav-nsk [51]

Answer:

Here is Automobile.java

class Automobile {   //class name

   private int ID, year, milesPerGallon, speed = 0;   // private member variables of Automobile of int type

   private String model, vinNo, make, color;   // private member variables of Automobile of String type

   public void setID(int ID){   //mutator method to set ID

      if (ID >= 0 && ID <= 9999)  // checks ID should not be negative or more than 9999

      this.ID = ID;  

     else this.ID = 0;  }  //if ID is negative or greater than 9999 then set ID to 0

   public void setModel(String model){  // mutator method to set model

       this.model = model;      }  

   public void setYear(int year){// mutator method to set year

      if (year >= 2000 && year <= 2017) //checks that year should not be earlier than 2000 or later than 2017

         this.ID = ID;

       else this.year = 0;     } //if year is less than 2000 or greater than 2017 then set year to 0

   public void setVinNo(String vinNo){  //mutator method to set vin number

       this.vinNo = vinNo;      }

   public void setMilesPerGalon(int milesPerGallon){  //mutator method to set miles per gallon

      if (milesPerGallon >= 10 && year <= 60)  //checks that miles per gallon should not be less than 10 or more than 60

          this.milesPerGallon = milesPerGallon;

      else this.milesPerGallon = 0;      } //if it is more than 60 or less than 10 then set miles per gallon to 0

   public void setSpeed(int speed){ //mutator method to set speed

       this.speed = speed;      }

   public void setMake(String make){  //mutator method to set make

       this.make = make;    }

   public void setColor(String color){  //mutator method to set color

       this.color = color;    }

   public int getID() //accessor method to get or access ID

   {return ID;} //returns the current ID

   public String getModel() //accessor method to get or access model

   {return model;} //returns the current model

   public int getYear()// accessor method to get or access year

   {return year;} //returns the current year

   public String getVinNo() // accessor method to get or access vin number

   {return vinNo;} //returns the current vin number

   public int getMilesPerGallon() //accessor method to get or access miles per gallon

   {return milesPerGallon;} //returns the current miles per gallon

   public int getSpeed() //accessor method to get or access speed

   {return speed;} //returns the current speed

   public String getMake() //accessor method to get or access make

   {return make;} //returns the current make

   public String getColor() //accessor method to get or access color

   {return color;}    //returns the current color

  public int Accelerate() {  //method that increase speed by 5

      setSpeed(speed + 5); //calls set speed to add 5 to the speed field

      return speed;    } //returns the speed after increasing 5

  public int Brake() { //method that decreases speed by 5

      setSpeed(speed - 5);//calls set speed to subtract 5 from the speed field

      return speed;    } //returns the speed after decreasing 5

  public int Accelerate(int spd) {  //overloading methods for Accelerate()that accepts a single parameter spd

      setSpeed(speed + spd);  //increases speed up to specified value of spd

      return speed;    }

  public int Brake(int spd) {  //overloading methods for Brake()that accepts a single parameter spd

      setSpeed(speed - spd); //decreases speed up to specified value of spd

      return speed;    }

   public Automobile(int ID, String make, String model, int year, String vinNo, int milesPerGallon, int speed, String color){  //constructor  that accepts arguments for each field value and uses the set methods to assign the values

      setID(ID);  

      setModel(model);  

      setYear(year);  

      setVinNo(vinNo);

      setMilesPerGalon(milesPerGallon);  

      setSpeed(speed);  

      setMake(make);  

      setColor(color);      } }

Explanation:

Here is TestAutomobiles.java

public class TestAutomobiles {   //class name

  public static void main(String args[]) {  //start of main method

    Automobile auto = new Automobile(123, "ABC", "XYZ", 2010, "MMM", 30, 150, "White");   //creates an object of Automobile class and calls its constructor passing the values for each of the fields

 System.out.println("Initial Speed: " + auto.getSpeed());  //calls getSpeed method to get the current speed using object auto. The current speed is 150

 System.out.println("Speed after applying acceleration: " + auto.Accelerate());   //calls Accelerate() method to increase the current speed to 5

 System.out.println("Speed after applying brake: " + auto.Brake());  //calls Brake() method to decrease the current speed to 5

   System.out.println("Speed after applying acceleration: " + auto.Accelerate(180));  //calls overloading Accelerate() method to increase the current speed to 180

     System.out.println("Speed after applying brake: " + auto.Brake(50));     }  } //calls overloading Brake() method to decrease the current speed to 50

//The screenshot of the output of the program is attached.

5 0
3 years ago
Technician A says that to cover all possible vehicle conditions, coolant must have a high freezing point. Technician B says cool
tiny-mole [99]
It would be option A!
please mark me brainliest ,
8 0
3 years ago
Read 2 more answers
Which format is best for the image below?
fredd [130]
I’m pretty sure it’s PNG
3 0
3 years ago
True or false
Semmy [17]
It’s false I hope this helped you
3 0
3 years ago
Explain benefits and disadvantages of using BNC over F-Type connectors on coaxial cables
Leokris [45]

Answer:

The benefits and disadvantages of using BNC over F-Type connectors on coaxial cables are;

The <em>benefits </em>of the BNC includes;

1) Easy connect and disconnect for a more rapid connection unlike the F-Type connector which is a screw connector

2) Reduction on stress on the termination of the coaxial cable

3) Appropriate in systems that see frequent reconfiguration unlike te F-Type connector that is permanently attached

4) Prevent unwanted disconnection ensuring image consistency by locking into place

5) Can extend to 300 ft.

The <em>disadvantages</em> of the BNC connectors are;

1) A separate power supply for the cable is required

2) Is used only for video transmission

3) Cannot be used for audio

4) The center wire is not easily visible like those of the F-Type connector which serves as a pin

5) The BNC connector is not watertight like the F-Type connectors

6) There are no provision for properly and securely attaching the cable to the camera for field work such that it can be easily disconnected while in use unlike the F-Type connectors

7) It is not as easy to manufacture like the F-Type connector and it is more expensive

8) The installation of the BNC connector on a coaxial cable is more intricate and the center cable is hidden from view after the BNC is attached thereby making it not possible to see if there is an error in installation which is unlike the F-type connector that takes less than 30 seconds to be attached to a cable

9) The F-Type connector provides more cost savings during installation than the BNC connector

Explanation:

The BNC connector is a type of bayonet connector used more frequently on CCTV systems. The benefits of the BNC includes;

The F-Type connectors are usable on SATV, CATV, and Digital TV where they are used together with RG6 or RG11 cables

3 0
3 years ago
Other questions:
  • What are personal skills? A manner of individual style How a person manages and expresses oneself One's ability to excel at spor
    13·2 answers
  • An individual is first with the network before they are authorized to access resources on the network A. countermeasure B. vulne
    11·1 answer
  • This operating system was used by individual computers an required users to type commands
    10·1 answer
  • A function may return a pointer, but the programmer must ensure that the pointer:
    14·1 answer
  • Josh wants to convey his best wishes to johnathin for a meeting schedualed later diring the day. Which business documented would
    7·1 answer
  • Testing for information would be most likely to occur in which type of engineering?
    5·2 answers
  • What do the following standards cover?
    7·1 answer
  • During his last performance review, Franco's boss urged him to set some short-term and long-term sales goals to help him perform
    6·2 answers
  • Define the terms of data and information .​
    6·1 answer
  • 9. Which of the following is considered an interface? (1 point)
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!