字典樹(shù)(Trie)

字典樹(shù)(trie)


在leetcode 上刷題遇到的一種數(shù)據(jù)結(jié)構(gòu),以前沒(méi)聽(tīng)說(shuō)過(guò),怪我孤陋寡聞,經(jīng)過(guò)了解發(fā)現(xiàn)應(yīng)用還是很廣的。覺(jué)得有必要記錄下

<a > leetcode 208</a>
<a >Trie的維基百科介紹</a>

  • 概念
  • 根節(jié)點(diǎn)不包含字符串
  • 從根節(jié)點(diǎn)到某一葉子節(jié)點(diǎn),路徑上組成的所有字符就是該節(jié)點(diǎn)對(duì)應(yīng)的字符串
  • 每個(gè)節(jié)點(diǎn)的公共前綴作為一個(gè)字符節(jié)點(diǎn)保存
  • 應(yīng)用
  • 詞頻統(tǒng)計(jì):比hash或者堆要節(jié)省空間,
  • 前綴匹配:前綴匹配的算法復(fù)雜度是O(1), 要查找匹配前綴字符串的長(zhǎng)度

實(shí)現(xiàn)


class TrieNode{
    TrieNode[] childs = new TrieNode[26]; // a-z
    int count; //frequence;
    char prefix; //當(dāng)前節(jié)點(diǎn)的字符前綴
    public TrieNode(char prefix){
        this.prefix = prefix; 
        count = 1; //root 的count 初始化為0,所以要
    }
    public TrieNode(){}
    public void addCount(){
        this.count++;
    }
    public void setPrefix(char prefix){
        this.prefix = prefix;
    }
}

public class Trie {
    private TrieNode root;

    public Trie() {
         root = new TrieNode();
    }

    public void insert(String word) {
        TrieNode searchNode = root;
        for (int level = 0;level < word.length();level++){
            char currentChar = word.charAt(level);
            TrieNode node = searchNode.childs[currentChar-'a'];
            if (searchNode.childs[currentChar-'a'] == null)      //如果這個(gè)前綴的還是null,那么新建插入一個(gè)
                searchNode.childs[currentChar-'a'] = new TrieNode(currentChar);
            else
                node.count++;
            searchNode = searchNode.childs[currentChar-'a'];
        }
    }

    public boolean search(String word) {
        TrieNode searchNode = root;
        for (int level = 0;level < word.length();level++){
            char currentChar = word.charAt(level);
            TrieNode node = searchNode.childs[currentChar-'a'];
            if (node == null)
                return false;
            searchNode = node;
        }
        int childCount = 0;
        for (TrieNode trieNode : searchNode.childs){
            if (trieNode == null)
                continue;
            childCount += trieNode.count;
        }
        return searchNode.count > childCount;
    }


    public boolean startsWith(String prefix) {
        TrieNode searchNode = root;
        for (int level = 0;level < prefix.length();level++){
            char currentChar = prefix.charAt(level);
            TrieNode node = searchNode.childs[currentChar-'a'];
            if (node == null)
                return false;
            searchNode = node;
        }
        return true;
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一,定義 在計(jì)算機(jī)科學(xué)中,trie,又稱前綴樹(shù)或字典樹(shù),是一種有序樹(shù),用于保存關(guān)聯(lián)數(shù)組,其中的鍵通常是字符串。與二...
    evil_ice閱讀 10,684評(píng)論 1 3
  • (本文轉(zhuǎn)自百度搜索第一個(gè)CSDN博客) 一、知識(shí)簡(jiǎn)介 Trie 的強(qiáng)大之處就在于它的時(shí)間復(fù)雜度。它的插入和查詢時(shí)間...
    Alan66閱讀 892評(píng)論 0 0
  • BWA Burrows-Wheeler Aligner作為一個(gè)十分出名的alignment軟件,在各個(gè)生信領(lǐng)域的比...
    栽生物坑里的信息汪閱讀 4,995評(píng)論 0 5
  • 今天是2017年7月19日,比起剛來(lái)到這個(gè)陌生城市時(shí)的狼狽,現(xiàn)在的我至少可以不用吃飯看別人的臉色,也不用穿著廉價(jià)的...
    竹風(fēng)追月閱讀 300評(píng)論 0 1
  • 告別 那些春天生發(fā)的花朵 在秋天已變成了薄薄的寒霜 期間有縈回的水流,和縈回的痛 草間的螢火,亮光只有剎那的微弱 ...
    芃麥子99閱讀 204評(píng)論 0 0

友情鏈接更多精彩內(nèi)容