-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBSTNode.java
More file actions
159 lines (147 loc) · 5.35 KB
/
BSTNode.java
File metadata and controls
159 lines (147 loc) · 5.35 KB
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
package phone_directory;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import phone_directory.DirectoryModel;
import phone_directory.BST;
import phone_directory.PhoneDirectory;
class BSTNode {
String name;
String phNum;
String email;
BSTNode left;
BSTNode right;
public BSTNode() {};
public BSTNode(DirectoryModel dModel) {
this.name = dModel.name;
this.phNum = dModel.phNum;
this.email = dModel.email;
}
public void insert(DirectoryModel directoryModel) {
if (comp(directoryModel.name, this.name)) {
if (left == null) left = new BSTNode(directoryModel);
else left.insert(directoryModel);
} else if (directoryModel.name.equals(this.name)) {
if (directoryModel.phNum != this.phNum || directoryModel.email != this.email) {
if (Long.parseLong(directoryModel.phNum)< Long.parseLong(this.phNum)) {
left.insert(directoryModel);
} else {
right.insert(directoryModel);
}
}
} else {
if (right == null) right = new BSTNode(directoryModel);
else right.insert(directoryModel);
}
}
public boolean comp(String name1, String name2) {
if (name1.length()<name2.length()) {
for (int i = 0; i<name1.length();i++) {
if (name1.toLowerCase().charAt(0)<name2.toLowerCase().charAt(0)) {
return true;
}
}
} else {
for (int i = 0; i<name2.length();i++) {
if (name1.toLowerCase().charAt(i)<name2.toLowerCase().charAt(i)) {
return true;
} else return false;
}
}
return false;
}
public void insertTodb(DirectoryModel directoryModel) {
try {
File file = new File("data.txt");
FileWriter fr = new FileWriter(file, true);
fr.write(directoryModel.name + " " + directoryModel.phNum+ " "+ directoryModel.email+"\n");
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void inorder() {
if (left != null) left.inorder();
System.out.print("Name: "+name+" Phone number: "+ phNum+" Email: "+email + "\n");
if (right != null) right.inorder();
}
public void display() {
BST bst = new BST();
try {
File f = new File("data.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
System.out.println("Saved Contacts");
while ((readLine = b.readLine()) != null) {
String[] arrOfStr = readLine.split(" ", 0);
if (arrOfStr.length == 3) {
bst.insert( new DirectoryModel(arrOfStr[0], arrOfStr[1], arrOfStr[2]));
}
}
bst.inorder();
} catch (IOException e) {
e.printStackTrace();
}
}
public void search(String name) {
try {
ArrayList<DirectoryModel> directoryModels = new ArrayList<DirectoryModel>();
File f = new File("data.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
while ((readLine = b.readLine()) != null) {
String[] arrOfStr = readLine.split(" ", 0);
if (name.toLowerCase().equals(arrOfStr[0].toLowerCase())) {
directoryModels.add(new DirectoryModel(arrOfStr[0], arrOfStr[1], arrOfStr[2]));
}
}
if (directoryModels.isEmpty()) {
System.out.println("No such contacts found!");
} else {
for (DirectoryModel a:directoryModels) {
System.out.print("Name: "+a.name+" Phone number: "+ a.phNum+" Email: "+a.email + "\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public BSTNode returnBST() {
BST bst = new BST();
try {
File f = new File("data.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
while ((readLine = b.readLine()) != null) {
String[] arrOfStr = readLine.split(" ", 0);
if (arrOfStr.length == 3) {
bst.insert( new DirectoryModel(arrOfStr[0], arrOfStr[1], arrOfStr[2]));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return bst.root;
}
public void minInBST(BSTNode bstNode) {
if (bstNode.left == null) {
System.out.print("Name: "+bstNode.name+" Phone number: "+ bstNode.phNum+" Email: "+bstNode.email + "\n");
} else {
minInBST(bstNode.left);
}
}
public void maxInBST(BSTNode bstNode) {
if (bstNode.right == null) {
System.out.print("Name: "+bstNode.name+" Phone number: "+ bstNode.phNum+" Email: "+bstNode.email + "\n");
} else {
maxInBST(bstNode.right);
}
}
}