首页 > 代码库 > tire树

tire树

#pragma once #include <string>using namespace std;#define MAX_CHAR 26struct 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树