Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. package folder;
  2.  
  3. import java.util.Scanner;
  4.  
  5. class BinaryTree {
  6.  
  7. static int con = 0;
  8. static int coun = 0;
  9. static String back = null;
  10.  
  11. public static void main(String[] args) {
  12. Scanner in = new Scanner(System.in);
  13. BinaryTree tree = new BinaryTree();
  14. while (in.hasNext()) {
  15. String acao = in.next();
  16. if (acao.equals("P")) {
  17. tree.printPreorder();
  18. System.out.print('\n');
  19. con = 0;
  20. } else if (acao.equals("R")) {
  21. tree.deletaritem(in.nextInt());
  22. System.out.println(back);
  23. back = null;
  24. } else if (acao.equals("B")) {
  25. String retorno = tree.buscar(in.nextInt());
  26. System.out.println(retorno);
  27. } else if (acao.equals("L")) {
  28. tree.contagem();
  29. System.out.println(coun);
  30. coun = 0;
  31. } else {
  32. int nr = Integer.parseInt(acao);
  33. in.nextLine();
  34. String a = in.nextLine();
  35. tree.inserir(nr, a);
  36. }
  37. }
  38. }
  39.  
  40. void printPreorder() {
  41. Preordem(raiz);
  42. }
  43.  
  44. public int contagem() {
  45. altura(raiz);
  46. return coun;
  47. }
  48.  
  49. class No {
  50. int key;
  51. String extenso;
  52. No left, right;
  53.  
  54. public No(int item, String extenso) {
  55. key = item;
  56. this.extenso = extenso;
  57. left = right = null;
  58. }
  59. }
  60.  
  61. No raiz;
  62. String extenso;
  63.  
  64. BinaryTree() {
  65. raiz = null;
  66. extenso = null;
  67. }
  68.  
  69. void Preordem(No no) {
  70. if (no != null) {
  71.  
  72. if (con > 0) {
  73. System.out.print("->");
  74. }
  75. System.out.print(no.key + ":'" + no.extenso + "'");
  76. con = 1;
  77. Preordem(no.left);
  78. Preordem(no.right);
  79. }
  80. }
  81.  
  82. void altura(No no) {
  83. if (no == null) {
  84. return;
  85. }
  86. coun++;
  87. altura(no.left);
  88. altura(no.right);
  89.  
  90. }
  91.  
  92. void inserir(int key, String extenso) {
  93. raiz = inserirTree(raiz, key, extenso);
  94. }
  95.  
  96. No inserirTree(No raiz, int key, String extenso) {
  97. if (raiz == null) {
  98. raiz = new No(key, extenso);
  99. return raiz;
  100. }
  101. if (key < raiz.key) {
  102. raiz.left = inserirTree(raiz.left, key, extenso);
  103. } else if (key > raiz.key) {
  104. raiz.right = inserirTree(raiz.right, key, extenso);
  105. }
  106. return raiz;
  107. }
  108.  
  109. public String deletaritem(int key) {
  110.  
  111. raiz = deletartree(raiz, key);
  112. return raiz.extenso;
  113.  
  114. }
  115.  
  116. No deletartree(No root, int key) {
  117.  
  118. if (root == null) {
  119. return raiz;
  120. } else if (key < root.key) {
  121. root.left = deletartree(root.left, key);
  122.  
  123. } else if (key > root.key) {
  124. root.right = deletartree(root.right, key);
  125.  
  126. } else {
  127. if (root.left == null) {
  128. back = root.extenso;
  129. return root.right;
  130. } else if (root.right == null) {
  131. back = root.extenso;
  132. return root.left;
  133. }
  134. root.key = minValue(root.right);
  135. root.right = deletartree(root.right, root.key);
  136. }
  137.  
  138. return raiz;
  139. }
  140.  
  141. int minValue(No raiz) {
  142. int minv = raiz.key;
  143. while (raiz.left != null) {
  144. minv = raiz.left.key;
  145. raiz = raiz.left;
  146. }
  147. return minv;
  148. }
  149.  
  150. public String buscar(int key) {
  151. String resul = busca(raiz, key);
  152. return resul;
  153. }
  154.  
  155. public String busca(No root, int key) {
  156. if (root == null || root.key == key) {
  157. return root.extenso;
  158. } else if (root.key > key) {
  159. return busca(root.left, key);
  160. } else {
  161. return busca(root.right, key);
  162. }
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement