一个Trie树的实现

Trie树实现代码:

// Max branch number. Such as 26 for lower case letter set, 10 for digit character set.
const int MAX_BRANCH_NUM = 26;

// Node of Trie Tree        
class CTrieNode
{
public:
	CTrieNode()
	{
		nPrefixCount = 0;
		nWordCount = 0;
		memset(next, NULL, sizeof(next));
	}
public:
	int nPrefixCount;    // Count of prefix in the Trie Tree
	int nWordCount;      // Count of word in the Trie Tree
	CTrieNode* next[MAX_BRANCH_NUM];  // Point to all the nodes in the next level
};

// Trie Tree class
class CTrieTree
{
public:
	CTrieTree();    // Constructor
	~CTrieTree();   // Destructor
	int GetIndex(char ch) const;           // Get the index of 'ch' in the 'next' array
	bool CheckWord(const char *s) const;   // Check if there is illegal character
	bool InsertWord(const char *s) const;  // Insert a word
	int SearchPrefix(const char *s) const; // Search the count of a prefix
	int SearchWord(const char *s) const;   // Search the count of a word
	bool DeleteWord(const char *s) const;  // Delete a word
private:
	void FreeTreeNodes(CTrieNode * root);  // Free those tree nodes
private:
	CTrieNode *pRoot; // Root node pointer of Trie Tree
};

// Constructor
CTrieTree::CTrieTree()
{
	pRoot = new CTrieNode; 
}

// Destructor
CTrieTree::~CTrieTree()
{
	FreeTreeNodes(pRoot);
}

// Get the index of 'ch' in the 'next' array
int CTrieTree::GetIndex(char ch) const
{
	// This is for lower case letter set
	return ch - 'a';
}

// Check if there is illegal character in the word
bool CTrieTree::CheckWord(const char *s) const
{
	// This is for lower case letter set
	for(int i = 0; s[i] != '\0'; i++)
		if(s[i] < 'a' || s[i] > 'z')
			return false;
	return true;
}

// Insert a word
bool CTrieTree::InsertWord(const char *s) const
{
	// Can't insert a null string or an word containing illegal character
	if((s[0] == '\0') || (CheckWord(s)== false))
		return false;
	CTrieNode * pCurrent = pRoot;
	int i = 0;
	while(1)
	{
		int nIndex = GetIndex(s[i]);
		if(pCurrent->next[nIndex] == NULL)
			pCurrent->next[nIndex] = new CTrieNode;
		pCurrent = pCurrent->next[nIndex];
		pCurrent->nPrefixCount++;
		i++;
		if(s[i] == '\0')
		{
			pCurrent->nWordCount++;
			break;
		}
	}
	return true;
};

// Search the count of a prefix in the Trie Tree
int CTrieTree::SearchPrefix(const char *s) const
{
	// There are no null prefix or a prefix containing illegal character
	if((s[0] == '\0') || (CheckWord(s)== false))
		return 0;
	CTrieNode * pCurrent = pRoot;
	int i = 0;
	while(1)
	{
		int nIndex = GetIndex(s[i]);
		pCurrent = pCurrent->next[nIndex];
		if((pCurrent == NULL) || (pCurrent->nPrefixCount == 0))
			return 0;
		i++;
		if(s[i] == '\0')
			return pCurrent->nPrefixCount;
	}
}

// Search the count of a word in the Trie Tree
int CTrieTree::SearchWord(const char *s) const
{
	// There are no null word or a word containing illegal character
	if((s[0] == '\0') || (CheckWord(s)== false))
		return 0;
	CTrieNode * pCurrent = pRoot;
	int i = 0;
	while(1)
	{
		int nIndex = GetIndex(s[i]);
		pCurrent = pCurrent->next[nIndex];
		if((pCurrent == NULL) || (pCurrent->nPrefixCount == 0))
			return 0;
		i++;
		if(s[i] == '\0')
			return pCurrent->nWordCount;
	}
}

// Delete a word
bool CTrieTree::DeleteWord(const char *s) const
{
	if(!SearchWord(s))
		return false;
	CTrieNode * pCurrent = pRoot;
	int i = 0;
	while(1)
	{
		int nIndex = GetIndex(s[i]);
		pCurrent = pCurrent->next[nIndex];
		pCurrent->nPrefixCount--;
		i++;
		if(s[i] == '\0')
		{
			pCurrent->nWordCount--;
			break;
		}
	}
	return true;
}

// Free those tree nodes
void CTrieTree::FreeTreeNodes(CTrieNode * pRoot)
{
	if(pRoot == NULL)
		return;
	for(int i = 0; i < MAX_BRANCH_NUM; i++)
		FreeTreeNodes(pRoot->next[i]);
	free(pRoot);
	pRoot = NULL;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值