Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Heap Java

mles
mles 


Heap To do: You will create a Priority Queue using a Heap represented in an array. All was covered in lecture yesterday and one day before last week. Needed: You will need a Heap class that will keep the MAX values as the highest priority that will be removed from the heap first, and the smaller numbers with less priority. The Heap class will need an instance field array that holds ints, you can just make the array size 25, and we'll make sure we don't go over 25 elements. The heap class will need a method for inserting items in the heap. The Heap class will need a method for removing items from the heap. Both methods will need to handle rearranging the items in the heap. The Heap class will need a method for printing out the heap level by level.......until you get to a level that starts with a null. A Driver class that sends in the ints to be stored in the heap. Implementation, run these steps in order: Add the following items to your heap: 15, 5, 8, 4, 9, 22, 17, 6, 14 Print Heap Remove two items from the heap. Print Heap Add the following 18, 12 Print Heap Remove three items from the heap. Print Heap Attachments
Tags Clarifications

Answers

Heap Program

sudheer488
sudheer488 



Please find the solution.

All the classes viz.,, heap, node are present inside the HeapApp.java file
Attachments
HeapApp.java (8 K) Download Preview
------------------------------------------------------------- public void displayHeap() { System.out.print("heapArray: "); // array format for(int m=0; m<currentSize; m++) if(heapArray[m] != null) System.out.print( heapArray[m].getKey() + " "); else System.out.print( "-- "); System.out.println(); // heap format int nBlanks = 32; int itemsPerRow = 1;

Output.PNG (25 K) Download Preview

Screenshots
Screenshot
Purchase Answer

Priority Queue using a Heap

felo
felo 



Find attached together with solution Attachments
Heap.png (96 K) Download Preview

Heap..rar (7 K) Download






Preview
// ------------------------------------------------------------- public void trickleUp(int index) { int parent = (index-1) / 2; Node bottom = heapArray[index]; while( index > 0 && heapArray[parent].getKey() < bottom.getKey() ) { heapArray[index] = heapArray[parent]; // move it down index = parent; parent =

Preview
args) throws IOException { int value, value2; Heap theHeap = new Heap(31); // make a Heap; max size 31 boolean success; theHeap.insert(15); // insert 10 items theHeap.insert(5); theHeap.insert(8); theHeap.insert(4); theHeap.insert(9); theHeap.insert(22); theHeap.insert(17); theHeap.insert(16); theHeap.insert(14);

Preview
iData; // data item (key) // ------------------------------------------------------------- public Node(int key) // constructor { iData = key; } // -------------------------------------------------------------

Screenshots
Screenshot
Purchase Answer