Trie在Java中的实现

Trie在Java中的实现,java,data-structures,trie,Java,Data Structures,Trie,这是我的Trie实现 public class Trie { private class Node{ private Map<Character, Node> children; boolean end; public Node(){ children = new HashMap<>(); end = false; } } priv

这是我的Trie实现

public class Trie {

    private class Node{
        private Map<Character, Node> children;
        boolean end;

        public Node(){
            children = new HashMap<>();
            end = false;
        }
    }

    private Node root;

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

    public void insert(String word){
        Node current = root;
        for (int i=0; i < word.length(); i++){
            char c = word.charAt(i);
            Node node = current.children.get(c);
            if(node == null){
                node = new Node();
                current.children.put(c, node);
            }
            current = node;
        }
    }

    public boolean search(String word){
        Node current = root;
        for(int i =0; i < word.length(); i++){
            char c = word.charAt(i);
            Node node = current.children.get(c);
            if(node == null) 
                return false;
            current = node;
        }
        return current.end;
    }
}
公共类Trie{
私有类节点{
私人地图儿童;
布尔端;
公共节点(){
children=newhashmap();
结束=假;
}
}
私有节点根;
公共图书馆{
根=新节点();
}
公共空白插入(字符串字){
节点电流=根;
for(int i=0;i
我想添加一个方法,给定一个字符串返回它的所有子项,这个方法可以用作真实世界的自动更正建议

以下是方法签名:

public List<String> returnAllChildren(String str){

}
public List returnAllChildren(String str){
}

我有点迷路了。非常感谢您的帮助。

首先,在插入节点时,应在尾部节点处设置end=true。然后在Trie中查找前缀时,只需找到前缀中最后一个字符的节点,然后递归地获取所有字符串。以下是一个可能对您有所帮助的示例:

public class Trie {

private class Node{
    private Map<Character, Node> children;
    boolean end;

    public Node(){
        children = new HashMap<>();
        end = false;
    }
}

private Node root;

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

public void insert(String word){
    Node current = root;
    for (int i=0; i < word.length(); i++){
        char c = word.charAt(i);
        Node node = current.children.get(c);
        if(node == null){
            node = new Node();
            current.children.put(c, node);
        }
        current = node;
    }
    // Set end to true
    current.end = true;
}

public boolean search(String word){
    Node current = root;
    for(int i =0; i < word.length(); i++){
        char c = word.charAt(i);
        Node node = current.children.get(c);
        if(node == null)
            return false;
        current = node;
    }
    return current.end;
}


private List<String> getAll(String prefix, Node node) {
    List<String> r = new ArrayList<>();
    if (node.end) {
        r.add(prefix);
    }
    for (Map.Entry<Character, Node> child : node.children.entrySet()) {
        List<String> subSuffix = getAll(prefix + child.getKey(), child.getValue());
        r.addAll(subSuffix);
    }
    return r;
}

public List<String> returnAllChildren(String str){
    List<String> r = new ArrayList<>();
    Node current = root;
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        Node node = current.children.get(c);
        if (node == null) {
            // Not found
            return r;
        }
        current = node;
    }
    // If you don't need the prefix, you can just
    // return getAll("", current);
    return getAll(str, current);
}

public static void main(String[] args) {
    Trie trie = new Trie();
    trie.insert("abc");
    trie.insert("abcd");
    trie.insert("ab");

    List<String> r = trie.returnAllChildren("a");
    for (String s : r) {
        System.out.println(s);
    }
}
}
公共类Trie{
私有类节点{
私人地图儿童;
布尔端;
公共节点(){
children=newhashmap();
结束=假;
}
}
私有节点根;
公共图书馆{
根=新节点();
}
公共空白插入(字符串字){
节点电流=根;
for(int i=0;i
首先,插入节点时,应在尾部节点处设置end=true。然后在Trie中查找前缀时,只需找到前缀中最后一个字符的节点,然后递归地获取所有字符串。以下是一个可能对您有所帮助的示例:

public class Trie {

private class Node{
    private Map<Character, Node> children;
    boolean end;

    public Node(){
        children = new HashMap<>();
        end = false;
    }
}

private Node root;

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

public void insert(String word){
    Node current = root;
    for (int i=0; i < word.length(); i++){
        char c = word.charAt(i);
        Node node = current.children.get(c);
        if(node == null){
            node = new Node();
            current.children.put(c, node);
        }
        current = node;
    }
    // Set end to true
    current.end = true;
}

public boolean search(String word){
    Node current = root;
    for(int i =0; i < word.length(); i++){
        char c = word.charAt(i);
        Node node = current.children.get(c);
        if(node == null)
            return false;
        current = node;
    }
    return current.end;
}


private List<String> getAll(String prefix, Node node) {
    List<String> r = new ArrayList<>();
    if (node.end) {
        r.add(prefix);
    }
    for (Map.Entry<Character, Node> child : node.children.entrySet()) {
        List<String> subSuffix = getAll(prefix + child.getKey(), child.getValue());
        r.addAll(subSuffix);
    }
    return r;
}

public List<String> returnAllChildren(String str){
    List<String> r = new ArrayList<>();
    Node current = root;
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        Node node = current.children.get(c);
        if (node == null) {
            // Not found
            return r;
        }
        current = node;
    }
    // If you don't need the prefix, you can just
    // return getAll("", current);
    return getAll(str, current);
}

public static void main(String[] args) {
    Trie trie = new Trie();
    trie.insert("abc");
    trie.insert("abcd");
    trie.insert("ab");

    List<String> r = trie.returnAllChildren("a");
    for (String s : r) {
        System.out.println(s);
    }
}
}
公共类Trie{
私有类节点{
私人地图儿童;
布尔端;
公共节点(){
children=newhashmap();
结束=假;
}
}
私有节点根;
公共图书馆{
根=新节点();
}
公共空白插入(字符串字){
节点电流=根;
for(int i=0;i
这是递归的一个很好的用例。我尝试采用迭代的方式。当您只想遍历每个节点的一个子节点时,迭代就可以了。但是,对于您的问题,您希望遍历每个节点的所有子节点。“简单”的迭代还不够好。(你可以不用再做了