-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remover nós da árvore.java
197 lines (180 loc) · 6.41 KB
/
Remover nós da á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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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 Node{
private String value;
private Node left;
private Node right;
public Node(String value){
this.value = value;
}
public String getValue(){
return this.value;
}
public void setValue(String value){
this.value = value;
}
public Node getLeft(){
return this.left;
}
public Node getRight(){
return this.right;
}
public void SetLeft(Node left){
this.left = left;
}
public void SetRight(Node right){
this.right = right;
}
}
class BinaryTree {
private Node initialNode;
public void add(String elem) {
if(initialNode==null){
initialNode = new Node(elem);
}else{
Node temp = initialNode;
while(temp!=null){
if(elem.compareToIgnoreCase(temp.getValue())>0){
if(temp.getRight()==null){
temp.SetRight(new Node(elem));
break;
}else{
temp = temp.getRight();
}
}else if(elem.compareToIgnoreCase(temp.getValue())<=0){
if(temp.getLeft()==null){
temp.SetLeft(new Node(elem));
break;
}else{
temp = temp.getLeft();
}
}
}
}
}
public void rem(String elem){
Node temp = initialNode;
Node paiTemp = temp;
while(temp!=null){
if(elem.compareToIgnoreCase(temp.getValue())==0){
break;
}else if(elem.compareToIgnoreCase(temp.getValue())>0){
paiTemp = temp;
temp = temp.getRight();
}else if(elem.compareToIgnoreCase(temp.getValue())<0){
paiTemp = temp;
temp = temp.getLeft();
}
}
if(temp!=null){
if(paiTemp==temp){
if(temp.getLeft()==null && temp.getRight()!=null){
initialNode = temp.getRight();
}else if(temp.getRight()==null && temp.getLeft()!=null){
initialNode = temp.getLeft();
}else if(temp.getRight()==null && temp.getLeft()==null){
initialNode = null;
}else{
Node aux = temp.getLeft();
Node paiAux = temp;
while(aux.getRight()!=null){
paiAux = aux;
aux = aux.getRight();
}
temp.setValue(aux.getValue());
if(paiAux!=temp){
paiAux.SetRight(aux.getLeft());
}else{
paiAux.SetLeft(aux.getLeft());
}
}
}else{
if(temp.getLeft()==null && temp.getRight()!=null){
if(paiTemp.getRight()==temp){
paiTemp.SetRight(temp.getRight());
}else if(paiTemp.getLeft()==temp){
paiTemp.SetLeft(temp.getRight());
}
}else if(temp.getRight()==null && temp.getLeft()!=null){
if(paiTemp.getRight()==temp){
paiTemp.SetRight(temp.getLeft());
}else if(paiTemp.getLeft()==temp){
paiTemp.SetLeft(temp.getLeft());
}
}else if(temp.getRight()==null && temp.getLeft()==null){
if(paiTemp.getRight()==temp){
paiTemp.SetRight(null);
}else if(paiTemp.getLeft()==temp){
paiTemp.SetLeft(null);
}
}else{
Node aux = temp.getLeft();
Node paiAux = temp;
while(aux.getRight()!=null){
paiAux = aux;
aux = aux.getRight();
}
temp.setValue(aux.getValue());
if(paiAux!=temp){
paiAux.SetRight(aux.getLeft());
}else{
paiAux.SetLeft(aux.getLeft());
}
}
}
}
}
private String treeSearch(Node actual) {
if (actual == null){
return "(null)";
}else{
if(actual.getLeft() == null && actual.getRight() == null){
return actual.getValue();
}else{
return String.format("l(%s) - %s - r(%s)", treeSearch(actual.getLeft()), actual.getValue(), treeSearch(actual.getRight()));
}
}
}
public String toString() {
return treeSearch(initialNode);
}
}
class Result {
private final static BinaryTree tree = new BinaryTree();
public static void createTree(String values){
String[] valuesToAdd = values.split(" ");
for(String value : valuesToAdd){
tree.add(value);
}
}
public static String removeNodes(String removeValues){
String[] valuesToRem = removeValues.split(" ");
for(String value : valuesToRem){
tree.rem(value);
}
return tree.toString();
}
}
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 addValues = bufferedReader.readLine();
String removeValues = bufferedReader.readLine();
Result.createTree(addValues);
String result = Result.removeNodes(removeValues);
bufferedWriter.write(result);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}