Trie 树

#include <iostream>
#include <string>
using namespace std;

const char c = 'a';//根据情况,可以改为 'A'

struct TrieNode{
 TrieNode *next[26];
 int cnt;
 TrieNode(){
  cnt=0;
  for(int i=0;i<26;i++){
   next[i]=NULL;
  }
 }
}*trieRoot = new TrieNode;

void trieInsert(TrieNode *root, string s){//字符串插入
 for(int i=0;i<s.size();i++){
  int index = s[i] - c;
  if(root->next[index] == NULL)
   root->next[index] = new TrieNode;
  root = root->next;
 }
 root->cnt++;
}

void trieSearch(TrieNode *root, string s){//字符串查找
 for(int i=0;i<s.size();i++){
  int index = s[i] - c;
  if(root->next[index] == NULL)
   return false;
  root = root->next;
 }
 return true;
}

int main(){
 return 0;
}

posted @ 2011-04-27 11:04  Pengchao Bai  阅读(151)  评论(0编辑  收藏  举报