Advertisement
MrRabetao

Untitled

Nov 14th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. package BT;
  2.  
  3. public class BinaryTree<Key extends Comparable<Key>, Value> {
  4.  
  5. private Celula root;
  6. private int n = 0, depth = 0, height = 0, count = 0;
  7.  
  8. public boolean isEmpty() {
  9. return root == null;
  10. }
  11.  
  12. public int size() {
  13. return size(root);
  14. }
  15.  
  16. private int size(Celula x) {
  17. if (x == null)
  18. return 0;
  19. return 1 + size(x.getEsquerda()) + size(x.getDireita());
  20. }
  21.  
  22. public Value get(Key key) {
  23. return get(root, key);
  24. }
  25.  
  26. private Value get(Celula x, Key key) {
  27. if (x == null)
  28. return null;
  29. int cmp = key.compareTo((Key) x.getKey());
  30. if (cmp < 0) {
  31. return get(x.getEsquerda(), key);
  32. } else {
  33. if (cmp > 0)
  34. return get(x.getDireita(), key);
  35. else
  36. return (Value) x.getVal();
  37. }
  38. }
  39.  
  40. public void put(Key key, Value valor) {
  41. depth = 0;
  42. root = put(root, key, valor);
  43. n++;
  44. }
  45.  
  46. private Celula put(Celula x, Key key, Value valor) {
  47. if (x == null) {
  48. Celula aux = new Celula(key, valor);
  49. aux.setDepth(depth);
  50.  
  51. return aux;
  52. }
  53. int cmp = key.compareTo((Key) x.getKey());
  54. if (cmp < 0) {
  55. depth++;
  56. x.setEsquerda(put(x.getEsquerda(), key, valor));
  57. } else {
  58. if (cmp > 0) {
  59. depth++;
  60. x.setDireita(put(x.getDireita(), key, valor));
  61. } else
  62. x.setVal(valor);
  63. }
  64. return x;
  65. }
  66.  
  67. public String toString() {
  68. String str = "";
  69. str += "Pre ordem: ";
  70. str += mostrarPre(root);
  71. str += "\n";
  72. str += "Pos ordem: ";
  73. str += mostrarPos(root);
  74. str += "\n";
  75. str += "In ordem: ";
  76. str += mostrarIn(root);
  77. return str;
  78. }
  79.  
  80. public int getN() {
  81. return n;
  82. }
  83.  
  84. private String mostrarPre(Celula x) {
  85. if (x == null)
  86. return "";
  87. return (x.getKey() + " - ") + mostrarPre(x.getEsquerda()) + mostrarPre(x.getDireita());
  88. }
  89.  
  90. private String mostrarPos(Celula x) {
  91. if (x == null)
  92. return "";
  93. return mostrarPos(x.getEsquerda()) + mostrarPos(x.getDireita()) + (x.getKey() + " - ");
  94. }
  95.  
  96. private String mostrarIn(Celula x) {
  97. if (x == null)
  98. return "";
  99. return mostrarIn(x.getEsquerda()) + (x.getKey() + " - ") + mostrarIn(x.getDireita());
  100. }
  101.  
  102. public int internalPathLenght() {
  103. return internalPathLenght(root);
  104. }
  105.  
  106. public int internalPathLenght(Celula x) {
  107. if (x == null)
  108. return 0;
  109. return (x.getDepth()) + internalPathLenght(x.getEsquerda()) + internalPathLenght(x.getDireita());
  110. }
  111.  
  112. public int height() {
  113. Celula aux = root;
  114. return height(root, aux);
  115. }
  116.  
  117. public int height(Celula x, Celula aux) {
  118. if (count == 0) {
  119. if (x == null) {
  120. return 0;
  121. }
  122. } else {
  123. if (count > 0) {
  124. if (x == null) {
  125. return aux.getDepth();
  126. }
  127. }
  128. }
  129. count++;
  130. aux = x;
  131. if (height(x.getEsquerda(), aux) > height)
  132. height = x.getDepth();
  133. if (height(x.getDireita(), aux) > height)
  134. height = x.getDepth();
  135. return height;
  136. }
  137.  
  138. }
  139.  
  140.  
  141.  
  142.  
  143. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. package BT;
  154.  
  155. public class Celula<Key, Value> {
  156. private int depth;
  157. private Key key;
  158. private Value val;
  159. private Celula esquerda, direita;
  160.  
  161. public Celula(Key key, Value val) {
  162. this.key = key;
  163. this.val = val;
  164. }
  165.  
  166. public Key getKey() {
  167. return key;
  168. }
  169.  
  170. public void setKey(Key key) {
  171. this.key = key;
  172. }
  173.  
  174. public Value getVal() {
  175. return val;
  176. }
  177.  
  178. public void setVal(Value val) {
  179. this.val = val;
  180. }
  181.  
  182. public Celula getEsquerda() {
  183. return esquerda;
  184. }
  185.  
  186. public void setEsquerda(Celula esquerda) {
  187. this.esquerda = esquerda;
  188. }
  189.  
  190. public Celula getDireita() {
  191. return direita;
  192. }
  193.  
  194. public void setDireita(Celula direita) {
  195. this.direita = direita;
  196. }
  197.  
  198. public int getDepth() {
  199. return depth;
  200. }
  201.  
  202. public void setDepth(int depth) {
  203. this.depth = depth;
  204. }
  205.  
  206.  
  207. }
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225. package BT;
  226.  
  227. import java.io.IOException;
  228. import java.nio.file.Files;
  229. import java.nio.file.Path;
  230. import java.nio.file.Paths;
  231.  
  232. import javax.swing.JOptionPane;
  233.  
  234. public class Cliente {
  235.  
  236. public static void main(String[] args) throws IOException {
  237. BinaryTree<String, Integer> tree = new BinaryTree<String, Integer>();
  238.  
  239. String key;
  240. String[] textS;
  241.  
  242. String caminhoS = JOptionPane.showInputDialog("Digite o diretório do tale.txt:\n"
  243. + "Exemplo: C:/Users/usuario/Desktop/tale.txt\n" + "Obs: Usar \"/\" ao inves de \"\\\"");
  244. Path caminho = Paths.get(caminhoS);
  245. String text = Files.readString(caminho);
  246. textS = text.split("\n");
  247.  
  248. for (int i = 0; i < textS.length; i++)
  249. textS[i] = textS[i].replaceAll("[^a-zA-Z_0-9]","");
  250.  
  251. for (int i = 0; i < textS.length; i++)
  252. tree.put(textS[i], tree.getN());
  253.  
  254. while (true) {
  255. int op = Integer.parseInt(JOptionPane.showInputDialog("1.Consultar\n2.Informacoes\n0.Sair"));
  256. switch (op) {
  257. case 1:
  258. key = JOptionPane.showInputDialog("Chave:");
  259. Integer aux = tree.get(key);
  260. if (aux != null)
  261. JOptionPane.showMessageDialog(null, aux);
  262. else
  263. JOptionPane.showMessageDialog(null, "Nao encontrado");
  264. break;
  265. case 2:
  266. JOptionPane.showMessageDialog(null, "Arvore:\n" + tree);
  267. JOptionPane.showMessageDialog(null, "Tamanho: " + tree.size());
  268. JOptionPane.showMessageDialog(null, "Altura: " + tree.height());
  269. JOptionPane.showMessageDialog(null, "Internal Path Lenght: " + tree.internalPathLenght());
  270. break;
  271. default:
  272. return;
  273. }
  274. }
  275. }
  276.  
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement