Getting a list of words from a Trie

后端 未结 10 1012
野性不改
野性不改 2021-01-02 15:29

I\'m looking to use the following code to not check whether there is a word matching in the Trie but to return a list all words beginning with the prefix inputted by the use

10条回答
  •  无人及你
    2021-01-02 15:48

    I built a trie once for one of ITA puzzles

    public class WordTree {
    
    
    class Node {
    
        private final char ch;
    
        /**
         * Flag indicates that this node is the end of the string.
         */
        private boolean end;
    
        private LinkedList children;
    
        public Node(char ch) {
            this.ch = ch;
        }
    
        public void addChild(Node node) {
            if (children == null) {
                children = new LinkedList();
            }
            children.add(node);
        }
    
        public Node getNode(char ch) {
            if (children == null) {
                return null;
            }
            for (Node child : children) {
                if (child.getChar() == ch) {
                    return child;
                }
            }
            return null;
        }
    
        public char getChar() {
            return ch;
        }
    
        public List getChildren() {
            if (this.children == null) {
                return Collections.emptyList();
            }
            return children;
        }
    
        public boolean isEnd() {
            return end;
        }
    
        public void setEnd(boolean end) {
            this.end = end;
        }
    }
    
    
    Node root = new Node(' ');
    
    public WordTree() {
    }
    
    /**
     * Searches for a strings that match the prefix.
     *
     * @param prefix - prefix
     * @return - list of strings that match the prefix, or empty list of no matches are found.
     */
    public List getWordsForPrefix(String prefix) {
        if (prefix.length() == 0) {
            return Collections.emptyList();
        }
        Node node = getNodeForPrefix(root, prefix);
        if (node == null) {
            return Collections.emptyList();
        }
        List> chars = collectChars(node);
        List words = new ArrayList(chars.size());
        for (LinkedList charList : chars) {
            words.add(combine(prefix.substring(0, prefix.length() - 1), charList));
        }
        return words;
    }
    
    
    private String combine(String prefix, List charList) {
        StringBuilder sb = new StringBuilder(prefix);
        for (Character character : charList) {
            sb.append(character);
        }
        return sb.toString();
    }
    
    
    private Node getNodeForPrefix(Node node, String prefix) {
        if (prefix.length() == 0) {
            return node;
        }
        Node next = node.getNode(prefix.charAt(0));
        if (next == null) {
            return null;
        }
        return getNodeForPrefix(next, prefix.substring(1, prefix.length()));
    }
    
    
    private List> collectChars(Node node) {
        List> chars = new ArrayList>();
    
        if (node.getChildren().size() == 0) {
            chars.add(new LinkedList(Collections.singletonList(node.getChar())));
        } else {
            if (node.isEnd()) {
                chars.add(new LinkedList 
                Collections.singletonList(node.getChar())));
            }
            List children = node.getChildren();
            for (Node child : children) {
                List> childList = collectChars(child);
                for (LinkedList characters : childList) {
                    characters.push(node.getChar());
                    chars.add(characters);
                }
            }
        }
        return chars;
    }
    
    
    public void addWord(String word) {
        addWord(root, word);
    }
    
    private void addWord(Node parent, String word) {
        if (word.trim().length() == 0) {
            return;
        }
        Node child = parent.getNode(word.charAt(0));
        if (child == null) {
            child = new Node(word.charAt(0));
            parent.addChild(child);
        } if (word.length() == 1) {
            child.setEnd(true);
        } else {
            addWord(child, word.substring(1, word.length()));
        }
    }
    
    
    public static void main(String[] args) {
        WordTree tree = new WordTree();
        tree.addWord("world");
        tree.addWord("work");
        tree.addWord("wolf");
        tree.addWord("life");
        tree.addWord("love");
        System.out.println(tree.getWordsForPrefix("wo"));
    }
    

    }

提交回复
热议问题