• 树查找Trie树|字典树的简介及实现


    最近使用开发的程过中出现了一个小问题,顺便记录一下原因和方法--树查找

        

        Trie,称又字典树、单词查找树,是一种树形结构,用于保存大批的字符串。它的长处是:利用字符串的共公前缀来约节存储空间。

        

        相对来说,Trie树是一种比拟简略的数据结构.解理起来比拟简略,正所谓简略的西东也得付出代价.故Trie树也有它的缺陷,Trie树的存内耗消非常大.当然,或许用左儿子右兄弟的方法树建的话,可能会好点.

        

        

    其基本质性可以纳归为:
    1. 根节点不含包字符,除根节点外每个节点都只含包一个字符。 
    2. 从根节点到某一节点,径路上经过的字符连接起来,为该节点对应的字符串。 
    3. 个每节点的全部子节点含包的字符都不同相。

        

    其基本操纵有:查找 插入和除删,当然除删操纵比拟少见.我在这里只是现实了对整个树的除删操纵,至于单个word的除删操纵也很简略.

        

    索搜字典项目的方法为:

        

    (1) 从根点结开始一次索搜;

        

    (2) 取得要查找关键词的第一个字母,并根据该字母选择对应的子树并转到该子树继续行进检索;

        (3) 在响应的子树上,取得要查找关键词的第二个字母,并进一步选择对应的子树行进检索。(4) 迭代程过……(5) 在某个点结处,关键词的全部字母已被掏出,则读取附在该点结上的息信,即实现查找。

        

        其他操纵相似处置.

        

        

        

        每日一道理
    古人云:“海纳百川,有容乃大。”人世间,不可能没有矛盾和争吵,我们要以磊落的胸怀和宽容的微笑去面对它 。哈伯德也曾说过:“宽恕和受宽恕的难以言喻的快乐,是连神明都会为之羡慕的极大乐事。”让我们从宽容中享受快乐,从谅解中体会幸福吧!

        

    /*
    Name: Trie树的基本现实 
    Author: MaiK 
    Description: Trie树的基本现实 ,括包查找 插入和除删操纵
    */

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

    const  int sonnum=26, base='a';
    struct Trie
    {
        int num;//to remember how many word can reach here,that is to say,prefix
        bool terminal;//If terminal==true ,the current point has no following point
        struct Trie *son[sonnum];//the following point
    }
    ;
    Trie *NewTrie() //  create a new node
    {
        Trie *temp=new Trie;
        temp->num=1;temp->terminal=false;
        for(int i=0;i<sonnum;++i)temp->son[i]=NULL;
        return temp;
    }

    void Insert(Trie *pnt, char *s, int len) //  insert a new word to Trie tree
    {
        Trie *temp=pnt;
        for(int i=0;i<len;++i)
        {
            if(temp->son[s[i]-base]==NULL)temp->son[s[i]-base]=NewTrie();
            else temp->son[s[i]-base]->num++;
            temp=temp->son[s[i]-base];
        }

        temp->terminal=true;
    }

    void Delete(Trie *pnt) //  delete the whole tree
    {
        if(pnt!=NULL)
        {
            for(int i=0;i<sonnum;++i)if(pnt->son[i]!=NULL)Delete(pnt->son[i]);
            delete pnt; 
            pnt=NULL;
        }

    }

    Trie* Find(Trie *pnt, char *s, int len) // trie to find the current word
    {
        Trie *temp=pnt;
        for(int i=0;i<len;++i)
            if(temp->son[s[i]-base]!=NULL)temp=temp->son[s[i]-base];
            else return NULL;
        return temp;
    }
     

    文章结束给大家分享下程序员的一些笑话语录: 现在社会太数字化了,所以最好是有一个集很多功能于一身的设备!

  • 相关阅读:
    Golang之字符串格式化
    BZOJ 4513: [Sdoi2016]储能表 [数位DP !]
    BZOJ 3329: Xorequ [数位DP 矩阵乘法]
    BZOJ 1833: [ZJOI2010]count 数字计数 [数位DP]
    HDU2089 不要62 BZOJ1026: [SCOI2009]windy数 [数位DP]
    未完
    [Miller-Rabin & Pollard-rho]【学习笔记】
    BZOJ 3551: [ONTAK2010]Peaks加强版 [Kruskal重构树 dfs序 主席树]
    BZOJ 3123: [Sdoi2013]森林 [主席树启发式合并]
    BZOJ 3545: [ONTAK2010]Peaks [Splay启发式合并]
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3052188.html
Copyright © 2020-2023  润新知