Question: ?IN JAVA :( BOOK: DATA STRUCTURE AND ALGORITHMS 2ND EDITION) QUESTION: 12.1. Convert the heap.java program (Listing 12.1) so the heap is an ascending, rather

?IN JAVA :( BOOK: DATA STRUCTURE AND ALGORITHMS 2ND EDITION)

QUESTION:

12.1. Convert the heap.java program (Listing 12.1) so the heap is an ascending, rather than a descending, heap. (That is, the node at the root is the smallest rather than the largest.) Make sure all operations work correctly.

?PLEASE MAKE SURE THE MAIN METHOD IS SAME AS GIVEN BELOW:

public static void main(String[] args) throws IOException

{

int value, value2;

Heap theHeap = new Heap(31); // make a Heap; max size 31

boolean success;

theHeap.insert(70); // insert 10 items

theHeap.insert(40);

theHeap.insert(50);

theHeap.insert(20);

theHeap.insert(60);

theHeap.insert(100);

theHeap.insert(80);

theHeap.insert(30);

theHeap.insert(10);

theHeap.insert(90);

while(true) // until [Ctrl]-[C]

{

System.out.print("Enter first letter of ");

System.out.print("show, insert, remove, change: ");

int choice = getChar();

switch(choice)

{

case 's': // show

theHeap.displayHeap();

break;

case 'i': // insert

System.out.print("Enter value to insert: ");

value = getInt();

success = theHeap.insert(value);

if( !success )

System.out.println("Can't insert; heap full");

break;

case 'r': // remove

if( !theHeap.isEmpty() )

theHeap.remove();

else

System.out.println("Can't remove; heap empty");

break;

case 'c': // change

System.out.print("Enter index of item: ");

value = getInt();

System.out.print("Enter new priority: ");

value2 = getInt();

success = theHeap.change(value, value2);

if( !success )

System.out.println("Invalid index");

break;

default:

System.out.println("Invalid entry ");

} // end switch

} // end while

} // end main()

PLEASE MAKE SURE THE OUTPUT IS SAME AS GIVEN BELOW:

OUTPUT:

Enter first letter of show, insert, remove, change: s

heapArray: 10 20 50 30 60 100 80 70 40 90

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

10

20 50

30 60 100 80

70 40 90

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

Enter first letter of show, insert, remove, change: i

Enter value to insert: 5

Enter first letter of show, insert, remove, change: s

heapArray: 5 10 50 30 20 100 80 70 40 90 60

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

5

10 50

30 20 100 80

70 40 90 60

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

Enter first letter of show, insert, remove, change: r

Enter first letter of show, insert, remove, change: s

heapArray: 10 20 50 30 60 100 80 70 40 90

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

10

20 50

30 60 100 80

70 40 90

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

Enter first letter of show, insert, remove, change: c

Enter index of item: 5

Enter new priority: 45

Enter first letter of show, insert, remove, change: s

heapArray: 10 20 45 30 60 50 80 70 40 90

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

10

20 45

30 60 50 80

70 40 90

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

Enter first letter of show, insert, remove, change:

THANKS.....................

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!