-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathMain.java
135 lines (118 loc) · 3.9 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
package ru.arhiser.prefix_tree;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] params) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(".\\src\\ru\\arhiser\\prefix_tree\\numbers.txt"));
TreeNode root = new TreeNode(' ');
for (String line: lines) {
root.insert(line);
}
//System.out.println(root.containString("18АА0603"));
writeTreeToFile(".\\src\\ru\\arhiser\\prefix_tree\\tree.dat", root);
TreeNode fromFile = readFromFile(".\\src\\ru\\arhiser\\prefix_tree\\tree.dat");
List<String> extractedFromTree = new ArrayList<>();
fromFile.getAllNumbers("", extractedFromTree);
}
static class TreeNode {
char value;
List<TreeNode> children;
public TreeNode(char value) {
this.value = value;
}
public void insert(String data) {
if (data.length() == 0) {
return;
}
if (children == null) {
children = new ArrayList<>();
}
char c = data.charAt(0);
TreeNode child = findNodeByChar(c);
if (child == null) {
child = new TreeNode(c);
children.add(child);
}
child.insert(data.substring(1));
}
private TreeNode findNodeByChar(char c) {
if (children != null) {
for(TreeNode node: children) {
if (node.value == c) {
return node;
}
}
}
return null;
}
private boolean containString(String str) {
TreeNode current = this;
for (int i = 0; i < str.length(); i++) {
current = current.findNodeByChar(str.charAt(i));
if (current == null) {
return false;
}
}
return true;
}
public void getAllNumbers(String path, List<String> result) {
if (value != ' ') {
path = path + value;
}
if (children != null) {
for(TreeNode node: children) {
node.getAllNumbers(path, result);
}
} else {
result.add(path);
}
}
public void writeToFile(PrintWriter writer) {
writer.write(value);
if (children != null) {
for (TreeNode node: children) {
node.writeToFile(writer);
}
}
writer.write(']');
}
public void readFromFile(FileReader reader) throws IOException {
char ch;
while ((ch = (char)reader.read()) != ']') {
TreeNode treeNode = new TreeNode(ch);
treeNode.readFromFile(reader);
if (children == null) {
children = new ArrayList<>();
}
children.add(treeNode);
}
}
}
public static void writeTreeToFile(String path, TreeNode root) {
try {
PrintWriter out = new PrintWriter(path);
root.writeToFile(out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static TreeNode readFromFile(String path) {
TreeNode root = new TreeNode(' ');
try {
FileReader reader = new FileReader(path);
reader.read();
root.readFromFile(reader);
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return root;
}
}