Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main{
- public static void main(String[] args) {
- Scanner in = new Scanner (System.in);
- int[] nums = new int[10001];
- int cont = 0;
- int num = 0;
- Arvore BST = new Arvore();
- while(in.hasNext()){
- num = in.nextInt();
- cont++;
- }
- int x = 0;
- while(x < cont){
- if(x==0){
- num = nums[x];
- BST(num);
- } else {
- num = nums[x];
- BST.novaChave(num);
- }
- }
- posOrdem(BST);
- }
- public class Arvore {
- public int valor;
- public Arvore esq;
- public Arvore dir;
- public Arvore(int valor) {
- this.dado = valor;
- this.esq = null;
- this.dir = null;
- }
- public void novaChave(int valor) {
- if (valor < this.dado) { //É menor ->
- if (this.esq != null) { //Tá vazio ->
- this.esq.novaChave(valor); //Tá sim, então cria um novo nó
- } else { //Tá não, então cria uma subárvore nova
- this.esq = new Arvore(valor);
- }
- } else {
- if (this.dir != null) {
- this.dir.novaChave(valor);
- } else {
- this.dir = new Arvore(valor);
- }
- }
- }
- public void posOrdem(Arvore BST) {
- if (this.esq != null) {
- this.esq.posOrdem();
- }
- if (this.dir != null) {
- this.dir.posOrdem();
- }
- System.out.println(this.dado);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement