Dictionary tree template ..

Source: Internet
Author: User

It's boring to get a dictionary tree template. I have never understood dictionary trees very well .. You may want to learn more about the data structure .. This is important ..

Prefix:

# Include <iostream>
# Include <string>
# Include <algorithm>
Using namespace STD;
# Define Maxs 26
# Define maxl 22
# Define maxn1005
Char STR [maxn] [maxl];
Struct Node
{
Int num; // num indicates the number of strings that use the node. If num is 1, the node is the only node of the string.
Node * Next [Maxs];
Node ()
{
Num = 0;
For (INT I = 0; I <Maxs; I ++)
{
Next [I] = NULL;
}
}
} * Root;
Void insert (node * RT, char * Str)
{
Int I, ID, Len;
Len = strlen (STR );
For (I = 0; I <Len; I ++)
{
Id = STR [I]-'A ';
If (RT-> next [ID] = NULL)
RT-> next [ID] = new node ();
RT = RT-> next [ID];
RT-> num ++;
}
}
Void serch (node * RT, char * s)
{
Int I, ID, Len;
Len = strlen (s );
For (I = 0; I <Len; I ++)
{
Id = s [I]-'A ';
If (RT-> num = 1) // The End of the search. The shortest prefix has been found.
Break;
Printf ("% C", s [I]);
RT = RT-> next [ID];
}
Printf ("\ n ");
}
Int main ()
{

Int Total = 0;
Int I;
Root = new node ();
While (scanf ("% s", & STR [total])! = EOF)
{
Insert (root, STR [total]);
Total ++;
}
For (I = 0; I <total; I ++)
{
Printf ("% s", STR [I]);
Serch (root, STR [I]);
}

Return 0;
}

Corresponding to the following suffixes:

# Include <iostream>
Using namespace STD;
Const int maxm = 30, kind = 26;
Int m;
Struct Node
{
Char * s;
Int prefix;
Bool isword;
Node * Next [kind];
Node ()
{
S = NULL;
Prefix = 0;
Isword = false;
Memset (next, 0, sizeof (next ));
}
} * Root; // Root
Void insert (node * root, char * s) // insert
{
Node * P = root;
For (INT I = 0; s [I]; I ++)
{
Int x = s [I]-'A ';
P-> S = S + I;
If (p-> next [x] = NULL)
P-> next [x] = new node;
P = p-> next [x];
P-> prefix ++;
}
P-> isword = true;
}
Bool del (node * root, char * s) // Delete
{
Node * P = root;
For (INT I = 0; s [I]; I ++)
{
Int x = s [I]-'A ';
If (p-> next [x] = NULL)
Return false;
P = p-> next [x];
}
If (p-> isword)
P-> isword = false;
Else
Return false;
Return true;
}
Bool search (node * root, char * s) // search
{
Node * P = root;
For (INT I = 0; s [I]; I ++)
{
Int x = s [I]-'A ';
If (p-> next [x] = NULL)
Return false;
P = p-> next [x];
}
Return p-> isword;
}
Int count (node * root, char * s) // statistics suffix
{
Node * P = root;
For (INT I = 0; s [I]; I ++)
{
Int x = s [I]-'A ';
If (p-> next [x] = NULL)
Return 0;
P = p-> next [x];
}
Return p-> prefix;
}
Int main ()
{
M = 0;
Root = new node;
Char s [maxm];
While (gets (s ))
{
If (strcmp (S, "") = 0)
Break;
Insert (root, S );
}
While (gets (s ))
Printf ("% d \ n", count (root, s ));
Return 0;
}

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.