Trie implementation

后端 未结 6 1932
醉梦人生
醉梦人生 2020-12-09 05:19

I am attempting to implement a very simple Trie in Java that supports 3 operations. I\'d like it to have an insert method, a has method (ie is a certain word in the trie),

6条回答
  •  青春惊慌失措
    2020-12-09 05:44

    Here is my implementation: -

    public class Tries {
    
    class Node {
        HashMap children;
        boolean end;
        public Node(boolean b){
            children = new HashMap();
            end = false;
        }
    }
    private Node root;
    public Tries(){
        root = new Node(false);
    }
    public static void main(String args[]){
        Tries tr = new Tries();
        tr.add("dog");
        tr.add("doggy");
    
        System.out.println(tr.search("dogg"));
        System.out.println(tr.search("doggy"));
    }
    private boolean search(String word) {
        Node crawl = root;
        int n = word.length();
        for(int i=0;i

提交回复
热议问题