Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CS253: Tries (2019)

CS253: Tries (2019)

Jinho D. Choi

October 07, 2019
Tweet

More Decks by Jinho D. Choi

Other Decks in Technology

Transcript

  1. public class TrieNode<T> { private Map<Character, TrieNode<T>> children; private TrieNode<T>

    parent; private boolean end_state; private char key; private T value; public TrieNode(TrieNode<T> parent, char key) { children = new HashMap<Character, TrieNode<T>>(); setEndState(false); setParent(parent); setKey(key); setValue(null); } <T> value end_state
  2. TrieNode.java public TrieNode<T> getChild(char key) { return children.get(key); } public

    TrieNode<T> addChild(char key) { TrieNode<T> child = getChild(key); if (child == null) { child = new TrieNode<T>(this, key); children.put(key, child); } return child; } public TrieNode<T> removeChild(char key) { return children.remove(key); } get() add() remove()
  3. Trie.java public class Trie<T> { private TrieNode<T> root; public Trie()

    { root = new TrieNode<>(null, (char) 0); } public T get(String key) { TrieNode<T> node = find(key); return (node != null && node.isEndState()) ? node.getValue() : null; } public TrieNode<T> find(String key) { char[] array = key.toCharArray(); TrieNode<T> node = root; for (int i = 0; i < key.length(); i++) { node = node.getChild(array[i]); if (node == null) return null; } return node; } (char)0 find()
  4. e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s

    ∅ 0 h ∅ l ∅ l ∅ 1 e ∅ e ∅ 2 o ∅ r ∅ e ∅
  5. e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s

    ∅ 0 h ∅ l ∅ l ∅ 1 e ∅ e ∅ 2 o ∅ r ∅ e ∅ 3
  6. public T put(String key, T value) { char[] array =

    key.toCharArray(); TrieNode<T> node = root; for (int i = 0; i < key.length(); i++) node = node.addChild(array[i]); node.setEndState(true); return node.setValue(value); }
  7. Trie - Remove 3 e 0 ∅ ∅ s ∅

    h ∅ l ∅ l 1 e ∅ e 2 o ∅ r ∅ e ∅ 3
  8. Trie - Remove 3 e 0 ∅ ∅ s ∅

    h ∅ l ∅ l 1 e ∅ e 2 o ∅ r ∅ e ∅ 3 “see”
  9. Trie - Remove 3 e 0 ∅ ∅ s ∅

    h ∅ l ∅ l 1 e ∅ e 2 o ∅ r ∅ e ∅ 3 “see”
  10. Trie - Remove 3 e 0 ∅ ∅ s ∅

    h ∅ l ∅ l 1 e ∅ o ∅ r ∅ e ∅ 3 “see”
  11. Trie - Remove 3 e 0 ∅ ∅ s ∅

    h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see”
  12. Trie - Remove 3 e 0 ∅ ∅ s ∅

    h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see” ”she”
  13. Trie - Remove 3 e 0 ∅ ∅ s ∅

    h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see” ”she”
  14. Trie - Remove 3 e 0 ∅ ∅ s ∅

    ∅ h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see” ”she”
  15. Trie - Remove 3 e 0 ∅ ∅ s ∅

    ∅ h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see” ”she” ”shell”
  16. Trie - Remove 3 e 0 ∅ ∅ s ∅

    h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see” ”she” ”shell”
  17. Trie - Remove 3 e 0 ∅ ∅ s ∅

    h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see” ”she” ”shell”
  18. Trie - Remove 3 e 0 ∅ ∅ s ∅

    h ∅ l ∅ o ∅ r ∅ e ∅ 3 “see” ”she” ”shell”
  19. Trie - Remove 3 e 0 ∅ ∅ s ∅

    h ∅ o ∅ r ∅ e ∅ 3 “see” ”she” ”shell”
  20. public boolean remove(String key) { TrieNode<T> node = find(key); if

    (node == null || !node.isEndState()) // node doesn't exist return false; if (node.hasChildren()) { // node to be removed as children node.setEndState(false); return true; } TrieNode<T> parent = node.getParent(); while (parent != null) { // remove iteratively parent.removeChild(node.getKey()); if (parent.hasChildren() || parent.isEndState()) // another word break; else { node = parent; parent = parent.getParent(); } } return true; }