字典樹Trie學習二:Java實現方式之一

來源:互聯網
上載者:User

標籤:移除   pre   lis   尋找   trie   name   length   facebook   get   

特別聲明:

  博文主要是學習過程中的知識整理,以便之後的查閱回顧。部分內容來源於網路(如有摘錄未標註請指出)。內容如有差錯,也歡迎指正!

 

系列文章:

1. 標準Trie字典樹學習一:原理解析

2. 標準Trie字典樹學習二:Java實現方式之一

 

 

Trie樹基於Java的一種簡單實現, 上代碼。

 

1. 定義節點類TrieNode

/** * TrieNode 節點類 * @author Konrad created on 2017/10/28 */public class TrieNode {    private LinkedList<TrieNode> children;  //子節點    private char data; //節點字元    private int freq; //頻率    boolean isEnd;  //是否為葉子節點    public TrieNode(char data){        this.children = new LinkedList<>();        this.freq = 0;        this.isEnd = false;        this.data = data;    }    public TrieNode childNode(char c){        if(null != children){            for(TrieNode child : children){                if(child.getData() == c){                    return child;                }            }        }        return null;    }    public LinkedList<TrieNode> getChildren() {        return children;    }    public void setChildren(LinkedList<TrieNode> children) {        this.children = children;    }    public char getData() {        return data;    }    public void setData(char data) {        this.data = data;    }    public int getFreq() {        return freq;    }    public void setFreq(int freq) {        this.freq = freq;    }    public void addFreq(int step){        this.freq += step;    }    public void subFreq(int step){        this.freq -= step;    }    public boolean isEnd() {        return isEnd;    }    public void setEnd(boolean end) {        isEnd = end;    }}

 

2. 定義Trie樹類TrieTree

/** * TrieTree Trie樹類 * * @author Konrad created on 2017/10/28 */public class TrieTree {    private TrieNode root;    public TrieTree() {        this.root = new TrieNode(‘ ‘);    }    //尋找是否存在    public boolean search(String word) {...}    //尋找返回節點    public TrieNode searchNode(String word) {...}    //插入    public void insert(String word) {...}    //移除    public void remove(String word) {...}    //擷取詞頻    public int getFreq(String word) {...}}

 

 3. 插入節點

public void insert(String word) {        TrieNode current = root;        for (int i = 0; i < word.length(); i++) {            TrieNode child = current.childNode(word.charAt(i));            if (null != child)                current = child;            else {                current.getChildren().add(new TrieNode(word.charAt(i)));                current = current.childNode(word.charAt(i));            }            current.addFreq(1);        }        current.setEnd(true);    }

 

 4.尋找節點

//尋找是否存在public boolean search(String word) {        TrieNode current = root;        for (int i = 0; i < word.length(); i++) {            if (null == current.childNode(word.charAt(i)))                return false;            else                current = current.childNode(word.charAt(i));        }        if (current.isEnd())            return true;        else            return false;    }//尋找返回節點public TrieNode searchNode(String word) {        TrieNode current = root;        for (int i = 0; i < word.length(); i++) {            if (null == current.childNode(word.charAt(i)))                return null;            else                current = current.childNode(word.charAt(i));        }        if (current.isEnd())            return current;        else            return null;    }

 

 5.刪除節點

//移除public void remove(String word) {        if (!search(word))            return;        TrieNode current = root;        for (char c : word.toCharArray()) {            TrieNode child = current.childNode(c);            if (child.getFreq() == 1) {                current.getChildren().remove(child);                return;            }else{                child.subFreq(1);                current = child;            }        }        current.setEnd(false);    }

 

6.擷取某個詞的頻率

 //擷取詞頻    public int getFreq(String word) {        TrieNode trieNode = searchNode(word);        if(null != trieNode){            return trieNode.getFreq();        }else{            return 0;        }    }

 

7.代碼測試

/** * @author Konrad created on 2017/10/28 */public class TrieTreeDemo {    public static void main(String[] args) {        String sentence = "today is good day what is you name at facebook how do you do what are you doing here";        TrieTree trieTree = new TrieTree();        for (String str : sentence.split(" ")) {            trieTree.insert(str); //插入節點,構建Trie樹        }        //判斷是否存在        System.out.println("Does the word ‘facebook‘ exists: " + trieTree.search("facebook"));        //統計do的詞頻        System.out.println("Frequent of the word ‘you‘: " + trieTree.getFreq("do"));        //移除today        System.out.println("Does the word ‘today‘ exists - before remove: " + trieTree.search("today"));        trieTree.remove("today");        System.out.println("Does the word ‘today‘ exists - after remove: " + trieTree.search("today"));    }}

 

輸出結果:

  

 

 

 這隻是Trie樹的實現方法之一,也可以使用其他不同方式來實現。

 

字典樹Trie學習二:Java實現方式之一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.