Question: In JAVA I am using Data Structure and algorithms in Java Second Edition (chapter 12) Program Must Compile and run Please WRITE UNDER ONE FILE!
In JAVA
I am using Data Structure and algorithms in Java Second Edition (chapter 12)
Program Must Compile and run Please WRITE UNDER ONE FILE!
Write a program that implements the tree heap (the tree-based implementation of the heap) discussed in the text. Make sure you can remove the largest item, insert items, and change an items key.
MUST COMPILE AND RUN ,PLEASE DISPLAY OUTPUT, PLEASE Write under one File
Please include main method below with program also sample output below please include screen shots.
public static void main(String[] args) throws IOException
{
int value;
Heap theHeap = new Heap();
// insert 24 items
theHeap.insert(60);
theHeap.insert(200);
theHeap.insert(40);
theHeap.insert(210);
theHeap.insert(42);
theHeap.insert(240);
theHeap.insert(10);
theHeap.insert(44);
theHeap.insert(46);
theHeap.insert(20);
theHeap.insert(65);
theHeap.insert(30);
theHeap.insert(90);
theHeap.insert(93);
theHeap.insert(96);
theHeap.insert(99);
theHeap.insert(80);
theHeap.insert(83);
theHeap.insert(86);
theHeap.insert(89);
theHeap.insert(70);
theHeap.insert(73);
theHeap.insert(76);
theHeap.insert(79);
theHeap.displayTree();
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.displayTree();
break;
case 'i': // insert
System.out.print("Enter value to insert: ");
value = getInt();
theHeap.insert(value);
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 node number (1 to maxSize): ");
value = getInt();
System.out.print("Enter new key: ");
int newKey = getInt();
boolean success = theHeap.change(value, newKey);
if( !success )
System.out.println("Invalid index");
break;
default:
System.out.println("Invalid entry");
} // end switch
} // end while
} // end main()
The output may look like:
......................................................
240
200 210
99 89 90 96
80 86 70 76 79 40 10 93
44 60 46 83 20 65 42 73 30 -- -- -- -- -- -- --
......................................................
Enter first letter of show, insert, remove, change: i
Enter value to insert: 25
Enter first letter of show, insert, remove, change: s
......................................................
240
200 210
99 89 90 96
80 86 70 76 79 40 10 93
44 60 46 83 20 65 42 73 30 25 -- -- -- -- -- --
......................................................
Enter first letter of show, insert, remove, change: i
Enter value to insert: 95
Enter first letter of show, insert, remove, change: s
......................................................
240
200 210
99 89 95 96
80 86 70 76 79 90 10 93
44 60 46 83 20 65 42 73 30 25 40 -- -- -- -- --
......................................................
Enter first letter of show, insert, remove, change: i
Enter value to insert: 250
Enter first letter of show, insert, remove, change: s
......................................................
250
200 240
99 89 210 96
80 86 70 76 79 95 10 93
44 60 46 83 20 65 42 73 30 25 40 90 -- -- -- --
......................................................
Enter first letter of show, insert, remove, change: r
Enter first letter of show, insert, remove, change: s
......................................................
240
200 210
99 89 95 96
80 86 70 76 79 90 10 93
44 60 46 83 20 65 42 73 30 25 40 -- -- -- -- --
......................................................
Enter first letter of show, insert, remove, change:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
