字典树-模板

//字典树-模板
#include<iostream>
#include<algorithm>
#include<string>
#include<fstream>
#include<vector>
using namespace std;


#define N 26
#define Offset 'a'

typedef struct TrieNode
{
	bool isword;
	TrieNode *next[N];
	TrieNode(){
		this->isword = false;
		for (int i = 0; i < N; i++) this->next[i] = NULL;
	}
}*Trie;

Trie tree = NULL;

void  InsertElement(const string& str){
	Trie root = tree;
	int i = 0;
	int ch;
	TrieNode* nextNode;
	while (i < str.size()){
		ch = str[i] - Offset;
		nextNode = root->next[ch];
		if (nextNode == NULL){
			nextNode = root->next[ch] = new TrieNode;
		}
		root = nextNode;
		i++;
	}
	root->isword = true;
}

你可能感兴趣的:(字典树-模板)