Advertisement
ArthurOliverB

Arvore com metodo de inserir

Apr 17th, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package Arvore;
  2.  
  3. public class Arvore {
  4. private No raiz;
  5. private No folha;
  6. private int quantidade;
  7.  
  8. public Arvore() {
  9. this.raiz = null;
  10. this.folha = folha;
  11. this.quantidade = 0;
  12. }
  13.  
  14. public int getQuantidade() {
  15. return quantidade;
  16. }
  17.  
  18. public void setQuantidade(int quantidade) {
  19. this.quantidade = quantidade;
  20. }
  21.  
  22. public boolean haveFilha() {
  23.  
  24. return raiz.getDireita() != null || raiz.getEsquerda() != null;
  25. }
  26.  
  27. public boolean IsEmpty() {
  28. return raiz == null;
  29. }
  30.  
  31. public boolean isEmptyE() {
  32. return raiz.getEsquerda() == null;
  33.  
  34. }
  35.  
  36. public boolean isEmptyD() {
  37. return raiz.getDireita() == null;
  38. }
  39.  
  40. public void inserir(int elemento) {
  41. No novo = new No(elemento);
  42. if (IsEmpty()) {
  43. this.raiz = novo;
  44. }
  45. if (!haveFilha()) {
  46. if (novo.getElemento() < raiz.getElemento()) {
  47.  
  48. raiz.setEsquerda(novo);
  49.  
  50. } else {
  51. raiz.setDireita(novo);
  52. }
  53. if (haveFilha()) {
  54. if (novo.getElemento() > raiz.getElemento()) {
  55. if (!isEmptyD()) {
  56. if (raiz.getDireita().getElemento() < novo
  57. .getElemento()) {
  58. raiz.getDireita().setDireita(novo);
  59. } else {
  60. raiz.getDireita().setEsquerda(novo);
  61. }
  62. if (raiz.getDireita().getElemento() > novo
  63. .getElemento()) {
  64. raiz.getDireita().setEsquerda(novo);
  65.  
  66. }
  67. if (novo.getElemento() < raiz.getElemento()) {
  68. if (!isEmptyE()) {
  69. if (novo.getElemento() > raiz.getEsquerda()
  70. .getElemento()) {
  71. raiz.getEsquerda().setDireita(novo);
  72. } else if (novo.getElemento() < raiz
  73. .getEsquerda().getElemento()) {
  74.  
  75. raiz.getEsquerda().setEsquerda(novo);
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. quantidade++;}
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement