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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
感情と身体を置き去りにしない、エンジニアの生きのこり方 ──いまから、ここから「自分の状態」を扱うという選択
saorimurooka
0
340
サイバーエージェントにおけるAI推進戦略と変革への取り組み
shotatsuge
0
550
AIAU_UMEMOGU_ninomiya_slide
ninomiya_ii
0
260
Comment regagner la souveraineté de vos données tout en étant payé grâce à Nostr !
rlifchitz
0
200
AWS Security Hub CSPMの成功・失敗体験
cmusudakeisuke
0
550
フルAIで個人開発して学んだあれこれ / yuruai vol.1
isaoshimizu
0
120
元・セキュリティ学習経験0大学生による業務紹介 / An Introduction to the Job by a Former College Student with Zero Security Training Experience
nttcom
0
160
フィジカル版Github Onshapeの紹介
shiba_8ro
0
320
WebGIS AI Agentの紹介
_shimizu
0
560
現場のトークンマネジメント
dak2
1
190
From Prompt Engineering to Loop Engineering
shibuiwilliam
1
220
40代で“やっとエンジニアになれた”――閉じた学びを開き、空の青さを知る / 20260628 Naoki Takahashi
shift_evolve
PRO
4
870
Featured
See All Featured
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
610
Joys of Absence: A Defence of Solitary Play
codingconduct
1
400
KATA
mclloyd
PRO
35
15k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1.1k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
200
Code Reviewing Like a Champion
maltzj
528
40k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2k
AI: The stuff that nobody shows you
jnunemaker
PRO
8
730
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
340
Visualization
eitanlees
152
17k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.6k
How to train your dragon (web standard)
notwaldorf
97
6.7k
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