Advertisement
Guest User

heap

a guest
Dec 9th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. package dataStructures;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.BufferedReader;
  6. import java.util.Scanner;
  7. public class BinaryHeap {
  8. public static void main(String[] args) throws IOException {
  9. int value, value2;
  10. Heap theHeap = new Heap(31);
  11. boolean success;
  12.  
  13. while (true) {
  14. Scanner in = new Scanner(System.in);
  15.  
  16. System.out.println("1-Show Heap");
  17. System.out.println("2-Add element to Heap");
  18. System.out.println("3-Remove max element of Heap");
  19. System.out.println("4-Change index of Heap to inserted element");
  20. System.out.println("5-Exit");
  21. int choice = in.nextInt();
  22. switch (choice) {
  23. case 1: // show
  24. theHeap.displayHeap();
  25. break;
  26. case 2: // insert
  27. System.out.print("Enter value to insert: ");
  28. value = in.nextInt();
  29. success = theHeap.insert(value);
  30. if (!success)
  31. System.out.println("Can't insert; heap full");
  32. break;
  33. case 3: // remove
  34. if (!theHeap.isEmpty())
  35. theHeap.remove();
  36. else
  37. System.out.println("Can't remove; heap empty");
  38. break;
  39. case '4': // change
  40. System.out.print("Enter current index of item: ");
  41. value = in.nextInt();
  42. System.out.print("Enter new key: ");
  43. value2 = in.nextInt();
  44. success = theHeap.change(value, value2);
  45. if (!success)
  46. System.out.println("Invalid index");
  47. break;
  48. case '5': // exit
  49. System.out.print("Exited!");
  50. System.exit(1);
  51. break;
  52. default:
  53. System.out.println("Wrong entry. Try again!\n");
  54. }
  55. }
  56. }
  57.  
  58. public static String getString() throws IOException {
  59. InputStreamReader isr = new InputStreamReader(System.in);
  60. BufferedReader br = new BufferedReader(isr);
  61. String s = br.readLine();
  62. return s;
  63. }
  64.  
  65. public static char getChar() throws IOException {
  66. String s = getString();
  67. return s.charAt(0);
  68. }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement