Tree Learning---------Dictionary tree (Trie trees)

Source: Internet
Author: User

The dictionary tree, also called the alphabet number, the prefix tree and so on, can not only store characters, but also store numbers, etc.

Also known as the word search tree, Trie tree, is a tree-shaped structure, is a hash tree variant. Typical applications are used for statistics, sorting, and saving a large number of strings (but not limited to strings), so it is often used by search engine systems for text frequency statistics.


Its advantages are: the use of the common prefix of the string to save storage space, minimize unnecessary string comparisons, query efficiency is higher than the hash table. The dictionary tree is very similar to the dictionary, when you want to look up a word is not in the dictionary tree, first look at the first letter of the word is not in the first layer of the dictionary, if not, the dictionary tree does not have the word, if in the child node in the letter to find whether there is a second letter of the word, There is no indication that there is no such word, and some words continue searching in the same way. The dictionary tree can be used not only to store letters, but also to store numbers and other data.


Node structure:

Each node corresponds to a maximum stored character array. Assuming that the dictionary only has 26 lowercase English letters, there should be an array of length 26 under each node. In other speech, the more types of elements can be saved, the larger the individual nodes occupy memory. If the Chinese characters are stored in a dictionary tree, then each node must open an array of thousands of commonly used Chinese characters as storage space, and the memory occupied is not an order of magnitude. But Trie tree is a kind of data structure that uses space to change time, fish and bear paw often can't have both.

Details of achievements:

      takes the first character to insert the string, starting at the child node of the root node, matching whether the current character already has a node, or pointing the pointer to that node. None creates a node for the character and points the pointer to the new node.
    • iteration.

Find Details:
Loops through the first character to insert the string, starting at the child node of the root node, matching whether the current character already has a node, and then continuing the loop, without returning false. The lookup is completed until the last character is matched.

Tree structure Chart:
We use apps, apply, Apple, append, back, basic, backen a few English words to create a tree-shaped structure:

It is easy to see that the same prefix of English words, will be merged in the same node, Trie tree along a node to retrieve,

Code:

#include <stdio.h>
#include <string.h>
#define MAXN 100000
typedef struct trienode{
int ncount;
struct Trienode *next[26];
}trienode;
Trienode MEMORY[MAXN];
int ALLOCP = 0;
void Init (Trienode **proot)
{
*proot = NULL;
}


Trienode *build ()
{
int i;
Trienode *p;
p = &Memory[allocp++];
P--ncount = 1;
for (int i = 0; i <; ++i)
{
P->next[i] = NULL;
}
return p;
}


void Insert (Trienode **proot, char *s)
{
int I, k;
Trienode *p;
if (! ( p = *proot))
{
p = *proot = Build ();
}
i = 0;
while (S[i])
{
K = s[i++]-' a ';
if (!p->next[k])
{
P->next[k] = Build ();
}
Else
{
P-&GT;NEXT[K]-ncount++;
}
p = p->next[k];
}
}


int Search (Trienode **proot, char *s)
{
int k;
Trienode *p;
p = *proot;
int i = 0;
while (P && s[i])
{
K = s[i++]-' a ';
p = p->next[k];
}
if (!p)
return 0;
Else
Return p->ncount;
}




int main ()
{
Char s[11];
Trienode * Root = NULL;
printf ("Please enter the string you want to store: \ n");
while (gets (s) && s[0])
{
Insert (&root, s);
}
printf ("Please enter a prefix for the query: \ n");
while (gets (s))
{
printf ("%d\n", Search (&root, s));
}
return 0;
}


Tree Learning---------Dictionary tree (Trie trees)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.