Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main{
  4. public static void main(String[] args) {
  5. Scanner in = new Scanner (System.in);
  6. int[] nums = new int[10001];
  7. int cont = 0;
  8. int num = 0;
  9. Arvore BST = new Arvore();
  10.  
  11. while(in.hasNext()){
  12. num = in.nextInt();
  13. cont++;
  14. }
  15.  
  16. int x = 0;
  17.  
  18. while(x < cont){
  19. if(x==0){
  20. num = nums[x];
  21. BST(num);
  22. } else {
  23. num = nums[x];
  24. BST.novaChave(num);
  25. }
  26. }
  27. posOrdem(BST);
  28. }
  29.  
  30. public class Arvore {
  31.  
  32. public int valor;
  33. public Arvore esq;
  34. public Arvore dir;
  35.  
  36. public Arvore(int valor) {
  37. this.dado = valor;
  38. this.esq = null;
  39. this.dir = null;
  40. }
  41.  
  42. public void novaChave(int valor) {
  43. if (valor < this.dado) { //É menor ->
  44. if (this.esq != null) { //Tá vazio ->
  45. this.esq.novaChave(valor); //Tá sim, então cria um novo nó
  46. } else { //Tá não, então cria uma subárvore nova
  47. this.esq = new Arvore(valor);
  48. }
  49.  
  50. } else {
  51. if (this.dir != null) {
  52. this.dir.novaChave(valor);
  53. } else {
  54. this.dir = new Arvore(valor);
  55. }
  56. }
  57. }
  58.  
  59. public void posOrdem(Arvore BST) {
  60. if (this.esq != null) {
  61. this.esq.posOrdem();
  62. }
  63. if (this.dir != null) {
  64. this.dir.posOrdem();
  65. }
  66. System.out.println(this.dado);
  67. }
  68.  
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement