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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
250
CS253: Network Flows (2019)
jdchoi77
0
170
CS253: Shortest Path (2019)
jdchoi77
0
180
CS253: Topological Sort (2019)
jdchoi77
0
190
CS253: Minimum Spanning Trees (2019)
jdchoi77
0
240
CS253: Balanced Binary Search Trees (2019)
jdchoi77
0
300
CS253: Binary Search Trees (2019)
jdchoi77
0
170
CS253: Distribution-based Sort (2019)
jdchoi77
0
150
CS253: Divide & Conquer Sort (2019)
jdchoi77
0
190
Other Decks in Technology
See All in Technology
徹底討論!ECS vs EKS!
daitak
3
1.7k
データレイクの「見えない問題」を可視化する
sansantech
PRO
1
200
ぼっちではじめた登壇が「51名」「241件」の発信に化けた
subroh0508
1
310
Oracle Cloud Infrastructure:2026年6月度サービス・アップデート
oracle4engineer
PRO
0
320
作る力から、見極める力へ — AI時代に広がるエンジニアの価値と役割
rince
0
340
When Platform Engineering Meets GenAI
sucitw
0
170
Zenoh on Zephyr on LiteX
takasehideki
2
110
FPC(フレキシブル)基板にZephyr実装してみた。
iotengineer22
0
170
AI Agentをシステムに組み込む前にゆるく向き合ってみる
hayama17
0
140
事業会社における 機械学習・推薦システム技術の活用事例と必要な能力 / ml-recsys-in-layerx-wantedly-2026
yuya4
0
160
技術・能力を向上する原理原則 #きのこセッションa #きのこ2026
bash0c7
0
130
感情と身体を置き去りにしない、エンジニアの生きのこり方 ──いまから、ここから「自分の状態」を扱うという選択
saorimurooka
0
340
Featured
See All Featured
Everyday Curiosity
cassininazir
0
240
Making the Leap to Tech Lead
cromwellryan
135
9.9k
Mind Mapping
helmedeiros
PRO
1
260
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
Rails Girls Zürich Keynote
gr2m
96
14k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
270
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
170
The Spectacular Lies of Maps
axbom
PRO
1
820
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
1.8k
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