Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
CS253: Tries (2019)
Search
Jinho D. Choi
October 07, 2019
Technology
290
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
CS253: Tries (2019)
https://github.com/emory-courses/cs253
Jinho D. Choi
October 07, 2019
More Decks by Jinho D. Choi
See All by Jinho D. Choi
CS253: Dynamic Programming (2019)
jdchoi77
0
260
CS253: Network Flows (2019)
jdchoi77
0
180
CS253: Shortest Path (2019)
jdchoi77
0
180
CS253: Topological Sort (2019)
jdchoi77
0
190
CS253: Minimum Spanning Trees (2019)
jdchoi77
0
250
CS253: Balanced Binary Search Trees (2019)
jdchoi77
0
300
CS253: Binary Search Trees (2019)
jdchoi77
0
170
CS253: Distribution-based Sort (2019)
jdchoi77
0
160
CS253: Divide & Conquer Sort (2019)
jdchoi77
0
200
Other Decks in Technology
See All in Technology
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1.2k
サーバー常駐型の 簡易障害調査AI エージェントを作ってみた話
masayoshi
0
230
500名弱規模の組織のPythonプロジェクト(dbt) をどう管理するか?
hiracky16
0
300
GopherCon @シアトル に行ってきました
logica0419
0
320
dbt in Microsoft Fabric
ryomaru0825
0
280
医療の現場を変革に挑戦した半年間の軌跡 - PythonとAIで現場を変える / From Code to Care
soudai
PRO
1
630
どんな手を使っても絶対間に合わせるスケジューラ
asari194617
0
1.5k
分割40%キーボードにスムーズに入門するには
hoto17296
1
140
APIセキュリティを組織で実現するには~注力する点と設計・実装に入れたい対策~
riiimparm
1
270
AIレビュー時代に必要なのは、SLOで引く撤退ライン
nobuoooo
0
140
Master Dataグループ紹介資料
sansan33
PRO
1
4.8k
Introduction to Sansan Meishi Maker Development Engineer
sansan33
PRO
0
460
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
220
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.8k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
240
Navigating Weather and Climate Data
rabernat
0
490
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
500
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
220
From π to Pie charts
rasagy
0
330
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
380
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
450
Transcript
Tries Data Structures and Algorithms Emory University Jinho D. Choi
https://github.com/emory-courses/cs253
None
{"she", "shell", "see", "shore", "selling", "sell"}
None
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
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()
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()
Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"}
Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅
Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅
Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅
Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s ∅
Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s ∅ h
∅
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ h ∅
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅ 1
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅ 1
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅ 1
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅ 1 e ∅
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅ 1 e ∅ e ∅
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅ 1 e ∅ e ∅ 2
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅ 1 e ∅ e ∅ 2
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅ 1 e ∅ e ∅ 2
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅ 1 e ∅ e ∅ 2 o ∅
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅ 1 e ∅ e ∅ 2 o ∅ r ∅
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅ 1 e ∅ e ∅ 2 o ∅ r ∅ e ∅
e ∅ Trie - Put 2 {“she”,”shell”,"see","shore","selling","sell"} ∅ ∅ s
∅ 0 h ∅ l ∅ l ∅ 1 e ∅ e ∅ 2 o ∅ r ∅ e ∅ 3
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); }
Trie - Remove 3 e 0 ∅ ∅ s ∅
h ∅ l ∅ l 1 e ∅ e 2 o ∅ r ∅ e ∅ 3
Trie - Remove 3 e 0 ∅ ∅ s ∅
h ∅ l ∅ l 1 e ∅ e 2 o ∅ r ∅ e ∅ 3 “see”
Trie - Remove 3 e 0 ∅ ∅ s ∅
h ∅ l ∅ l 1 e ∅ e 2 o ∅ r ∅ e ∅ 3 “see”
Trie - Remove 3 e 0 ∅ ∅ s ∅
h ∅ l ∅ l 1 e ∅ o ∅ r ∅ e ∅ 3 “see”
Trie - Remove 3 e 0 ∅ ∅ s ∅
h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see”
Trie - Remove 3 e 0 ∅ ∅ s ∅
h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see” ”she”
Trie - Remove 3 e 0 ∅ ∅ s ∅
h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see” ”she”
Trie - Remove 3 e 0 ∅ ∅ s ∅
∅ h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see” ”she”
Trie - Remove 3 e 0 ∅ ∅ s ∅
∅ h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see” ”she” ”shell”
Trie - Remove 3 e 0 ∅ ∅ s ∅
h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see” ”she” ”shell”
Trie - Remove 3 e 0 ∅ ∅ s ∅
h ∅ l ∅ l 1 o ∅ r ∅ e ∅ 3 “see” ”she” ”shell”
Trie - Remove 3 e 0 ∅ ∅ s ∅
h ∅ l ∅ o ∅ r ∅ e ∅ 3 “see” ”she” ”shell”
Trie - Remove 3 e 0 ∅ ∅ s ∅
h ∅ o ∅ r ∅ e ∅ 3 “see” ”she” ”shell”
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; }
None