TrieTree——筆記

 之前寫的,現在看不懂了。。。 還是要溫故而知新呀! 本篇博客僅爲了保留代碼。

 

 

#include <iostream>
#include <stdlib.h>
#include <string>
#include <queue>
using namespace std;

/************************************************************************/
/* Trie樹結構
* 查找字符串
*/
/************************************************************************/
typedef struct TrieNode
{
	char data;
	TrieNode *children[26];
	bool isEndingChar;
	int count;	// 記錄此結點被多少個單詞佔用
	int freq;	// 記錄單詞被插入多少次
	TrieNode()
	{
		isEndingChar = false;
		for (int i = 0; i < 26; ++i)
		{
			children[i] = NULL;
		}
		count = 0;
		freq = 0;
	}
}TrieNode;

/************************************************************************/
/* Trie類
*/
/************************************************************************/
class Trie
{
public:
	TrieNode *root;		// 根結點

public:
	//構造函數
	Trie()
	{
		root = NULL;
	}

	// 析構
	~Trie()
	{
		delete root;
		root = NULL;
	}
	
	/****************************************************************
	* @brief : 		插入字符串
	* @author : 	dyx
	* @date : 		2019/7/4 15:34
	* @version : 	ver 1.0
	* @inparam :	輸入string類型的字符串
	* @outparam : 
	*****************************************************************/
	void insert(string text)
	{
		if (!root)
		{
			root = new TrieNode;
		}
		TrieNode *p = root;
		for (int i = 0; i < text.length(); ++i)
		{
			int index = text[i] - 'a';
			if (p->children[index] == NULL)
			{
				p->children[index] = new TrieNode;
				p->children[index]->data = text[i];
			}
			p->count++;
			p = p->children[index];
		}
		p->count++;
		p->isEndingChar = true;
	}


	/****************************************************************
	* @brief : 		查找是否有此字符串
	* @author : 	dyx
	* @date : 		2019/7/4 15:35
	* @version : 	ver 1.0
	* @inparam :	
	* @outparam :	成功返回true
	*****************************************************************/
	bool find(string pattern)
	{
		TrieNode *p = root;
		for (int i = 0; i < pattern.length(); ++i)
		{
			int index = pattern[i] - 'a';
			if (p->children[index] == NULL)
			{
				return false;
			}
			p = p->children[index];
		}
		return p->isEndingChar;
	}


	/****************************************************************
	* @brief : 		打印所有的字典
	* @author : 	dyx
	* @date : 		2019/7/4 15:36
	* @version : 	ver 1.0
	* @inparam : 
	* @outparam : 
	*****************************************************************/
	void print(TrieNode *Root, string prefix, int &order) const
	{
		if (Root != NULL)
		{
			if (Root->isEndingChar)		// 是終止字符,prefix是不斷+出來的,是整個字符串
			{
				cout << ++order << " " << prefix <<endl;
			}
			for (int i = 0; i < 26; ++i)
			{
				if (Root->children[i] != NULL)
				{
					print(Root->children[i], prefix + (Root->children[i]->data), order);// 通過prefix+root->children[i]->data, 鏈接所有單詞
				}
			}
		}
	}
};



int main()
{
	Trie *trie = new Trie();
	trie->insert("are");
	trie->insert("arrg");
	trie->insert("hello");
	trie->insert("her");
	trie->insert("his");
	trie->insert("history");
	cout << trie->find("him") << endl;
	cout << trie->find("his") << endl;
	int id = 0;
	trie->print(trie->root, " ", id);
	system("pause");
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章