
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- DSA - Skip List Data Structure
- Linked Lists
- DSA - Linked List Data Structure
- DSA - Doubly Linked List Data Structure
- DSA - Circular Linked List Data Structure
- Stack & Queue
- DSA - Stack Data Structure
- DSA - Expression Parsing
- DSA - Queue Data Structure
- DSA - Circular Queue Data Structure
- DSA - Priority Queue Data Structure
- DSA - Deque Data Structure
- Searching Algorithms
- DSA - Searching Algorithms
- DSA - Linear Search Algorithm
- DSA - Binary Search Algorithm
- DSA - Interpolation Search
- DSA - Jump Search Algorithm
- DSA - Exponential Search
- DSA - Fibonacci Search
- DSA - Sublist Search
- DSA - Hash Table
- Sorting Algorithms
- DSA - Sorting Algorithms
- DSA - Bubble Sort Algorithm
- DSA - Insertion Sort Algorithm
- DSA - Selection Sort Algorithm
- DSA - Merge Sort Algorithm
- DSA - Shell Sort Algorithm
- DSA - Heap Sort Algorithm
- DSA - Bucket Sort Algorithm
- DSA - Counting Sort Algorithm
- DSA - Radix Sort Algorithm
- DSA - Quick Sort Algorithm
- Matrices Data Structure
- DSA - Matrices Data Structure
- DSA - Lup Decomposition In Matrices
- DSA - Lu Decomposition In Matrices
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- DSA - Spanning Tree
- DSA - Topological Sorting
- DSA - Strongly Connected Components
- DSA - Biconnected Components
- DSA - Augmenting Path
- DSA - Network Flow Problems
- DSA - Flow Networks In Data Structures
- DSA - Edmonds Blossom Algorithm
- DSA - Maxflow Mincut Theorem
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Range Queries
- DSA - Segment Trees
- DSA - Fenwick Tree
- DSA - Fusion Tree
- DSA - Hashed Array Tree
- DSA - K-Ary Tree
- DSA - Kd Trees
- DSA - Priority Search Tree Data Structure
- Recursion
- DSA - Recursion Algorithms
- DSA - Tower of Hanoi Using Recursion
- DSA - Fibonacci Series Using Recursion
- Divide and Conquer
- DSA - Divide and Conquer
- DSA - Max-Min Problem
- DSA - Strassen's Matrix Multiplication
- DSA - Karatsuba Algorithm
- Greedy Algorithms
- DSA - Greedy Algorithms
- DSA - Travelling Salesman Problem (Greedy Approach)
- DSA - Prim's Minimal Spanning Tree
- DSA - Kruskal's Minimal Spanning Tree
- DSA - Dijkstra's Shortest Path Algorithm
- DSA - Map Colouring Algorithm
- DSA - Fractional Knapsack Problem
- DSA - Job Sequencing with Deadline
- DSA - Optimal Merge Pattern Algorithm
- Dynamic Programming
- DSA - Dynamic Programming
- DSA - Matrix Chain Multiplication
- DSA - Floyd Warshall Algorithm
- DSA - 0-1 Knapsack Problem
- DSA - Longest Common Sub-sequence Algorithm
- DSA - Travelling Salesman Problem (Dynamic Approach)
- Hashing
- DSA - Hashing Data Structure
- DSA - Collision In Hashing
- Disjoint Set
- DSA - Disjoint Set
- DSA - Path Compression And Union By Rank
- Heap
- DSA - Heap Data Structure
- DSA - Binary Heap
- DSA - Binomial Heap
- DSA - Fibonacci Heap
- Tries Data Structure
- DSA - Tries
- DSA - Standard Tries
- DSA - Compressed Tries
- DSA - Suffix Tries
- Treaps
- DSA - Treaps Data Structure
- Bit Mask
- DSA - Bit Mask In Data Structures
- Bloom Filter
- DSA - Bloom Filter Data Structure
- Approximation Algorithms
- DSA - Approximation Algorithms
- DSA - Vertex Cover Algorithm
- DSA - Set Cover Problem
- DSA - Travelling Salesman Problem (Approximation Approach)
- Randomized Algorithms
- DSA - Randomized Algorithms
- DSA - Randomized Quick Sort Algorithm
- DSA - Karger’s Minimum Cut Algorithm
- DSA - Fisher-Yates Shuffle Algorithm
- Miscellaneous
- DSA - Infix to Postfix
- DSA - Bellmon Ford Shortest Path
- DSA - Maximum Bipartite Matching
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Selection Sort Interview Questions
- DSA - Merge Sort Interview Questions
- DSA - Insertion Sort Interview Questions
- DSA - Heap Sort Interview Questions
- DSA - Bubble Sort Interview Questions
- DSA - Bucket Sort Interview Questions
- DSA - Radix Sort Interview Questions
- DSA - Cycle Sort Interview Questions
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Compressed Tries
Compressed Trie is also known as Patricia Trie or Radix Tree. It is a type of trie data structure that is used for storing and retrieving keys in a dataset, where the keys are strings.
You can think of a compressed trie as a trie where the nodes with only one child are merged with their parent nodes. This helps in reducing the space complexity of the trie and makes it more efficient.
The compression of redundant nodes helps in memory management. This type of trie is used with applications that need space management.
Properties of Compressed Tries
Following are some of the properties of compressed tries:
- The nodes of compressed tries have at least two children.
- The edges of the trie are labeled with characters.
- The keys are stored in the leaves of the trie.
Representation of Compressed Tries
Now, let's see how a compressed trie is represented:
It is represented as a tree where each node has a label and a list of children. The edges of the tree are labeled with characters. The keys are stored in the leaves of the tree.
Here is an example of a compressed trie:
(root) | b | e, i | \ ar, ll d \ \ l l / \ d, ll bull
In the above example, the keys "bear", "bell", "bid", "bull" are stored in the leaves of the trie.
Operations on Compressed Tries
We can perform the following operations on compressed tries:
- Insert: Inserting a new key into the trie.
- Search: Searching for a key in the trie.
- Delete: Deleting a key from the trie.
Insert Operation on Compressed Tries
For insertion of a new key into the compressed trie, we need to follow these steps:
Algorithm for Insert Operation
Following is the algorithm for inserting a new key into the compressed trie:
Insert the new key into the trie. If the node has only one child, merge it with its parent.
Code for Insert Operation
Now, let's implement the insert operation in a compressed trie. For this, first we need to define the structure of the trie node then implement the insert operation.
//C Program to perform Insert Operation on Compressed Trie #include <stdio.h> #include <stdlib.h> #include <string.h> #define ALPHABET_SIZE 26 // Structure for Trie Node struct TrieNode { struct TrieNode* children[ALPHABET_SIZE]; int isEndOfWord; }; // Function to create a new Trie Node struct TrieNode* createNode() { struct TrieNode* node = (struct TrieNode*)malloc(sizeof(struct TrieNode)); node->isEndOfWord = 0; for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[i] = NULL; } return node; } // Function to insert a key into the Trie void insert(struct TrieNode* root, char* key) { struct TrieNode* temp = root; for (int i = 0; i < strlen(key); i++) { int index = key[i] - 'a'; if (!temp->children[index]) { temp->children[index] = createNode(); } temp = temp->children[index]; } temp->isEndOfWord = 1; } void display(struct TrieNode* root, char str[], int level) { if (root->isEndOfWord) { str[level] = '\0'; printf("%s\n", str); } for (int i = 0; i < ALPHABET_SIZE; i++) { if (root->children[i]) { str[level] = i + 'a'; display(root->children[i], str, level + 1); } } } // Main Function int main() { struct TrieNode* root = createNode(); char keys[][8] = {"bear", "bell", "bid", "bull"}; for (int i = 0; i < 4; i++) { insert(root, keys[i]); } char str[20]; display(root, str, 0); return 0; }
Output
Following is the output of the above code:
bear bell bid bull
//C++ Program to perform Insert Operation on Compressed Trie #include <iostream> #include <string> using namespace std; #define ALPHABET_SIZE 26 // Structure for Trie Node struct TrieNode { TrieNode* children[ALPHABET_SIZE]; bool isEndOfWord; }; // Function to create a new Trie Node TrieNode* createNode() { TrieNode* node = new TrieNode(); node->isEndOfWord = false; for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[i] = NULL; } return node; } // Function to insert a key into the Trie void insert(TrieNode* root, string key) { TrieNode* temp = root; for (int i = 0; i < key.length(); i++) { int index = key[i] - 'a'; if (!temp->children[index]) { temp->children[index] = createNode(); } temp = temp->children[index]; } temp->isEndOfWord = true; } void display(TrieNode* root, char str[], int level) { if (root->isEndOfWord) { str[level] = '\0'; cout << str << endl; } for (int i = 0; i < ALPHABET_SIZE; i++) { if (root->children[i]) { str[level] = i + 'a'; display(root->children[i], str, level + 1); } } } // Main Function int main() { TrieNode* root = createNode(); string keys[] = {"bear", "bell", "bid", "bull"}; for (int i = 0; i < 4; i++) { insert(root, keys[i]); } char str[20]; display(root, str, 0); return 0; }
Output
Following is the output of the above code:
bear bell bid bull
//Java Program to perform Insert Operation on Compressed Trie class TrieNode { TrieNode[] children; boolean isEndOfWord; TrieNode() { children = new TrieNode[26]; isEndOfWord = false; } } public class Main { static final int ALPHABET_SIZE = 26; // Function to insert a key into the Trie static void insert(TrieNode root, String key) { TrieNode temp = root; for (int i = 0; i < key.length(); i++) { int index = key.charAt(i) - 'a'; if (temp.children[index] == null) { temp.children[index] = new TrieNode(); } temp = temp.children[index]; } temp.isEndOfWord = true; } static void display(TrieNode root, char[] str, int level) { if (root.isEndOfWord) { str[level] = '\0'; System.out.println(new String(str)); } for (int i = 0; i < ALPHABET_SIZE; i++) { if (root.children[i] != null) { str[level] = (char) (i + 'a'); display(root.children[i], str, level + 1); } } } // Main Function public static void main(String[] args) { TrieNode root = new TrieNode(); String[] keys = {"bear", "bell", "bid", "bull"}; for (String key : keys) { insert(root, key); } char[] str = new char[20]; display(root, str, 0); } }
Output
Following is the output of the above code:
bear bell bid bull
#Python Program to perform Insert Operation on Compressed Trie class TrieNode: def __init__(self): self.children = [None] * 26 self.isEndOfWord = False # Function to insert a key into the Trie def insert(root, key): temp = root for i in range(len(key)): index = ord(key[i]) - ord('a') if not temp.children[index]: temp.children[index] = TrieNode() temp = temp.children[index] temp.isEndOfWord = True def display(root, str, level): if root.isEndOfWord: print(''.join(str[:level])) for i in range(26): if root.children[i]: str[level] = chr(i + ord('a')) display(root.children[i], str, level + 1) # Main Function if __name__ == '__main__': root = TrieNode() keys = ["bear", "bell", "bid", "bull"] for key in keys: insert(root, key) str = [''] * 20 display(root, str, 0)
Output
Following is the output of the above code:
bear bell bid bull
Search Operation on Compressed Tries
For searching a key in the compressed trie, we need to follow these steps:
Algorithm for Search Operation
Following is the algorithm for searching a key in the compressed trie:
Search for the key in the trie. If the key is found, return true. Otherwise, return false.
Code for Search Operation
Now, let's implement the search operation in a compressed trie. For this, we need to define the structure of the trie node and then implement the search operation.
//C Program to perform Search Operation on Compressed Trie #include <stdio.h> #include <stdlib.h> #include <string.h> #define ALPHABET_SIZE 26 // Structure for Trie Node struct TrieNode { struct TrieNode* children[ALPHABET_SIZE]; int isEndOfWord; }; // Function to create a new Trie Node struct TrieNode* createNode() { struct TrieNode* node = (struct TrieNode*)malloc(sizeof(struct TrieNode)); node->isEndOfWord = 0; for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[i] = NULL; } return node; } // Function to insert a key into the Trie void insert(struct TrieNode* root, char* key) { struct TrieNode* temp = root; for (int i = 0; i < strlen(key); i++) { int index = key[i] - 'a'; if (!temp->children[index]) { temp->children[index] = createNode(); } temp = temp->children[index]; } temp->isEndOfWord = 1; } // Function to search a key in the Trie int search(struct TrieNode* root, char* key) { struct TrieNode* temp = root; for (int i = 0; i < strlen(key); i++) { int index = key[i] - 'a'; if (!temp->children[index]) { return 0; } temp = temp->children[index]; } return (temp != NULL && temp->isEndOfWord); } void display(struct TrieNode* root, char str[], int level) { if (root->isEndOfWord) { str[level] = '\0'; printf("%s\n", str); } for (int i = 0; i < ALPHABET_SIZE; i++) { if (root->children[i]) { str[level] = i + 'a'; display(root->children[i], str, level + 1); } } } // Main Function int main() { struct TrieNode* root = createNode(); char keys[][8] = {"bear", "bell", "bid", "bull"}; for (int i = 0; i < 4; i++) { insert(root, keys[i]); } char str[20]; display(root, str, 0); char key[] = "bell"; if (search(root, key)) { printf("%s is present in the trie.\n", key); } else { printf("%s is not present in the trie.\n", key); } return 0; }
Output
Following is the output of the above code:
bear bell bid bull bell is present in the trie.
//C++ Program to perform Search Operation on Compressed Trie #include <iostream> #include <string> using namespace std; #define ALPHABET_SIZE 26 // Structure for Trie Node struct TrieNode { TrieNode* children[ALPHABET_SIZE]; bool isEndOfWord; }; // Function to create a new Trie Node TrieNode* createNode() { TrieNode* node = new TrieNode(); node->isEndOfWord = false; for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[i] = NULL; } return node; } // Function to insert a key into the Trie void insert(TrieNode* root, string key) { TrieNode* temp = root; for (int i = 0; i < key.length(); i++) { int index = key[i] - 'a'; if (!temp->children[index]) { temp->children[index] = createNode(); } temp = temp->children[index]; } temp->isEndOfWord = true; } // Function to search a key in the Trie bool search(TrieNode* root, string key) { TrieNode* temp = root; for (int i = 0; i < key.length(); i++) { int index = key[i] - 'a'; if (!temp->children[index]) { return false; } temp = temp->children[index]; } return (temp != NULL && temp->isEndOfWord); } void display(TrieNode* root, char str[], int level) { if (root->isEndOfWord) { str[level] = '\0'; cout << str << endl; } for (int i = 0; i < ALPHABET_SIZE; i++) { if (root->children[i]) { str[level] = i + 'a'; display(root->children[i], str, level + 1); } } } // Main Function int main() { TrieNode* root = createNode(); string keys[] = {"bear", "bell", "bid", "bull"}; for (int i = 0; i < 4; i++) { insert(root, keys[i]); } char str[20]; display(root, str, 0); string key = "bell"; if (search(root, key)) { cout << key << " is present in the trie." << endl; } else { cout << key << " is not present in the trie." << endl; } return 0; }
Output
Following is the output of the above code:
bear bell bid bull bell is present in the trie.
//Java Program to perform Search Operation on Compressed Trie class TrieNode { TrieNode[] children; boolean isEndOfWord; TrieNode() { children = new TrieNode[26]; isEndOfWord = false; } } public class Main { static final int ALPHABET_SIZE = 26; // Function to insert a key into the Trie static void insert(TrieNode root, String key) { TrieNode temp = root; for (int i = 0; i < key.length(); i++) { int index = key.charAt(i) - 'a'; if (temp.children[index] == null) { temp.children[index] = new TrieNode(); } temp = temp.children[index]; } temp.isEndOfWord = true; } // Function to search a key in the Trie static boolean search(TrieNode root, String key) { TrieNode temp = root; for (int i = 0; i < key.length(); i++) { int index = key.charAt(i) - 'a'; if (temp.children[index] == null) { return false; } temp = temp.children[index]; } return (temp != null && temp.isEndOfWord); } static void display(TrieNode root, char[] str, int level) { if (root.isEndOfWord) { str[level] = '\0'; System.out.println(new String(str)); } for (int i = 0; i < ALPHABET_SIZE; i++) { if (root.children[i] != null) { str[level] = (char) (i + 'a'); display(root.children[i], str, level + 1); } } } // Main Function public static void main(String[] args) { TrieNode root = new TrieNode(); String[] keys = {"bear", "bell", "bid", "bull"}; for (String key : keys) { insert(root, key); } char[] str = new char[20]; display(root, str, 0); String key = "bell"; if (search(root, key)) { System.out.println(key + " is present in the trie."); } else { System.out.println(key + " is not present in the trie."); } } }
Output
Following is the output of the above code:
bear bell bid bull bell is present in the trie.
#Python Program to perform Search Operation on Compressed Trie class TrieNode: def __init__(self): self.children = [None] * 26 self.isEndOfWord = False # Function to insert a key into the Trie def insert(root, key): temp = root for i in range(len(key)): index = ord(key[i]) - ord('a') if not temp.children[index]: temp.children[index] = TrieNode() temp = temp.children[index] temp.isEndOfWord = True # Function to search a key in the Trie def search(root, key): temp = root for i in range(len(key)): index = ord(key[i]) - ord('a') if not temp.children[index]: return False temp = temp.children[index] return (temp != None and temp.isEndOfWord) def display(root, str, level): if root.isEndOfWord: print(''.join(str[:level])) for i in range(26): if root.children[i]: str[level] = chr(i + ord('a')) display(root.children[i], str, level + 1) # Main Function if __name__ == '__main__': root = TrieNode() keys = ["bear", "bell", "bid", "bull"] for key in keys: insert(root, key) str = [''] * 20 display(root, str, 0) key = "bell" if search(root, key): print(key, "is present in the trie.") else: print(key, "is not present in the trie.")
Output
Following is the output of the above code:
bear bell bid bull bell is present in the trie.
Delete Operation on Compressed Tries
For deleting a key from the compressed trie, we need to follow these steps:
Algorithm for Delete Operation
Following is the algorithm for deleting a key from the compressed trie:
Search for the key in the trie. If the key is found, mark the last node as not the end of the word. If the last node has no children, delete it.
Code for Delete Operation
Now, let's implement the delete operation in a compressed trie. For this, we need to define the structure of the trie node and then implement the delete operation.
//C Program to perform Delete Operation on Compressed Trie #include <stdio.h> #include <stdlib.h> #include <string.h> #define ALPHABET_SIZE 26 // Structure for Trie Node struct TrieNode { struct TrieNode* children[ALPHABET_SIZE]; int isEndOfWord; }; // Function to create a new Trie Node struct TrieNode* createNode() { struct TrieNode* node = (struct TrieNode*)malloc(sizeof(struct TrieNode)); node->isEndOfWord = 0; for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[i] = NULL; } return node; } // Function to insert a key into the Trie void insert(struct TrieNode* root, char* key) { struct TrieNode* temp = root; for (int i = 0; i < strlen(key); i++) { int index = key[i] - 'a'; if (!temp->children[index]) { temp->children[index] = createNode(); } temp = temp->children[index]; } temp->isEndOfWord = 1; } // Function to search a key in the Trie int search(struct TrieNode* root, char* key) { struct TrieNode* temp = root; for (int i = 0; i < strlen(key); i++) { int index = key[i] - 'a'; if (!temp->children[index]) { return 0; } temp = temp->children[index]; } return (temp != NULL && temp->isEndOfWord); } // Function to delete a key from the Trie void delete(struct TrieNode* root, char* key) { if (!search(root, key)) { return; } struct TrieNode* temp = root; for (int i = 0; i < strlen(key); i++) { int index = key[i] - 'a'; temp = temp->children[index]; } temp->isEndOfWord = 0; } void display(struct TrieNode* root, char str[], int level) { if (root->isEndOfWord) { str[level] = '\0'; printf("%s\n", str); } for (int i = 0; i < ALPHABET_SIZE; i++) { if (root->children[i]) { str[level] = i + 'a'; display(root->children[i], str, level + 1); } } } // Main Function int main() { struct TrieNode* root = createNode(); char keys[][8] = {"bear", "bell", "bid", "bull"}; for (int i = 0; i < 4; i++) { insert(root, keys[i]); } char str[20]; display(root, str, 0); char key[] = "bell"; delete(root, key); printf("Trie after deletion:\n"); display(root, str, 0); if (search(root, key)) { printf("%s is present in the trie.\n", key); } else { printf("%s is not present in the trie.\n", key); } return 0; }
Output
Following is the output of the above code:
bear bell bid bull bell is not present in the trie.
//C++ Program to perform Delete Operation on Compressed Trie #include <iostream> #include <string> using namespace std; #define ALPHABET_SIZE 26 // Structure for Trie Node struct TrieNode { TrieNode* children[ALPHABET_SIZE]; bool isEndOfWord; }; // Function to create a new Trie Node TrieNode* createNode() { TrieNode* node = new TrieNode(); node->isEndOfWord = false; for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[i] = NULL; } return node; } // Function to insert a key into the Trie void insert(TrieNode* root, string key) { TrieNode* temp = root; for (int i = 0; i < key.length(); i++) { int index = key[i] - 'a'; if (!temp->children[index]) { temp->children[index] = createNode(); } temp = temp->children[index]; } temp->isEndOfWord = true; } // Function to search a key in the Trie bool search(TrieNode* root, string key) { TrieNode* temp = root; for (int i = 0; i < key.length(); i++) { int index = key[i] - 'a'; if (!temp->children[index]) { return false; } temp = temp->children[index]; } return (temp != NULL && temp->isEndOfWord); } // Function to delete a key from the Trie void deleteKey(TrieNode* root, string key) { if (!search(root, key)) { return; } TrieNode* temp = root; for (int i = 0; i < key.length(); i++) { int index = key[i] - 'a'; temp = temp->children[index]; } temp->isEndOfWord = false; } void display(TrieNode* root, char str[], int level) { if (root->isEndOfWord) { str[level] = '\0'; cout << str << endl; } for (int i = 0; i < ALPHABET_SIZE; i++) { if (root->children[i]) { str[level] = i + 'a'; display(root->children[i], str, level + 1); } } } // Main Function int main() { TrieNode* root = createNode(); string keys[] = {"bear", "bell", "bid", "bull"}; for (int i = 0; i < 4; i++) { insert(root, keys[i]); } char str[20]; display(root, str, 0); string key = "bell"; deleteKey(root, key); cout << "Trie after deletion:" << endl; display(root, str, 0); if (search(root, key)) { cout << key << " is present in the trie." << endl; } else { cout << key << " is not present in the trie." << endl; } return 0; }
Output
Following is the output of the above code:
bear bell bid bull bell is not present in the trie.
//Java Program to perform Delete Operation on Compressed Trie class TrieNode { TrieNode[] children; boolean isEndOfWord; TrieNode() { children = new TrieNode[26]; isEndOfWord = false; } } public class Main { static final int ALPHABET_SIZE = 26; // Function to search a key in the Trie static boolean search(TrieNode root, String key) { TrieNode temp = root; for (int i = 0; i < key.length(); i++) { int index = key.charAt(i) - 'a'; if (temp.children[index] == null) { return false; } temp = temp.children[index]; } return (temp != null && temp.isEndOfWord); } // Function to insert a key into the Trie static void insert(TrieNode root, String key) { TrieNode temp = root; for (int i = 0; i < key.length(); i++) { int index = key.charAt(i) - 'a'; if (temp.children[index] == null) { temp.children[index] = new TrieNode(); } temp = temp.children[index]; } temp.isEndOfWord = true; } // Function to delete a key from the Trie static void deleteKey(TrieNode root, String key) { if (!search(root, key)) { return; } TrieNode temp = root; for (int i = 0; i < key.length(); i++) { int index = key.charAt(i) - 'a'; temp = temp.children[index]; } temp.isEndOfWord = false; } static void display(TrieNode root, char[] str, int level) { if (root.isEndOfWord) { str[level] = '\0'; System.out.println(new String(str)); } for (int i = 0; i < ALPHABET_SIZE; i++) { if (root.children[i] != null) { str[level] = (char) (i + 'a'); display(root.children[i], str, level + 1); } } } // Main Function public static void main(String[] args) { TrieNode root = new TrieNode(); String[] keys = {"bear", "bell", "bid", "bull"}; for (String key : keys) { insert(root, key); } char[] str = new char[20]; display(root, str, 0); String key = "bell"; System.out.println("Trie after deletion:"); display(root, str, 0); if (search(root, key)) { System.out.println(key + " is present in the trie."); } else { System.out.println(key + " is not present in the trie."); } } }
Output
Following is the output of the above code:
bear bell bid bull bell is not present in the trie.
#Python Program to perform Delete Operation on Compressed Trie class TrieNode: def __init__(self): self.children = [None] * 26 self.isEndOfWord = False # Function to insert a key into the Trie def insert(root, key): temp = root for i in range(len(key)): index = ord(key[i]) - ord('a') if not temp.children[index]: temp.children[index] = TrieNode() temp = temp.children[index] temp.isEndOfWord = True # Function to search a key in the Trie def search(root, key): temp = root for i in range(len(key)): index = ord(key[i]) - ord('a') if not temp.children[index]: return False temp = temp.children[index] return (temp != None and temp.isEndOfWord) # Function to delete a key from the Trie def deleteKey(root, key): if not search(root, key): return temp = root for i in range(len(key)): index = ord(key[i]) - ord('a') temp = temp.children[index] temp.isEndOfWord = False def display(root, str, level): if root.isEndOfWord: print(''.join(str[:level])) for i in range(26): if root.children[i]: str[level] = chr(i + ord('a')) display(root.children[i], str, level + 1) # Main Function if __name__ == '__main__': root = TrieNode() keys = ["bear", "bell", "bid", "bull"] for key in keys: insert(root, key) str = [''] * 20 key = "bell" print("Trie before deletion:") display(root, str, 0) deleteKey(root, key) print("Trie after deletion:") display(root, str, 0) if search(root, key): print(key, "is present in the trie.") else: print(key, "is not present in the trie.")
Output
Following is the output of the above code:
bear bell bid bull Trie after deletion: bear bid bull bell is not present in the trie.
Applications of Compressed Tries
Compressed tries are used in various applications, such as:
- Spell checkers: In spell checkers, compressed tries are used to store the dictionary of words.
- Text editors
- Routing tables in computer networks
- Genome sequence analysis
- String matching algorithms
Conclusion
In this chapter, we learned about compressed tries, which are used to store a large number of strings efficiently. We discussed the structure of a compressed trie, the insert operation, search operation, and delete operation on a compressed trie. We also implemented these operations in C, C++, Java, and Python programming languages.