An Implementation of Trie

Source: Internet
Author: User

Trie. h file

# Ifndef _ trie_h
# DEFINE _ trie_h

# Include <cstdio>
# Include <cstdlib>
# Include <cmath>
# Include <cstring>
# Include <algorithm>
Using namespace STD;

Inline int sub_size (INT level)
{
Return level> 5? 1: 256/(level * level );
}

Struct search_tree
{
Search_tree (INT level)
{
Int Len = sub_size (level );
WORD = false;
Sub_tree = new search_tree * [Len];
Next = NULL;
For (INT I = 0; I <Len; I ++)
Sub_tree [I] = NULL;
}

Char cchar;
Bool word;
Search_tree ** sub_tree;
Search_tree * next;
};

Class Trie
{
Public:
Trie ()
{
For (INT I = 0; I <256; I ++)
T [I] = NULL;
}
~ Trie ()
{
For (INT I = 0; I <256; I ++)
Search_tree_delete (T [I], 1 );
}
Int add (char * Str );
Int prefix_in_tree (char * Str );

PRIVATE:
Void search_tree_delete (search_tree * s_tree, int level );
Void search_tree_add (search_tree * & s_tree, char * STR, int level );
Int prefix_in_tree (search_tree * s_tree, char * STR, int level );
Unsigned int Hash (unsigned char cchar, int Len );
Search_tree * t [256];
};

# Endif

Trie. cpp

# Include "trie. H"
Using namespace STD;

Unsigned int trie: Hash (unsigned char cchar, int Len)
{
Return cchar % Len;
}

Int trie: add (char * Str)
{
Int hash_code = hash (unsigned char) (* Str), 256 );
Search_tree_add (T [hash_code], STR, 1 );
Return 1;
}

Int trie: prefix_in_tree (char * Str)
{
Return prefix_in_tree (* (t + (unsigned char) (* Str), STR, 1 );
}

Void trie: search_tree_add (search_tree * & s_tree, char * STR, int level)
{
Search_tree * cur = NULL;
If (s_tree = NULL)
{
S_tree = new search_tree (LEVEL + 1 );
S_tree-> cchar = * STR;
Cur = s_tree;
}
Else
{
If (s_tree-> cchar! = * Str)
{
S_tree-> next = new search_tree (LEVEL + 1 );
S_tree-> next-> cchar = * STR;
Cur = s_tree-> next;
}
Else
Cur = s_tree;
}
 
If (* (STR + 1) = '/0 ')
{
Cur-> word = true;
Return;
}
 
Int hash_code = hash (* (STR + 1), sub_size (LEVEL + 1 ));
Search_tree_add (cur-> sub_tree [hash_code], STR + 1, LEVEL + 1 );
Return;
}

Int trie: prefix_in_tree (search_tree * s_tree, char * STR, int level)
{
If (s_tree = NULL | * STR = '/0 ')
Return 0;

Int hash_code;

For (search_tree * cur = s_tree; cur! = NULL; cur = cur-> next)
{
If (cur-> cchar = * Str)
{
If (cur-> word)
Return level;

Hash_code = hash (* (STR + 1), sub_size (LEVEL + 1 ));
Return prefix_in_tree (cur-> sub_tree [hash_code], STR + 1, LEVEL + 1 );
}
}

Return 0;
}

Void trie: search_tree_delete (search_tree * s_tree, int level)
{
If (s_tree = NULL)
Return;

Search_tree_delete (s_tree-> next, level );
Int Len = sub_size (LEVEL + 1 );
For (INT I = 0; I <Len; I ++)
{
Search_tree_delete (s_tree-> sub_tree [I], level + 1 );
}
Delete s_tree;
}

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.