码迷,mamicode.com
首页 > 其他好文 > 详细

tire树

时间:2014-11-11 16:19:55      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   sp   for   div   on   log   bs   

#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树

标签:blog   io   ar   sp   for   div   on   log   bs   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!