-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
207 lines (200 loc) · 5.97 KB
/
Main.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
198
199
200
201
202
203
204
205
206
207
import java.io.*;
import java.io.BufferedReader;
import java.util.*;
import java.io.FileReader;
class Node {
String name;
Node lft;
Node rgt;
String cellphone;
Node(String name, String cellphone) {
this(name, cellphone, null, null);
}
Node(String name, String cellphone, Node lft, Node rgt) {
this.cellphone = cellphone;
this.name = name;
this.lft = lft;
this.rgt = rgt;
}
}
class BinaryTree {
Node raiz;
BinaryTree() {
raiz = null;
}
Node getRaiz() {
return raiz;
}
//Insert in Binary Tree
void insert(Node x) {
raiz = insert(x, raiz);
}
public static Node insert(Node newNode, Node parent) {
if (parent == null) {
parent = newNode;
} else if ((newNode.name).compareTo(parent.name) < 0) {
parent.lft = insert((newNode), parent.lft);
} else if ((newNode.name).compareTo(parent.name) > 0) {
parent.rgt = insert((newNode), parent.rgt);
} else {
System.out.println("Erro, already exists!");
}
return parent;
}
//Remove from Binary Tree
void remotion(String x, Node raiz) throws Exception {
raiz = remove(x, raiz);
}
Node remove(String x, Node parent) throws Exception {
if (parent == null) {
throw new Exception("Erro!");
} else if((x).compareTo(parent.name) < 0) {
parent.lft = remove(x, parent.lft);
} else if((x).compareTo(parent.name) > 0) {
parent.rgt = remove(x, parent.rgt);
} else if(parent.rgt == null) {
parent = parent.lft;
} else if(parent.lft == null) {
parent = parent.rgt;
} else {
parent.lft = biggertLft(parent, parent.lft);
}
return parent;
}
Node biggertLft(Node parent, Node j) {
if (j.rgt == null) {
parent.name=j.name; j=j.lft;
} else {
j.rgt = biggertLft(parent, j.rgt);
}
return j;
}
//Search in Binary Tree
void search(String name, Node parent) {
if (parent == null) {
System.out.println("->Not Found");
} else if (name.compareTo(parent.name) == 0) {
System.out.println("->Found: " + parent.cellphone);
} else if (name.compareTo(parent.name) < 0) {
search(name, parent.lft);
} else if (name.compareTo(parent.name) > 0) {
search(name, parent.rgt);
} else {
System.out.println("->Contact Not Found");
}
}
//Tree Traversals
void PreOrderTraversal(Node parent) {
if (parent!= null) {
System.out.println(parent.name + " " + parent.cellphone);
PreOrderTraversal(parent.lft);
PreOrderTraversal(parent.rgt);
}
}
void PostOrderTraversal(Node parent) {
if (parent!= null) {
PostOrderTraversal(parent.lft);
PostOrderTraversal(parent.rgt);
System.out.println(parent.name + " " + parent.cellphone);
}
}
void InOrderTraversal(Node parent) {
if (parent != null) {
InOrderTraversal(parent.lft);
System.out.println(parent.name + " " + parent.cellphone);
InOrderTraversal(parent.rgt);
}
}
//Cleaning list file
void ListClear(Node parent) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("list.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("list.txt"));
String line = "";
line = reader.readLine();
while (line != null) {
bw.flush();
line = reader.readLine();
}
bw.close();
reader.close();
}
//Update content in list file
void ListUpdater(Node parent) throws Exception {
FileWriter fw = new FileWriter("list.txt", true);
if (parent != null) {
ListUpdater(parent.lft);
fw.write(parent.name + " # " + parent.cellphone + "\n");
ListUpdater(parent.rgt);
}
fw.close();
}
}
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
String line = "";
String straux = "";
String straux2 = "";
Scanner scan1 = new Scanner(System.in);
Scanner scan2 = new Scanner(System.in);
Scanner scan3 = new Scanner(System.in);
Scanner scan4 = new Scanner(System.in);
int var = 3;
BinaryTree tree = new BinaryTree();
BufferedReader reader = new BufferedReader(new FileReader("list.txt"));
String strings[] = new String[2];
line = reader.readLine();
while (line != null) {
strings = line.split(" # ");
tree.insert(new Node(strings[0], strings[1]));
line = reader.readLine();
}
do {
System.out.print("\nDigital Phone Book \n1-Search\n2-InOrderTraversal\n3-PreOrderTraversal\n4-PostOrderTraversal\n5-Insert\n6-Remove\n7-Finish\n\nType an Option:");
var = scan.nextInt();
switch (var) {
case 1:
System.out.print("Type a name to be searched:");
String var2 = scan1.nextLine();
tree.search(var2, tree.getRaiz());
break;
case 2:
System.out.println("\n->InOrder Traversal:");
tree.InOrderTraversal(tree.getRaiz());
break;
case 3:
System.out.println("\n->PreOrder Traversal:");
tree.PreOrderTraversal(tree.getRaiz());
break;
case 4:
System.out.println("\n->PostOrder Traversal:");
tree.PostOrderTraversal(tree.getRaiz());
break;
case 5:
System.out.print("Type a name to be inserted on the list:");
straux = scan2.nextLine();
System.out.print("Type the phone number:");
straux2 = scan3.nextLine();
tree.insert(new Node(straux, straux2));
tree.ListClear(tree.getRaiz());
tree.ListUpdater(tree.getRaiz());
break;
case 6:
System.out.print("Type a name to be romoved from list:");
straux = scan4.nextLine();
tree.remotion(straux, tree.getRaiz());
tree.ListClear(tree.getRaiz());
tree.ListUpdater(tree.getRaiz());
break;
default:
}
} while (var >= 1 && var <=6);
scan.close();
scan1.close();
scan2.close();
scan3.close();
scan4.close();
reader.close();
System.out.println("\nExecution Ended");
}
}