`
bcyy
  • 浏览: 1830910 次
文章分类
社区版块
存档分类
最新评论

GeekforGeeks Trie - 键树简介 - 构造 插入 和 搜索

 
阅读更多

Trie是非常高效的信息检索数据结构, 时间效率会是O(m),其中m是需要搜索的关键字的长度。缺点就是需要的存储空间大。

Trie的特点:

1. 每个Trie的节点都由多个分支构成

2. 每个分支代表可能的关键字的一个字符

3. 需要mark(标志)每个关键字的最后一个字符为leaf node(叶子节点)

英文字母的节点数据结构可以表示如下:

struct TrieNode
{
	int value; /* Used to mark leaf nodes */
	TrieNode *children[ALPHABET_SIZE];
};


插入关键字:

1. 关键字的每个字符都作为独立的trie节点, 注意每个子节点都是一组指针,指向下一个trie节点。

2 如果输入的关键字是新的,或者是比原有关键字长, 就需要构造新的节点, 而且需要标志它的结束点为叶子节点。

3. 如果关键字比原有的某个关键字短,那么就可以只标志新的叶子节点。

4. 关键字的长度决定了trie的深度


搜索关键字:

1. 比较关键字的字符,然后往下一层移动

2. 如果关键字结束,或者没有这个字符在trie中,那么搜索结束。 前者比较最后一个节点是否是叶子节点,如果是表示搜索成功,否则不成功。后者表示搜索不成功。

参考原文:

http://www.geeksforgeeks.org/trie-insert-and-search/

实现程序:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>

#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
#define ALPHABET_SIZE (26)
#define CHAR_TO_INDEX(c) ((int)c - (int)'a')

struct TrieNode
{
	int value; /* Used to mark leaf nodes */
	TrieNode *children[ALPHABET_SIZE];
};

struct TrieT
{
	TrieNode *root;
	int count;
};

// Returns new trie node (initialized to NULLs)
TrieNode *getNode(void)
{
	TrieNode *pNode = NULL;
	pNode = (TrieNode *)malloc(sizeof(TrieNode));

	if( pNode )
	{
		int i;
		pNode->value = 0;
		for(i = 0; i < ALPHABET_SIZE; i++)
		{
			pNode->children[i] = NULL;
		}
	}
	return pNode;
}

// Initializes trie (root is dummy node)
void initialize(TrieT *pTrie)
{
	pTrie->root = getNode();
	pTrie->count = 0;
}

// If not present, inserts key into trie
// If the key is prefix of trie node, just marks leaf node
void insert(TrieT *pTrie, char key[])
{
	int level = 0;
	int length = strlen(key);
	int index = 0;
	TrieNode *pCrawl;

	pTrie->count++;
	pCrawl = pTrie->root;

	for( level = 0; level < length; level++ )
	{
		index = CHAR_TO_INDEX(key[level]);
		if( !pCrawl->children[index] )
		{
			pCrawl->children[index] = getNode();
		}
		pCrawl = pCrawl->children[index];
	}
	// mark last node as leaf
	pCrawl->value = pTrie->count;
}

// Returns non zero, if key presents in trie
int search(TrieT *pTrie, char key[])
{
	int level;
	int length = strlen(key);
	int index;
	TrieNode *pCrawl;

	pCrawl = pTrie->root;
	for( level = 0; level < length; level++ )
	{
		index = CHAR_TO_INDEX(key[level]);
		if( !pCrawl->children[index] )
		{
			return 0;
		}
		pCrawl = pCrawl->children[index];
	}
	return (0 != pCrawl && pCrawl->value);
}

// Driver
int main()
{
	// Input keys (use only 'a' through 'z' and lower case)
	char keys[][8] = {"the", "a", "there", "answer", "any", "by", "bye", "their"};
	TrieT trie;

	char output[][32] = {"Not present in trie", "Present in trie"};

	initialize(&trie);

	// Construct trie
	for(int i = 0; i < ARRAY_SIZE(keys); i++)
	{
		insert(&trie, keys[i]);
	}

	// Search for different keys
	printf("%s --- %s\n", "the", output[search(&trie, "the")] );
	printf("%s --- %s\n", "these", output[search(&trie, "these")] );
	printf("%s --- %s\n", "their", output[search(&trie, "their")] );
	printf("%s --- %s\n", "thaw", output[search(&trie, "thaw")] );

	return 0;
}






分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics