Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dataStructures;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.BufferedReader;
- import java.util.Scanner;
- public class BinaryHeap {
- public static void main(String[] args) throws IOException {
- int value, value2;
- Heap theHeap = new Heap(31);
- boolean success;
- while (true) {
- Scanner in = new Scanner(System.in);
- System.out.println("1-Show Heap");
- System.out.println("2-Add element to Heap");
- System.out.println("3-Remove max element of Heap");
- System.out.println("4-Change index of Heap to inserted element");
- System.out.println("5-Exit");
- int choice = in.nextInt();
- switch (choice) {
- case 1: // show
- theHeap.displayHeap();
- break;
- case 2: // insert
- System.out.print("Enter value to insert: ");
- value = in.nextInt();
- success = theHeap.insert(value);
- if (!success)
- System.out.println("Can't insert; heap full");
- break;
- case 3: // remove
- if (!theHeap.isEmpty())
- theHeap.remove();
- else
- System.out.println("Can't remove; heap empty");
- break;
- case '4': // change
- System.out.print("Enter current index of item: ");
- value = in.nextInt();
- System.out.print("Enter new key: ");
- value2 = in.nextInt();
- success = theHeap.change(value, value2);
- if (!success)
- System.out.println("Invalid index");
- break;
- case '5': // exit
- System.out.print("Exited!");
- System.exit(1);
- break;
- default:
- System.out.println("Wrong entry. Try again!\n");
- }
- }
- }
- public static String getString() throws IOException {
- InputStreamReader isr = new InputStreamReader(System.in);
- BufferedReader br = new BufferedReader(isr);
- String s = br.readLine();
- return s;
- }
- public static char getChar() throws IOException {
- String s = getString();
- return s.charAt(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement