MCPcopy Create free account
hub / github.com/Seogeurim/CS-study / Trie

Class Trie

contents/data-structure/code/Trie/TrieExample.java:24–51  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22}
23
24class Trie {
25 TrieNode root = new TrieNode();
26
27 void insert(String word) {
28 TrieNode current = root;
29 for (int i = 0; i < word.length(); i++) {
30 char c = word.charAt(i);
31 if (!current.hasChild(c)) {
32 current.children[c - 'a'] = new TrieNode();
33 }
34 current = current.getChild(c);
35 }
36 current.isEnd = true;
37 }
38
39 boolean checkWord(String word) {
40 TrieNode current = root;
41 for (int i = 0; i < word.length(); i++) {
42 char c = word.charAt(i);
43 if (current.hasChild(c)) {
44 current = current.getChild(c);
45 } else {
46 return false;
47 }
48 }
49 return current.isEnd;
50 }
51}
52
53class TrieNode {
54 TrieNode[] children = new TrieNode[26];

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected