首页 > 其他 > 详细

tire树

时间:2014-11-11 16:19:13      阅读:327      评论:0      收藏:0      [点我收藏+]
#pragma once 

#include <string>
using namespace std;
#define MAX_CHAR 26

struct node 
{
	bool isWord;
	node* next[MAX_CHAR];

	node()
	{
		isWord = false;
		for(int i = 0;i<MAX_CHAR;i++)
			next[i] = NULL;
	}
};

class Tire
{
public:
	Tire(){root = NULL;}

	void insert(string& str)
	{
		if(!root)
			root = new node;

		node* location = root;
		for(int i = 0;i< (int)str.length();i++)
		{
			int num = str[i] - ‘a‘;
			if(!location->next[num])
			{
				location->next[num] = new node;
			}
			location = location->next[num];
		}
		location->isWord = true;
	}

	bool search(string& str)
	{
		if(!root) return false;
		node* location = root;

		for (int i = 0;i<(int)str.length();i++)
		{
			int num = str[i] - ‘a‘;
			if(!location->next[num])
				return false;
			location = location->next[num];
		}
		return location->isWord;
	}
public:
	node* root;
};

 

tire树

原文:http://www.cnblogs.com/kangbry/p/4089529.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!