Trie परिचय
In this page:
class TrieNode:
def __init__(self):
self.children = {}
self.is_end = False
What is a Trie
एक trie (prefix tree) strings का एक collection एक समय में एक character store करता है, जहां root से हर path एक prefix spell करता है, और एक जैसे letters से शुरू होने वाले paths tree में वही initial nodes share करते हैं।
उदाहरण: What is a Trie
#include <iostream>
using namespace std;
int main() {
cout << "'cat' and 'car' share the path c->a, then split at t vs r -- shared prefixes share the same nodes";
return 0;
}
public class Main {
public static void main(String[] args) {
System.out.println("'cat' and 'car' share the path c->a, then split at t vs r -- shared prefixes share the same nodes");
}
}
print("'cat' and 'car' share the path c->a, then split at t vs r -- shared prefixes share the same nodes")
#include <stdio.h>
int main() {
printf("'cat' and 'car' share the path c->a, then split at t vs r -- shared prefixes share the same nodes");
return 0;
}
Login to try C/C++/Java code in the editor
Trie Nodes
किसी trie में हर node इसके possible अगले characters के links रखता है (अक्सर lowercase English letters के लिए 26 slots), साथ ही एक marker indicate करते हुए कि इस node तक का path सिर्फ एक prefix के बजाय एक असली stored word पूरा करता है।
उदाहरण: Trie Nodes
#include <iostream>
using namespace std;
struct Node { Node* children[26] = {}; bool isEnd = false; };
int main() {
Node root;
cout << "Each node holds 26 child slots (a-z) plus an isEnd marker for 'a stored word ends here'";
return 0;
}
public class Main {
static class Node { Node[] children = new Node[26]; boolean isEnd = false; }
public static void main(String[] args) {
Node root = new Node();
System.out.println("Each node holds 26 child slots (a-z) plus an isEnd marker for 'a stored word ends here'");
}
}
class Node:
def __init__(self):
self.children = {}
self.is_end = False
root = Node()
print("Each node holds 26 child slots (a-z) plus an is_end marker for 'a stored word ends here'")
#include <stdio.h>
struct Node { struct Node* children[26]; int isEnd; };
int main() {
printf("Each node holds 26 child slots (a-z) plus an isEnd marker for 'a stored word ends here'");
return 0;
}
Login to try C/C++/Java code in the editor
Prefix Sharing
चूंकि shared prefixes nodes की वही chain reuse करते हैं, cat, car, और cart जैसे words सभी उसी ca path से branch off होते हैं बजाय हर एक को characters का पूरी तरह अलग sequence store करने के।
उदाहरण: Prefix Sharing
#include <iostream>
#include <string>
using namespace std;
int main() {
string words[] = {"cat", "car", "cart"};
cout << "'cat','car','cart' all branch off the shared 'ca' path instead of storing 3 full separate sequences";
return 0;
}
public class Main {
public static void main(String[] args) {
String[] words = {"cat", "car", "cart"};
System.out.println("'cat','car','cart' all branch off the shared 'ca' path instead of storing 3 full separate sequences");
}
}
words = ["cat", "car", "cart"]
print("'cat','car','cart' all branch off the shared 'ca' path instead of storing 3 full separate sequences")
#include <stdio.h>
int main() {
char* words[] = {"cat", "car", "cart"};
printf("'cat','car','cart' all branch off the shared 'ca' path instead of storing 3 full separate sequences");
return 0;
}
Login to try C/C++/Java code in the editor
Trie Benefits
यह shared-prefix structure prefix-based operations को — जैसे यह जांचना कि क्या कोई stored word किसी दिए string से शुरू होता है — essentially answer करना free बना देता है, क्योंकि आप हर stored word scan करने के बजाय सिर्फ मौजूदा links follow कर रहे हैं।
उदाहरण: Trie Benefits
#include <iostream>
using namespace std;
int main() {
cout << "Checking whether any stored word starts with a given prefix is nearly free -- just follow existing nodes";
return 0;
}
public class Main {
public static void main(String[] args) {
System.out.println("Checking whether any stored word starts with a given prefix is nearly free -- just follow existing nodes");
}
}
print("Checking whether any stored word starts with a given prefix is nearly free -- just follow existing nodes")
#include <stdio.h>
int main() {
printf("Checking whether any stored word starts with a given prefix is nearly free -- just follow existing nodes");
return 0;
}
Login to try C/C++/Java code in the editor
Applications
Tries type करते समय autocomplete suggestions, एक dictionary के खिलाफ match करते spell-checkers, और Boggle या Scrabble solvers जैसे word games को power देते हैं जिन्हें हज़ारों words में fast prefix lookups चाहिए।
उदाहरण: Applications
#include <iostream>
using namespace std;
int main() {
cout << "Tries power autocomplete, spell-checkers, and word games needing fast prefix lookups";
return 0;
}
public class Main {
public static void main(String[] args) {
System.out.println("Tries power autocomplete, spell-checkers, and word games needing fast prefix lookups");
}
}
print("Tries power autocomplete, spell-checkers, and word games needing fast prefix lookups")
#include <stdio.h>
int main() {
printf("Tries power autocomplete, spell-checkers, and word games needing fast prefix lookups");
return 0;
}
Login to try C/C++/Java code in the editor
- हर node में पूरे words store करना, जब हर node सिर्फ एक character store करता है और shared prefixes nodes reuse करते हैं।
- किसी word के end को mark करना भूल जाना, इसलिए
"car"को"cart"के prefix से अलग नहीं बताया जा सकता। childrenarray कोnullptrinitialize न करना, इसलिए garbage pointers follow होते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: