-
Notifications
You must be signed in to change notification settings - Fork 0
/
Percursos em árvore.java
134 lines (122 loc) · 4.13 KB
/
Percursos em árvore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class BinaryNode {
private int value;
private BinaryNode left;
private BinaryNode right;
public BinaryNode(int value){
this.value = value;
}
public int getValue(){
return this.value;
}
public void setValue(int value){
this.value = value;
}
public BinaryNode getLeft(){
return this.left;
}
public BinaryNode getRight(){
return this.right;
}
public void setLeft(BinaryNode left){
this.left = left;
}
public void setRight(BinaryNode right){
this.right = right;
}
}
class BinaryTree {
private BinaryNode initialNode;
public void add(int elem) {
if(initialNode==null){
initialNode = new BinaryNode(elem);
}else{
BinaryNode temp = initialNode;
while(temp!=null){
if(elem>temp.getValue()){
if(temp.getRight()==null){
temp.setRight(new BinaryNode(elem));
break;
}else{
temp = temp.getRight();
}
}else if(elem<temp.getValue()){
if(temp.getLeft()==null){
temp.setLeft(new BinaryNode(elem));
break;
}else{
temp = temp.getLeft();
}
}else{
break;
}
}
}
}
private String posOrdem(BinaryNode a){
String r = "";
if(a.getLeft()!=null && a.getRight()==null){
r += posOrdem(a.getLeft());
}else if(a.getRight()!=null && a.getLeft()==null){
r += posOrdem(a.getRight());
}else if(a.getRight()!=null && a.getLeft()!=null){
r += posOrdem(a.getLeft()) + posOrdem(a.getRight());
}
return r + a.getValue() + " ";
}
private String preOrdem(BinaryNode a){
String r = a.getValue() + " ";
if(a.getLeft()!=null && a.getRight()==null){
r += preOrdem(a.getLeft());
}else if(a.getRight()!=null && a.getLeft()==null){
r += preOrdem(a.getRight());
}else if(a.getRight()!=null && a.getLeft()!=null){
r += preOrdem(a.getLeft()) + preOrdem(a.getRight());
}
return r;
}
private String emOrdem(BinaryNode a){
String r = "";
if(a.getLeft()!=null && a.getRight()==null){
r += emOrdem(a.getLeft()) + " " + a.getValue();
}else if(a.getRight()!=null && a.getLeft()==null){
r += a.getValue() + " " + emOrdem(a.getRight());
}else if(a.getRight()!=null && a.getLeft()!=null){
r += emOrdem(a.getLeft()) + " " + a.getValue() + " " + emOrdem(a.getRight());
}else{
r += a.getValue();
}
return r;
}
public void questao(){
System.out.println("Em ordem:\n" + emOrdem(initialNode));
System.out.println("Pre ordem:\n" + preOrdem(initialNode));
System.out.println("Pos ordem:\n" + posOrdem(initialNode));
}
}
public class Solution {
public static void addArvore(String entrada, BinaryTree a) {
String[] elementos = entrada.trim().split(",");
for(int i=0;i<elementos.length;i++){
a.add(Integer.valueOf(elementos[i]));
}
}
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String entrada = bufferedReader.readLine();
BinaryTree a = new BinaryTree();
addArvore(entrada,a);
a.questao();
bufferedReader.close();
}
}