-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.java
More file actions
149 lines (133 loc) · 4.22 KB
/
Trie.java
File metadata and controls
149 lines (133 loc) · 4.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
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
import java.util.*;
public class Trie {
public Node starter;
public Trie() {
starter = new Node(' ', 0, null);
}
// public void test(String fullString, int f)
// {
// Node current = starter;
// for(int i = 0; i < fullString.length(); i ++)
// {
// if(i == fullString.length())
// }
// }
public void insertMain(String fullString, long fre) {
// if(fullString.equals("the"))
// {
// System.out.println("the inserted");
// }
insert(fullString, 0, fre, starter);
}
public void insert(String fullString, int index, long fre, Node current2) {
char x = fullString.charAt(index);
if (index == fullString.length() - 1) {
if (current2.pointers == null || current2.get(x) == null) {
Node temp = new Node(x, fre, fullString);
current2.set(x, temp);
}
else
{
Node temp = current2.get(x);
temp.word = fullString;
temp.wF = fre;
}
} else {
if (current2.contains(x) == true) {
insert(fullString, index + 1, fre, current2.get(x));
} else {
Node y = new Node(x, 0, null);
current2.set(x, y);
insert(fullString, index + 1, fre, current2.get(x));
current2.set(x, y);
}
}
}
//Finds the node of the inputted string
//Returns null if it's not in the trie
public Node find(String fullString, int index, Node current) {
char x = fullString.charAt(index);
if (fullString.length() == 1) {
if (current.contains(x) == true) {
return current.get(x);
} else {
return null;
}
}
if (index != fullString.length() - 1) {
if (current.get(x) == null) {
return null;
} else {
return find(fullString, index + 1, current.get(x));
}
} else {
return current.get(x);
}
}
//Finds the starting point then calls findAll and returns priority queue
public PriorityQueue<Node> findStarter(String input) {
Node current = starter;
NodeComp comp = new NodeComp();
PriorityQueue<Node> blank = new PriorityQueue<Node>(comp);
for (int i = 0; i < input.length(); i++) {
char x = input.charAt(i);
if (current.contains(x) == true) {
current = current.get(x);
} else {
return null;
}
}
findAll(current, blank);
return blank;
}
public void findAll(Node current, PriorityQueue<Node> pq) {
if (current.word != null) {
pq.add(current);
}
Set<Map.Entry<Character, Node>> x = current.pointers.entrySet();
for(Map.Entry<Character, Node> t : x)
{
Node l = t.getValue();
findAll(l, pq);
}
}
public void sortAndPrint(PriorityQueue<Node> pq)
{
//Put top 5 (or less) into an ArrayList
ArrayList<Node> list = new ArrayList<Node>();
for(int i = 0; i < 5; i ++)
{
if(pq == null)
{
break;
}
else if(pq.size() == 0)
{
break;
}
else
{
list.add(pq.remove());
}
}
//Selection Sort
for(int i = 0; i < list.size(); i ++)
{
for(int j = i + 1; j < list.size(); j++)
{
if(list.get(i).word.compareTo(list.get(j).word) > 0)
{
Node temp = list.get(j);
Node temp2 = list.get(i);
list.set(i, temp);
list.set(j, temp2);
}
}
}
//print
for(int i = 0; i < list.size(); i ++)
{
System.out.println(list.get(i).word);
}
}
}