Riječ ' Pokušaj ' je izvadak iz riječi ' dohvaćanje '. Trie je sortirana podatkovna struktura temeljena na stablu koja pohranjuje skup nizova. Ima broj pokazivača jednak broju znakova abecede u svakom čvoru. Može pretraživati riječ u rječniku uz pomoć prefiksa riječi. Na primjer, ako pretpostavimo da su svi nizovi sastavljeni od slova ' a 'do' S ' u engleskoj abecedi, svaki trie čvor može imati najviše 26 bodova.
liste od lateksa
Trie je također poznat kao digitalno stablo ili stablo prefiksa. Položaj čvora u Trie određuje ključ s kojim je taj čvor povezan.
Svojstva Trie za skup niza:
- Korijenski čvor trie uvijek predstavlja nulti čvor.
- Svako dijete čvorova sortirano je abecednim redom.
- Svaki čvor može imati najviše 26 djeca (A do Ž).
- Svaki čvor (osim korijena) može pohraniti jedno slovo abecede.
Donji dijagram prikazuje trie prikaz za zvono, medvjedić, otvor, palicu, loptu, stop, kundak i hrpu.
Osnovne operacije Trie
Postoje tri operacije u Trie:
- Umetanje čvora
- Pretraživanje čvora
- Brisanje čvora
Umetanje čvora u Trie
Prva operacija je umetanje novog čvora u trie. Prije nego što započnemo implementaciju, važno je razumjeti neke točke:
- Svako slovo ulaznog ključa (riječi) umetnuto je kao pojedinac u Trie_node. Imajte na umu da djeca pokazuju na sljedeću razinu Trie čvorova.
- Niz ključnih znakova djeluje kao indeks djece.
- Ako sadašnji čvor već ima referencu na sadašnje slovo, postavite sadašnji čvor na taj referencirani čvor. U suprotnom, stvorite novi čvor, postavite slovo da bude jednako sadašnjem slovu, pa čak i započnite sadašnji čvor s ovim novim čvorom.
- Duljina znaka određuje dubinu pokušaja.
Implementacija umetanja novog čvora u Trie
public class Data_Trie { private Node_Trie root; public Data_Trie(){ this.root = new Node_Trie(); } public void insert(String word){ Node_Trie current = root; int length = word.length(); for (int x = 0; x <length; x++){ char l="word.charAt(x);" node_trie node="current.getNode().get(L);" if (node="=" null){ (); current.getnode().put(l, node); } current="node;" current.setword(true); < pre> <h3>Searching a node in Trie</h3> <p>The second operation is to search for a node in a Trie. The searching operation is similar to the insertion operation. The search operation is used to search a key in the trie. The implementation of the searching operation is shown below.</p> <p>Implementation of search a node in the Trie</p> <pre> class Search_Trie { private Node_Trie Prefix_Search(String W) { Node_Trie node = R; for (int x = 0; x <w.length(); x++) { char curletter="W.charAt(x);" if (node.containskey(curletter)) node="node.get(curLetter);" } else return null; node; public boolean search(string w) node_trie !="null" && node.isend(); < pre> <h3>Deletion of a node in the Trie</h3> <p>The Third operation is the deletion of a node in the Trie. Before we begin the implementation, it is important to understand some points:</p> <ol class="points"> <li>If the key is not found in the trie, the delete operation will stop and exit it.</li> <li>If the key is found in the trie, delete it from the trie.</li> </ol> <p> <strong>Implementation of delete a node in the Trie</strong> </p> <pre> public void Node_delete(String W) { Node_delete(R, W, 0); } private boolean Node_delete(Node_Trie current, String W, int Node_index) { if (Node_index == W.length()) { if (!current.isEndOfWord()) { return false; } current.setEndOfWord(false); return current.getChildren().isEmpty(); } char A = W.charAt(Node_index); Node_Trie node = current.getChildren().get(A); if (node == null) { return false; } boolean Current_Node_Delete = Node_delete(node, W, Node_index + 1) && !node.isEndOfWord(); if (Current_Node_Delete) { current.getChildren().remove(A); return current.getChildren().isEmpty(); } return false; } </pre> <h2>Applications of Trie</h2> <p> <strong>1. Spell Checker</strong> </p> <p>Spell checking is a three-step process. First, look for that word in a dictionary, generate possible suggestions, and then sort the suggestion words with the desired word at the top.</p> <p>Trie is used to store the word in dictionaries. The spell checker can easily be applied in the most efficient way by searching for words on a data structure. Using trie not only makes it easy to see the word in the dictionary, but it is also simple to build an algorithm to include a collection of relevant words or suggestions.</p> <p> <strong>2. Auto-complete</strong> </p> <p>Auto-complete functionality is widely used on text editors, mobile applications, and the Internet. It provides a simple way to find an alternative word to complete the word for the following reasons.</p> <ul> <li>It provides an alphabetical filter of entries by the key of the node.</li> <li>We trace pointers only to get the node that represents the string entered by the user.</li> <li>As soon as you start typing, it tries to complete your input.</li> </ul> <p> <strong>3. Browser history</strong> </p> <p>It is also used to complete the URL in the browser. The browser keeps a history of the URLs of the websites you've visited.</p> <h2>Advantages of Trie</h2> <ol class="points"> <li>It can be insert faster and search the string than hash tables and binary search trees.</li> <li>It provides an alphabetical filter of entries by the key of the node.</li> </ol> <h2>Disadvantages of Trie</h2> <ol class="points"> <li>It requires more memory to store the strings.</li> <li>It is slower than the hash table.</li> </ol> <h2>Complete program in C++</h2> <pre> #include #include #include #define N 26 typedef struct TrieNode TrieNode; struct TrieNode { char info; TrieNode* child[N]; int data; }; TrieNode* trie_make(char info) { TrieNode* node = (TrieNode*) calloc (1, sizeof(TrieNode)); for (int i = 0; i <n; i++) node → child[i]="NULL;" data="0;" info="info;" return node; } void free_trienode(trienode* node) { for(int i="0;" < n; if (node !="NULL)" free_trienode(node child[i]); else continue; free(node); trie loop start trienode* trie_insert(trienode* flag, char* word) temp="flag;" for (int word[i] ; int idx="(int)" - 'a'; (temp child[idx]="=" null) child[idx]; }trie flag; search_trie(trienode* position="word[i]" child[position]="=" 0; child[position]; && 1) 1; check_divergence(trienode* len="strlen(word);" (len="=" 0) last_index="0;" len; child[position]) j="0;" <n; j++) (j child[j]) + break; last_index; find_longest_prefix(trienode* (!word || word[0]="=" '