Advertisement
eulaliaaires

Lista1 BST

Jun 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. static class Node {
  5.  
  6. private Node left;
  7. private Node right;
  8. private Value valor;
  9.  
  10. public Node(Value valor) {
  11. this.valor = valor;
  12. this.right = null;
  13. this.left = null;
  14. }
  15.  
  16. public Node() {
  17. this.valor = null;
  18. this.right = null;
  19. this.left = null;
  20. }
  21.  
  22. public Node insert(Node node, Value valor) {
  23. if (this.valor == null) {
  24. Node arvore = new Node(valor);
  25. return arvore;
  26. } else if (valor.getValue2() < node.valor.getValue2()) {
  27. if (node.left != null) {
  28. insert(node.left, valor);
  29. } else {
  30. node.left = new Node(valor);
  31. }
  32.  
  33. } else if (valor.getValue2() > node.valor.getValue2()) {
  34. if (node.right != null) {
  35. insert(node.right, valor);
  36. } else {
  37. node.right = new Node(valor);
  38. }
  39. }
  40. return node;
  41. }
  42.  
  43. public void posOrder(Node node) {
  44. if (node == null) {
  45. return;
  46. }
  47. posOrder(node.left);
  48. posOrder(node.right);
  49. System.out.println(node.valor.getValue2());
  50.  
  51. }
  52.  
  53. }
  54.  
  55. static class Value {
  56. private int valor;
  57.  
  58. public Value() {
  59.  
  60. }
  61.  
  62. public Value(int valor) {
  63. this.valor = valor;
  64. }
  65.  
  66. public int getValue2() {
  67. return this.valor;
  68. }
  69.  
  70. public void setValue(int valor) {
  71. this.valor = valor;
  72.  
  73. }
  74. }
  75.  
  76. public static void main(String[] args) {
  77. // TODO Auto-generated method stub
  78. Scanner in = new Scanner(System.in);
  79. Node root = new Node();
  80. while (in.hasNextInt()) {
  81.  
  82. int entradas = in.nextInt();
  83. Value d = new Value(entradas);
  84.  
  85. root = root.insert(root, d);
  86.  
  87. }
  88. root.posOrder(root);
  89.  
  90. }
  91.  
  92. }
  93. /*lista 1 BST*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement