-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parentesco entre árvores.java
143 lines (127 loc) · 3.92 KB
/
Parentesco entre árvores.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
135
136
137
138
139
140
141
142
143
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 No {
private String info;
private No left;
private No right;
public No(String info){
this.info = info;
}
public String getInfo(){
return info;
}
public No getLeft(){
return this.left;
}
public No getRight(){
return this.right;
}
public void setLeft(No left){
this.left = left;
}
public void setRight(No right){
this.right = right;
}
}
class ArvoreBinaria {
private No a;
public void add(String e) {
if(a ==null){
a = new No(e);
}else{
No temp = a;
while(temp!=null){
if(e.compareTo(temp.getInfo())>0){
if(temp.getRight()==null){
temp.setRight(new No(e));
break;
}else{
temp = temp.getRight();
}
}else if(e.compareTo(temp.getInfo())<0){
if(temp.getLeft()==null){
temp.setLeft(new No(e));
break;
}else{
temp = temp.getLeft();
}
}else{
break;
}
}
}
}
public int relacao(String nodeA, String nodeB){
int contador = 0;
No temp = a;
while(temp!=null){
if(nodeA.compareTo(temp.getInfo())>0){
temp = temp.getRight();
}else if(nodeA.compareTo(temp.getInfo())<0){
temp = temp.getLeft();
}else{
break;
}
}
if(temp !=null){
while(temp!=null){
if(nodeB.compareTo(temp.getInfo())>0){
temp = temp.getRight();
contador++;
}else if(nodeB.compareTo(temp.getInfo())<0){
temp = temp.getLeft();
contador++;
}else{
break;
}
}
if(temp==null){
contador = 0;
}
}
return contador;
}
}
class Result {
public static String nodeGenealogy(String values, String nodeA, String nodeB) {
ArvoreBinaria arvore = new ArvoreBinaria();
String[] value = values.trim().split(" ");
for(int i=0; i<value.length;i++){
arvore.add(value[i]);
}
String r = "";
int relacao = (arvore.relacao(nodeA, nodeB)>arvore.relacao(nodeB, nodeA))?arvore.relacao(nodeA, nodeB):arvore.relacao(nodeB, nodeA);
if(relacao>0){
r = "Pai";
for(int j=1;j<relacao;j++){
r += " " + "do Pai";
}
}else{
r = "nao ha parentesco";
}
return r;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String treeNodes = bufferedReader.readLine();
String nodeA = bufferedReader.readLine();
String nodeB = bufferedReader.readLine();
String result = Result.nodeGenealogy(treeNodes, nodeA, nodeB);
bufferedWriter.write(result);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}