Last active
December 10, 2019 17:21
-
-
Save t9toqwerty/510d14c215a05ad6208f157dadab60e2 to your computer and use it in GitHub Desktop.
Trie Insertion, Search & Deletion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app; | |
import java.util.ArrayList; | |
import java.util.Stack; | |
/** | |
* Trie | |
*/ | |
public class Trie { | |
private TrieNode root; | |
public Trie() { | |
root = new TrieNode(); | |
} | |
public void insert(char[] word) { | |
TrieNode traversalPointer = root; | |
for (int i = 0; i < word.length; i++) { | |
if (traversalPointer.links[this.getIndex(word[i])] == null) { | |
traversalPointer.links[this.getIndex(word[i])] = new TrieNode(); | |
} | |
traversalPointer = traversalPointer.links[this.getIndex(word[i])]; | |
} | |
traversalPointer.isCompleteWord = true; | |
} | |
public boolean delete(char[] word) { | |
TrieNode traversalPointer = root; | |
Stack<TrieNode> s = new Stack<TrieNode>(); | |
for (int i = 0; i < word.length; i++) { | |
if (traversalPointer == null) { | |
return false; | |
} | |
traversalPointer = traversalPointer.links[this.getIndex(word[i])]; | |
if (traversalPointer == null) { | |
return false; | |
} else { | |
s.push(traversalPointer); | |
} | |
} | |
if (traversalPointer == null) { | |
return false; | |
} | |
if (traversalPointer.isCompleteWord) { | |
TrieNode currentNode = s.pop(); | |
currentNode.isCompleteWord = false; | |
currentNode = null; | |
while (!s.isEmpty()) { | |
TrieNode tempNode = s.pop(); | |
if (tempNode.canBeDeleted()) { | |
tempNode = null; | |
} | |
} | |
return true; | |
} | |
return false; | |
} | |
/** | |
* TODO: Fix This | |
*/ | |
public ArrayList<String> printAllWords() { | |
return this.root.words(new ArrayList<Character>()); | |
} | |
public boolean containsWord(char[] word) { | |
TrieNode traversalPointer = root; | |
for (int i = 0; i < word.length; i++) { | |
if (traversalPointer == null) { | |
return false; | |
} | |
traversalPointer = traversalPointer.links[this.getIndex(word[i])]; | |
} | |
if (traversalPointer == null) { | |
return false; | |
} | |
if (traversalPointer.isCompleteWord) { | |
return true; | |
} | |
return false; | |
} | |
private int getIndex(char character) { | |
return ((int) character) - 97; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app; | |
import java.util.ArrayList; | |
/** | |
* TrieNode | |
*/ | |
public class TrieNode { | |
public boolean isCompleteWord; | |
public TrieNode[] links; | |
public static int LINK_COUNT = 26; | |
public TrieNode() { | |
this.isCompleteWord = false; | |
links = new TrieNode[LINK_COUNT]; | |
for (int i = 0; i < links.length; i++) { | |
links[i] = null; | |
} | |
} | |
public boolean canBeDeleted() { | |
boolean canBeDeleted = true; | |
for (int i = 0; i < links.length; i++) { | |
canBeDeleted = (canBeDeleted && (this.links[i] != null)); | |
} | |
return canBeDeleted && (!this.isCompleteWord); | |
} | |
/** | |
* TODO: Fix This | |
* | |
* @param wordList | |
* @return | |
*/ | |
public ArrayList<String> words(ArrayList<Character> wordList) { | |
ArrayList<String> words = new ArrayList<String>(); | |
for (int i = 0; i < this.links.length; i++) { | |
if (links[i] != null) { | |
wordList.add((char) (i + 97)); | |
links[i].words(wordList); | |
} | |
} | |
words.add(wordList.toString()); | |
return words; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment