-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.java
More file actions
103 lines (86 loc) · 2.28 KB
/
Trie.java
File metadata and controls
103 lines (86 loc) · 2.28 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
package com.company;
import java.util.ArrayList;
import java.util.List;
public class Trie
{
private TrieNode root;
/**
* Constructor
*/
public Trie()
{
root = new TrieNode();
}
/**
* Adds a words to the Trie
* @param words
*/
public void addWord(List<String> words)
{
root.addWord(words);
}
/**
* check is contains url
* @param words
*/
public boolean containsUrl(List<String> words){
TrieNode lastNode = root;
for (int i = 0; i < words.size(); i ++)
{
lastNode = lastNode.getNode(words.get(i));
//If no node matches, then no words exist, return empty list
if (lastNode == null) {
return false;
}
}
return lastNode.isUrl();
}
/**
* Get the words in the Trie with the given
* prefix of list
* @param prefixWords
* @return a List containing String objects containing the words in
* the Trie with the given prefix.
*/
public List getWords(List<String> prefixWords)
{
//Find the node which represents the last letter of the prefix
TrieNode lastNode = root;
for (int i = 0; i < prefixWords.size(); i ++)
{
lastNode = lastNode.getNode(prefixWords.get(i));
//If no node matches, then no words exist, return empty list
if (lastNode == null) return new ArrayList();
}
//Return the words which eminate from the last node
return lastNode.getWords();
}
/**
* return the number of character nodes
* @return
*/
public int getCharacterNodes() {
return root.getCharacterNodes();
}
/**
* return the number of word nodes
* @return
*/
public int getWordNodes() {
return root.getWordNodes();
}
/**
* will return the number of leaf nodes in trie
* @return
*/
public int getLeafs(){
return root.getLeafs();
}
/**
* this void will get the list of all trienode words
* @return
*/
public List<String> getNodeWords(){
return root.getNodeWords();
}
}