Trie树实现一

Trie树可以用来进行高效的关键字检索,查询效率只跟要查询的关键字的长度相关,可跟hash相比拟。

有关Trie树的详细介绍,请参考http://linux.thai.net/~thep/datrie/datrie.html

这里给出了Trie树最简单的一种实现,即把Trie树看作有限状态自动机,用数组实现状态的转换。

#include <iostream>
#include <vector>

using namespace std;

class Node
{
    public:
	vector<Node *> next;
	bool leaf;
	Node():next(26, (Node *)NULL)
	{
	    leaf = false;
	}
};

class ArrayTrie
{
    public:
	Node root;
	bool insert(const char*& key);
	bool remove(const char*& key);
	bool find(const char*& key);
};

bool ArrayTrie::insert(const char*& key)
{
    Node* cur = &root; // cannot use the reference, because the reference cannot be rebound
    int i = 0;
    while( key[i] )
    {
	int pos = key[i] - 'a';
	if( (cur->next).at(pos) == NULL )
	    (cur->next)[pos] = new Node();
	cur =  (cur->next)[pos];
	++i;
    }
    return cur->leaf?false:(cur->leaf=true);
}

bool ArrayTrie::remove(const char*& key)
{
    Node* cur = &root;
    int i = 0;
    while( key[i] )
    {
	int pos = key[i] - 'a';
	if( (cur->next).at(pos) == NULL )
	    return false;
	cur = (cur->next).at(pos);
	++i;
    }
    cur->leaf = false;
    return true;
}

bool ArrayTrie::find(const char*& key)
{
    Node cur = root;
    int i = 0;
    while( key[i] )
    {
	int pos = key[i] - 'a';
	if( (cur.next)[pos] == NULL )
	    return false;
	cur = *( (cur.next).at(pos) );
	++i;
    }
    return cur.leaf;
}

void Test()
{
    ArrayTrie trie;
    const char* str[] = {"baby", "badge", "bachelor", "jar", "java", "javac", "javap", "bad"};
    for(int i = 0; i < 8; ++i)
    {
	bool r = trie.insert( str[i] );
	if( r )
	    cout << "insert " << str[i] << endl;
	else cout << "fail to insert " << str[i] << endl;
    }

    for(int i = 0; i < 8; ++i)
    {
	bool r = trie.find( str[i] );
	if( r )
	    cout << "find " << str[i] << endl;
	else cout << "fail to find " << str[i] << endl;

	trie.remove( str[i] );

	r = trie.find( str[i] );
	if( r )
	    cout << "find " << str[i] << endl;
	else cout << "fail to find " << str[i] << endl;
    }
}
int main()
{
    Test();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值