【问题标题】:Java - Deleting word in trie (pseudo code)Java - 删除 trie 中的单词(伪代码)
【发布时间】:2014-04-21 19:19:00
【问题描述】:

我一直在努力计划如何从 trie 中删除单词。我有一个实现,它在节点上使用一维数组,该数组保存一个单词的下一个字符。我了解如何删除整个单词,但不考虑包含要删除的较小单词的较大单词,因此尝试从下面的 trie 中删除“bat”、“battle”、“as”和“any”(*表示词尾)并离开“battery”、“battlefield”、“ask”和“anywho”:

    root 
    / \
   a   b-a-t*-t-e-r-y*
  / \         |
 n   s*-k*    l-e*-f-i-e-l-d*
 |
 y*-w-h-o*

以下是我目前实现的 trie:

public class TrieNode {

    protected char letter = ' ';
    protected TrieNode parentNode = null;
    protected boolean fullWord = false;
    protected TrieNode[] children = new TrieNode[26];

    public TrieNode(char letter, TrieNode parentNode){
        this.letter = letter;
        this.parentNode = parentNode;
    }

    public boolean hasChildren(){
        int index = 0;

        while(index < children.length){
            if(children[index] != null) {
                return true;
            }
            index++;
        }
        return false;
    }

    public TrieNode nodeForLetter(char ch) {
        return children[ch - 97];
    }

    public boolean isEndOfWord() {
        return fullWord;
    }
}

public class Trie implements Iterable<String> {

    private int numOfNodes;
    private int numOfWords;
    private TrieNode root = new TrieNode(' ', null);

    public Trie() {
    }

    public void addWord(String s) {
        if (hasWord(s)) return;

        int index = 0;
        TrieNode iterator = root;

        while(index < s.length()){
            if(iterator.children[s.charAt(index) - 97] == null){
                iterator.children[s.charAt(index) - 97] = new TrieNode(s.charAt(index), iterator);
                numOfNodes++;
            }

            iterator = iterator.children[s.charAt(index) - 97];

            index++;

            if(index == s.length()){
                iterator.fullWord = true;
                numOfWords++;
            }
        }
    }

    // Issues on this one 
    public void deleteWord(String s) {
        if(s.length() == 0) return; 
        // make method to check for empty trie
        else if(!(hasWord(s))) return;
        else {
            TrieNode iterator = root;
            int index = 0;

            while(index < s.length()){
                if(iterator.children[index] != null){
                    /* What would (in pseudo code) need to be put here to account for this */
                }
            }
        }
    }

    public boolean hasWord(String s) {
        TrieNode current = root;

        while(current != null){
            for (int i = 0; i < s.length(); i++) {
                if(current.letter == ' ') return false; // error here probably
                else current = current.children[i];
            }
            if (current.fullWord == true) return true;
            else return false;
        }
        return false;
    }

    public Iterator<String> iterator() {
        return new TrieIterator();  // has basic iterator functions
    }

}

谁能建议一些伪代码来帮助我解决这个问题

【问题讨论】:

    标签: java trie


    【解决方案1】:

    char = trie 中的当前位置

    char-1 = trie 中的前一个位置

    1. 在 trie 中找到要删除的单词。它将由 fullWord 指示。更改 fullWord 的状态。

    2. 单词后面是否包含更多字符?退出。

    3. 最后一个字母是否包含任何子项?退出。

    -- 从 trie 中删除字符 -- 此时唯一的选择是如果单词后面没有单词或任何子单词,所以删除 -- 所有字符,直到我们找到一个单词或任何孩子,这表示一个新单词

    ' 4.虽然当前char不是一个完整的单词并且没有子:pointer=char-1;删除字符; char=指针。

    ' 5. 退出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 2021-06-02
      相关资源
      最近更新 更多