1. The document describes a Trie data structure and its implementation in Java. A Trie is a tree used to store strings that allows for efficient retrieval of strings based on prefixes.
2. Nodes in the Trie contain a map of child nodes and a boolean to indicate if it is the end of a word. Words are inserted recursively by traversing the tree and adding child nodes.
3. Searching and deletion are also done recursively by traversing the tree based on the characters in the key and updating nodes as needed. Insertion, search, and deletion all have time complexity of O(key length).
2. What Is TRIE?
• Trie is an efficient information retrieval data structure also
called digital tree and sometimes radix tree or prefix tree (as
they can be searched by prefixes), is an ordered tree data
structure that is used to store A dynamic set or associative
array where the keys are usually strings.
3. Why Trie Data Structure?
• Searching trees in general favor keys which are of fixed size since
this leads to efficient storage management.
• However in case of applications which are retrieval based and
which call for keys varying length, tries provide better options.
• Tries are also called as Lexicographic Search trees.
• The name trie (pronounced as “try”)originated from the word
“retrieval”.
5. NODE STRUCTURE
private class TrieNode {
Map<Character, TrieNode> children;
boolean endOfWord;
public TrieNode() {
children = new HashMap<>();
endOfWord = false;
}
}
Initializing a map with key
& value named children
Indicates Whether This Node Completes A Word
9. Insert: cs
False
False
root
c
a
insert (“CS”)
public class Trie {
public Trie() { root = new TrieNode(); }
Private class TrieNode{
Map<Character, TrieNode> children;
boolean endOfWord;
public TrieNode() { children = new HashMap<>();
endOfWord = false;
}
}
public void insert (String word) {
insertRecursive(root, word, 0);
}
private void insertRecursive(TrieNode current, String word, int index) {
if (index == word.length())
{
current.endOfWord = true; return;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
if (node == null)
{ node = new TrieNode();
current.children.put(ch, node);
}
insertRecursive(node, word, index + 1);
Public static void main(String
[]args){
Trie tree = new Trie();
tree.insert(“CS”)
}
insertRecursiver (root,“CS”,0)
10. Insert: cs
False
False
root
C
a
public class Trie {
public Trie() { root = new TrieNode(); }
Private class TrieNode{
Map<Character, TrieNode> children;
boolean endOfWord;
public TrieNode() { children = new HashMap<>();
endOfWord = false;
}
}
public void insert (String word) {
insertRecursive(root, word, 0);
}
private void insertRecursive(TrieNode current, String word, int index) {
if (index == word.length())
{
current.endOfWord = true; return;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
if (node == null)
{ node = new TrieNode();
current.children.put(ch, node);
}
insertRecursive(node, word, index + 1);
Public static void main(String
[]args){
Trie tree = new Trie();
tree.insert(“CS”)}
insertRecursiver (root,“CS”,0)
insertRecursiver (root,“CS”,1)
Insert(“CS”)
False
S
insertRecursiver (root,“CS”,2)
11. Insert: cs
False
False
root
C
public class Trie {
public Trie() { root = new TrieNode(); }
Private class TrieNode{
Map<Character, TrieNode> children;
boolean endOfWord;
public TrieNode() { children = new HashMap<>();
endOfWord = false;
}
}
public void insert (String word) {
insertRecursive(root, word, 0);
}
private void insertRecursive(TrieNode current, String word, int index) {
if (index == word.length())
{
current.endOfWord = true; return;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
if (node == null)
{ node = new TrieNode();
current.children.put(ch, node);
}
insertRecursive(node, word, index + 1); }
Public static void main(String
[]args){
Trie tree = new Trie();
tree.insert(“CS”)}
insertRecursiver (root,“CS”,0)
insertRecursiver (root,“CS”,1)
Insert(“CS”)
S
insertRecursiver (root,“CS”,2)
TrueFalseTrue
12. Insert: cse
C
False
s
False
False
root
True
a
True
e
public class Trie {
public Trie() { root = new TrieNode(); }
Private class TrieNode{
Map<Character, TrieNode> children;
boolean endOfWord;
public TrieNode() { children = new HashMap<>();
endOfWord = false;
}
}
public void insert (String word) {
insertRecursive(root, word, 0);
}
private void insertRecursive(TrieNode current, String word, int index) {
if (index == word.length())
{
current.endOfWord = true; return;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
if (node == null)
{ node = new TrieNode();
current.children.put(ch, node);
}
insertRecursive(node, word, index + 1); }
True
13. Insert: data
C
False
s
False
e
True
True
root
False
False
False
d
a
t
a
False
m
public class Trie {
public Trie() { root = new TrieNode(); }
Private class TrieNode{
Map<Character, TrieNode> children;
boolean endOfWord;
public TrieNode() { children = new HashMap<>();
endOfWord = false;
}
}
public void insert (String word) {
insertRecursive(root, word, 0);
}
private void insertRecursive(TrieNode current, String word, int index) {
if (index == word.length())
{
current.endOfWord = true; return;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
if (node == null)
{ node = new TrieNode();
current.children.put(ch, node);
}
insertRecursive(node, word, index + 1); }
True
25. root
Remove: cs
C
False
s
False
e
True
True
False
False
False
d
a
t
a
True
True
s
b
a
public void delete(String word) {
delete(root, word, 0);
}
private boolean delete(TrieNode current,
String word, int index) {
if (index == word.length()) {
if (!current.endOfWord) {
return false;}
current.endOfWord = false;
return current.children.size() == 0;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
if (node == null) {
return false;}
boolean shouldDeleteCurrentNode =
delete(node, word, index + 1);
if (shouldDeleteCurrentNode) {
current.children.remove(ch);
return current.children.size() == 0;}
return false;}delete(“cs”)
delete(root,“cs”,0)
Ch= c
C?
node
delete(s,“cs”,1)
Ch= s
s?
node
delete(e,“cs”,2)
False
False
False
False
False
CS Removed