Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- static class Node {
- private Node left;
- private Node right;
- private Value valor;
- public Node(Value valor) {
- this.valor = valor;
- this.right = null;
- this.left = null;
- }
- public Node() {
- this.valor = null;
- this.right = null;
- this.left = null;
- }
- public Node insert(Node node, Value valor) {
- if (this.valor == null) {
- Node arvore = new Node(valor);
- return arvore;
- } else if (valor.getValue2() < node.valor.getValue2()) {
- if (node.left != null) {
- insert(node.left, valor);
- } else {
- node.left = new Node(valor);
- }
- } else if (valor.getValue2() > node.valor.getValue2()) {
- if (node.right != null) {
- insert(node.right, valor);
- } else {
- node.right = new Node(valor);
- }
- }
- return node;
- }
- public void posOrder(Node node) {
- if (node == null) {
- return;
- }
- posOrder(node.left);
- posOrder(node.right);
- System.out.println(node.valor.getValue2());
- }
- }
- static class Value {
- private int valor;
- public Value() {
- }
- public Value(int valor) {
- this.valor = valor;
- }
- public int getValue2() {
- return this.valor;
- }
- public void setValue(int valor) {
- this.valor = valor;
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner in = new Scanner(System.in);
- Node root = new Node();
- while (in.hasNextInt()) {
- int entradas = in.nextInt();
- Value d = new Value(entradas);
- root = root.insert(root, d);
- }
- root.posOrder(root);
- }
- }
- /*lista 1 BST*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement