Answered step by step
Verified Expert Solution
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 unheaped is element of the ArrayList
The children of an element at position i are at positions i and i
import java.util.ArrayList;
public class HeapObjects
int size ; current # of elements in the heap
ArrayList a; arraylist to hold heap elements
public HeapObjects
size ;
a new ArrayList;
method to add to the heap
public void addE 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 ; i asize; i
s s ageti;
return s;
public static void mainString args throws Exception
HeapObjects heap new HeapObjects;
heap.add;
heap.add;
heap.add;
heap.add;
heap.add;
heap.add;
heap.add;
heap.add;
heap.add;
heap.add;
heap.add;
heap.add;
HeapObjects heap new HeapObjects;
heapaddLorem;
heapaddipsum;
heapadddolor;
heapaddconsetetur;
heapaddiriure;
heapaddsadipscing;
heapaddaliquam;
heapaddeleifend;
heap.add;
Systemout.printlncurrent integer heap is heap;
System.out.printlncurrent words heap is heap;
System.out.printlnRemoving objects one by one from the heap";
heap.remove;
System.out.printlnheap is heap;
heapremove;
System.out.printlnheap is heap;
heapremove;
System.out.printlnheap is heap;
heapremove;
System.out.printlnheap is heap;
heapremove;
System.out.printlnheap is heap;
heapremove;
System.out.printlnheap is heap;
heapremove;
System.out.printlnheap is heap;
heapremove;
System.out.printlnheap is heap;
In the given file, HeapObjectsProgramming.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:
linux: CSWeek$ java Heapbjects
current words heap is Lorem aliquam ipsum consetetur iriure sadipscing dolor eleifend
Removing objects one by one from the heap
heap is
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
linux: CSWeek$
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started