ผมจะลองทำ search engine โดยใช้ red black tree อ่าครับ แต่ติดอยุ่ตอนนี้ แนะนำทีครับ
Code (Java)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
public static String[] A = new String[150000];
public static String[] B = new String[150000];
public static String[] C = new String[150000];
public static String[] D = new String[150000];
public static String[] E = new String[150000];
public static String[] F = new String[150000];
public static String[] G = new String[150000];
public static String[] H = new String[150000];
public static String[] I = new String[150000];
public static String[] J = new String[150000];
public static String[] K = new String[150000];
public static String[] L = new String[150000];
public static String[] M = new String[150000];
public static String[] N = new String[150000];
public static String[] O = new String[150000];
public static String[] P = new String[150000];
public static String[] Q = new String[150000];
public static String[] R = new String[150000];
public static String[] S = new String[150000];
public static String[] T = new String[150000];
public static String[] U = new String[150000];
public static String[] V = new String[150000];
public static String[] W = new String[150000];
public static String[] X = new String[150000];
public static String[] Y = new String[150000];
public static String[] Z = new String[150000];
public static int ca = 0;
public static int cb = 0;
public static int cc = 0;
public static int cd = 0;
public static int ce = 0;
public static int cf = 0;
public static int cg = 0;
public static int ch = 0;
public static int ci = 0;
public static int cj = 0;
public static int ck = 0;
public static int cl = 0;
public static int cm = 0;
public static int cn = 0;
public static int co = 0;
public static int cp = 0;
public static int cq = 0;
public static int cr = 0;
public static int cs = 0;
public static int ct = 0;
public static int cu = 0;
public static int cv = 0;
public static int cw = 0;
public static int cx = 0;
public static int cy = 0;
public static int cz = 0;
public static void insert(String key){
if (key.length() > 1) {
String checkback = key.substring(key.length()-1,key.length());
switch (checkback) {
case ",":
key = key.substring(0,key.length()-1);
break;
case "?":
key = key.substring(0,key.length()-1);
break;
case ")":
key = key.substring(0,key.length()-1);
break;
case "(":
key = key.substring(0,key.length()-1);
break;
case ".":
key = key.substring(0,key.length()-1);
break;
case ":":
key = key.substring(0,key.length()-1);
break;
case "/":
key = key.substring(0,key.length()-1);
break;
case "'":
key = key.substring(0,key.length()-1);
break;
case ";":
key = key.substring(0,key.length()-1);
break;
}
String keyc = key.substring(0,1);
switch (keyc) {
case "A":
A[ca] = key;
ca++;
break;
case "a":
A[ca] = key;
ca++;
break;
case "B":
B[cb] = key;
cb++;
break;
case "b":
B[cb] = key;
cb++;
break;
case "C":
C[cc] = key;
cc++;
break;
case "c":
C[cc] = key;
cc++;
break;
case "D":
D[cd] = key;
cd++;
break;
case "d":
D[cd] = key;
cd++;
break;
case "E":
E[ce] = key;
ce++;
break;
case "e":
E[ce] = key;
ce++;
break;
case "F":
F[cf] = key;
cf++;
break;
case "f":
F[cf] = key;
cf++;
break;
case "G":
G[cg] = key;
cg++;
break;
case "g":
G[cg] = key;
cg++;
break;
case "H":
H[ch] = key;
ch++;
break;
case "h":
H[ch] = key;
ch++;
break;
case "I":
I[ci] = key;
ci++;
break;
case "i":
I[ci] = key;
ci++;
break;
case "J":
J[cj] = key;
cj++;
break;
case "j":
J[cj] = key;
cj++;
break;
case "K":
K[ck] = key;
ck++;
break;
case "k":
K[ck] = key;
ck++;
break;
case "L":
L[cl] = key;
cl++;
break;
case "l":
L[cl] = key;
cl++;
break;
case "M":
M[cm] = key;
cm++;
break;
case "m":
M[cm] = key;
cm++;
break;
case "N":
N[cn] = key;
cn++;
break;
case "n":
N[cn] = key;
cn++;
break;
case "O":
O[co] = key;
co++;
break;
case "o":
O[co] = key;
co++;
break;
case "P":
P[cp] = key;
cp++;
break;
case "p":
P[cp] = key;
cp++;
break;
case "Q":
Q[cq] = key;
cq++;
break;
case "q":
Q[cq] = key;
cq++;
break;
case "R":
R[cr] = key;
cr++;
break;
case "r":
R[cr] = key;
cr++;
break;
case "S":
S[cs] = key;
cs++;
break;
case "s":
S[cs] = key;
cs++;
break;
case "T":
T[ct] = key;
ct++;
break;
case "t":
T[ct] = key;
ct++;
break;
case "U":
U[cu] = key;
cu++;
break;
case "u":
U[cu] = key;
cu++;
break;
case "V":
V[cv] = key;
cv++;
break;
case "v":
V[cv] = key;
cv++;
break;
case "W":
W[cw] = key;
cw++;
break;
case "w":
W[cw] = key;
cw++;
break;
case "X":
X[cx] = key;
cx++;
break;
case "x":
X[cx] = key;
cx++;
break;
case "Y":
Y[cy] = key;
cy++;
break;
case "y":
Y[cy] = key;
cy++;
break;
case "Z":
Z[cz] = key;
cz++;
break;
case "z":
Z[cz] = key;
cz++;
break;
}
}
}
public static void main(String[] args) {
// A string array with duplicate values
String[] data = { "A", "C", "B", "D", "A", "B", "E", "D", "B", "C" };
System.out.println("Original array : " + Arrays.toString(data));
try {
FileReader fr = new FileReader("C:\\test.txt");
BufferedReader br = new BufferedReader(fr);
String[] a = {};
String line = br.readLine();
while (line != null) {
a = line.split(" ");
for (int i = 0; i < a.length; i++) {
// System.out.println(a[i]);
if (a[i] != null) {
insert(a[i]);
List<String> list = Arrays.asList(data);
Set<String> set = new HashSet<String>(list);
}
}
line = br.readLine();
}
} catch (IOException e) {
System.out.println("ERROR");
}
}
}