【以空间换取时间】 Trie

看了看Trie,了解了一个概念,以空间换取时间,嗯,这个名词感觉好高深,Trie就是把每个单词按照树枝状的分布开来,Trie把要查找的关键词看作一个字符序列,并根据构成关键词字符的先后顺序构造用于检索的树结构;一棵m度的Trie树或者为空,或者由m棵m度的Trie树构成。


特点:
利用串的公共前缀->节约内存
根结点不包含任何字母
其余结点仅包含一个字母(非元素)
每个结点的子节点包含字母不同


找了下关于字典树的模版,贴在下面,你懂的~

#include <iostream>
#include <stack>

using namespace std;

const int sonnum=26, base='a';

struct Trie{
      int num;  //记录有多少个单词能到达次,也即相同前缀的个位
      bool terminal; //判断是否是结束节点
      struct Trie *son[sonnum];
      Trie()
      {
          num=1; terminal=false;
          memset(son,NULL,sizeof(son));
      }
};

Trie* NewTrie()
{
    Trie *temp=new Trie();
    return temp;
}

void Insert(Trie *root, char *s)
{
    Trie *temp=root;
    while(*s)
    {
        if(temp->son[*s-base]==NULL)   //不存在 则建立
                   temp->son[*s-base]=NewTrie();
        else 
                temp->son[*s-base]->num++;
        temp=temp->son[*s-base];
        s++;
    }
    temp->terminal=true;  //到达尾部,标记一个串
}

bool Search(Trie *root, char *s)
{
    Trie *temp=root;
    while(*s)
    {
        if(temp->son[*s-base]!=NULL) 
            temp=temp->son[*s-base];
        else
            return false;
        s++;
    }
    return true;
}


int Search1(Trie *root, char *s)
{
    Trie *temp=root;
    while(*s)
    {
        if(temp->son[*s-base]!=NULL) 
        return 0;
        
		temp=temp->son[*s-base];
        /*else
            return false;*/
        s++;
    }
    return p->num;
}

void DeleteAll(Trie *root)  //删除全部节点
{
    Trie *temp=root;
    for(int i=0; i<sonnum; i++)
    {
        if(root->son[i]!=NULL)
            DeleteAll(root->son[i]);
    }
    delete root;
}

bool DeleteWord(Trie *root,char *word)   //删除某个单词
{
    Trie *current=root;
    stack<Trie*> nodes;  //用来记录经过的中间节点,供以后自上而下的删除
    while(*word && current!=NULL)
    {
         nodes.push(current); //经过的中间节点压栈
         current=current->son[*word-base];
         word++;
    }
    if(current && current->terminal)  //此时current指向该word对应的最后一个节点
    { 
          while(nodes.size()!=0)
          {
              char c=*(--word);
              current=nodes.top()->son[c-base];  //取得当前处理的节点
              if(current->num==1)  //判断该节点是否只被word用,若不是,则不能删除
              {
                  delete current;
                  nodes.top()->son[c-base]=NULL;  //把上层的节点next中指向current节点的指针置为NULL
                  nodes.pop();
              }
              else  //不能删,只把num相应减1
              {
                  current->num--;
                  nodes.pop();
                  while(nodes.size()!=0)
                  {
                       char *c=--word;
                       current=nodes.top()->son[*c-base];
                       current->num--;
                       nodes.pop();
                  }
                  break;
              }
          }
          return true;
    }
    else
        return false;
}



int main()
{
    Trie *root=NewTrie();
    Insert(root,"a");
    Insert(root,"abandon");
    Insert(root,"abandoned");
    //不存在的情况
    if(Search(root,"abc"))
        printf("Found!\n");
    else
        printf("NotFound!\n");
    //存在的情况
    if(Search(root,"abandon"))
         printf("Found!\n");
    else
        printf("NotFound!\n");
    //能找到 并删除
    if(DeleteWord(root,"abandon"))
        printf("Delete!\n");
    else
        printf("NotFound\n");
    //找不到
    if(DeleteWord(root,"abc"))
        printf("Delete!\n");
    else
        printf("NotFound!\n");
    
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值