-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslide24.java
More file actions
89 lines (70 loc) · 2.22 KB
/
slide24.java
File metadata and controls
89 lines (70 loc) · 2.22 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
public class SearchDict_charArray {
static final int SIZE = 26;
static class TrieNode
{
TrieNode[] Child = new TrieNode[SIZE];
boolean leaf;
public TrieNode() {
leaf = false;
for (int i =0 ; i< SIZE ; i++)
Child[i] = null;
}
}
static void insert(TrieNode root, String Key)
{
int n = Key.length();
TrieNode pChild = root;
for (int i=0; i<n; i++)
{
int index = Key.charAt(i) - 'a';
if (pChild.Child[index] == null)
pChild.Child[index] = new TrieNode();
pChild = pChild.Child[index];
}
pChild.leaf = true;
}
static void searchWord(TrieNode root, boolean Hash[],
String str)
{
if (root.leaf == true)
System.out.println(str);
for (int K =0; K < SIZE; K++)
{
if (Hash[K] == true && root.Child[K] != null )
{
char c = (char) (K + 'a');
searchWord(root.Child[K], Hash, str + c);
}
}
}
static void PrintAllWords(char Arr[], TrieNode root,
int n)
{
boolean[] Hash = new boolean[SIZE];
for (int i = 0 ; i < n; i++)
Hash[Arr[i] - 'a'] = true;
TrieNode pChild = root ;
String str = "";
for (int i = 0 ; i < SIZE ; i++)
{
if (Hash[i] == true && pChild.Child[i] != null )
{
str = str+(char)(i + 'a');
searchWord(pChild.Child[i], Hash, str);
str = "";
}
}
}
public static void main(String args[])
{
String Dict[] = {"go", "bat", "me", "eat",
"goal", "boy", "run"} ;
TrieNode root = new TrieNode();
int n = Dict.length;
for (int i=0; i<n; i++)
insert(root, Dict[i]);
char arr[] = {'e', 'o', 'b', 'a', 'm', 'g', 'l'} ;
int N = arr.length;
PrintAllWords(arr, root, N);
}
}