Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ * Binary Heap of Objects. * The Heap is represented as an ArrayList of Objects * The next element to be un - heaped

/* Binary Heap of Objects.
* The Heap is represented as an ArrayList of Objects
* The next element to be un-heaped is element 0 of the ArrayList
* The children of an element at position i are at positions 2*i+1 and 2*i+2
*
*/
import java.util.ArrayList;
public class HeapObjects2>{
int size =0; // current # of elements in the heap
ArrayList a; // arraylist to hold heap elements
public HeapObjects2(){
size =0;
a = new ArrayList();
}
// method to add to the heap
public void add(E e){
//YOUR CODE
}
/** Remove from the heap and rebalance. Removed element is returned */
public E remove() throws Exception {
//YOUR CODE
}
public String toString(){
String s ="";
for (int i =0; i a.size(); i++){
s =(s + a.get(i)+"");
}
return s;
}
public static void main(String[] args) throws Exception {
HeapObjects2 heap = new HeapObjects2>();
heap.add(10);
heap.add(17);
heap.add(14);
heap.add(21);
heap.add(20);
heap.add(13);
heap.add(19);
heap.add(18);
heap.add(11);
heap.add(8);
heap.add(15);
heap.add(9);
HeapObjects2 heap2= new HeapObjects2>();
heap2.add("Lorem");
heap2.add("ipsum");
heap2.add("dolor");
heap2.add("consetetur");
heap2.add("iriure");
heap2.add("sadipscing");
heap2.add("aliquam");
heap2.add("eleifend");
// heap.add(5);
//System.out.println("current integer heap is->"+ heap);
System.out.println("current words heap is->"+ heap2);
System.out.println("Removing objects one by one from the heap");
heap.remove();
System.out.println("heap is->"+ heap);
heap2.remove();
System.out.println("heap is->"+ heap2);
heap2.remove();
System.out.println("heap is->"+ heap2);
heap2.remove();
System.out.println("heap is->"+ heap2);
heap2.remove();
System.out.println("heap is->"+ heap2);
heap2.remove();
System.out.println("heap is->"+ heap2);
heap2.remove();
System.out.println("heap is->"+ heap2);
heap2.remove();
System.out.println("heap is->"+ heap2);
}
}In the given file, HeapObjects_Programming.java, we try to program a binary heap of Objects.
Write the missing add and remove methods. Here is the output when you run the program with correct
code:
linux6: /CS3/Week6$ java Heap0bjects2
current words heap is-> Lorem aliquam ipsum consetetur iriure sadipscing dolor eleifend
Removing objects one by one from the heap
heap is->91310181411,2119201517
heap is-> aliquam consetetur ipsum eleifend iriure sadipscing dolor
heap is-> consetetur dolor ipsum eleifend iriure sadipscing
heap is -> dolor eleifend ipsum sadipscing iriure
heap is-> eleifend iriure ipsum sadipscing
heap is-> ipsum iriure sadipscing
heap is-> iriure sadipscing
heap is -> sadipscing
linux6: /CS3/Week6$
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions