Java 在尝试中自动完成,打印根节点时出现问题

Java 在尝试中自动完成,打印根节点时出现问题,java,data-structures,trie,Java,Data Structures,Trie,在这个Trie中,我试图实现自动完成功能,我确信插入到Trie中是正确的,但在尝试打印时,我无法打印每个单词的第一个字母,因为在回溯时,它没有打印父节点,我不知何故必须打印父节点,为此我已将父字母添加到节点中,但在某些情况下,它是失败的 例如: 以下是Trie中的文字: dehradun delhi dhanbad dhule dibrugarh dimapur dindigul durgapur 如果我把字母“d”递过去 我得到: dehradun elhi dhanbad hule dib

在这个Trie中,我试图实现自动完成功能,我确信插入到Trie中是正确的,但在尝试打印时,我无法打印每个单词的第一个字母,因为在回溯时,它没有打印父节点,我不知何故必须打印父节点,为此我已将父字母添加到节点中,但在某些情况下,它是失败的

例如: 以下是Trie中的文字:

dehradun
delhi
dhanbad
dhule
dibrugarh
dimapur
dindigul
durgapur
如果我把字母“d”递过去

我得到:

dehradun
elhi
dhanbad
hule
dibrugarh
imapur
indigul
durgapur
源代码:

class TrieNode {
    char letter; // to store the letter
    TrieNode[] links; // array of 26 Nodes
    boolean fullword; // check for word
    public TrieNode parent; // storing parent letter

    TrieNode(char letter, boolean fullword, TrieNode p) {
        this.letter = letter;
        links = new TrieNode[26];
        this.fullword = fullword;
        this.parent = p;

    }
}

class temp8 {
    boolean new_word = false;
    ArrayList<String> al = new ArrayList<String>();

    TrieNode root = new TrieNode('!', false, null);

    void construct_trie(String s) {
        TrieNode temp = root; // copy root node
        TrieNode check;
        TrieNode par = root; // initially parent is root

        for (int i = 0; i < s.length(); i++) // loop over each character in word
        {
            int t = s.charAt(i); // integer value of character

            t = t - 97; // calculate appropriate index for array

            while ((check = temp.links[t]) != null) // check if root is null..
            {
                temp = temp.links[t]; // go forward
                t = s.charAt(++i);
                t = t - 97;
                par = temp; // update parent
            }

            if (i != s.length() - 1) // if not end of word..pass false
            {
                temp.links[t] = new TrieNode((char) (t + 97), false, par);
                par = temp.links[t];
            } else // if end of word..pass true to full word
            {
                temp.links[t] = new TrieNode((char) (t + 97), true, par);
                par = temp.links[t];
            }

            temp = temp.links[t]; // update temp..

        }
    }

    void read_trie(String find) // pass the intial string
    {
        int len = find.length();
        int i = 0;

        TrieNode temp = root; // copy root

        while (i != len) // go until the string matches
        {
            int t = find.charAt(i);
            t = t - 97;

            temp = temp.links[t];

            i++;

        }

        print_all(temp); // pass the node

    }

    void print_all(TrieNode t) // from here we have to recursively print all
                                // nodes if they are not null
    {

        if (t == null) // base condition
        {
            return;
        }

        if (new_word) // initially for first time don't print parent letter
        {
            System.out.print(t.parent.letter);
            new_word = false;
        }

        System.out.print(t.letter);

        if (t.fullword) {
            System.out.println();
            new_word = true;
        }

        for (int i = 0; i < 26; i++) {
            print_all(t.links[i]);

        }
    }
}
类三节点{
char letter;//用于存储字母
三节点[]链接;//由26个节点组成的数组
布尔fullword;//检查单词
公共三元组父级;//存储父级字母
三元组(字符字母、布尔全字、三元组p){
这个字母=字母;
链接=新的三元组[26];
this.fullword=fullword;
这个。父=p;
}
}
第八节课{
布尔新词=false;
ArrayList al=新的ArrayList();
三节点根=新三节点(“!”,false,null);
无效构造(字符串s){
三节点温度=根;//复制根节点
三电极检查;
TrimeNODPAR=根;//初始父是根
for(int i=0;i
您父母的信在根的每个分支上只打印一次;最好只将父字符串传递给子字符串,并仅在到达完整单词时打印

阅读代码:

    void read_trie(String find) // pass the intial string
    {
        int len = find.length();
        int i = 0;

        TrieNode temp = root; // copy root

        String match = "";
        while (i != len) // go until the string matches
        {
            int t = find.charAt(i);
            t = t - 97;

            temp = temp.links[t];
            if (temp==null) break;
            match += temp.letter;

            i++;
        }

        // Remove the last letter since it's added in the print code below
        if (match.length()>0) match = match.substring(0, match.length()-1);
        print_all(temp, match); // pass the node
    }
打印代码:

    void print_all(TrieNode t, String parent) 
            // from here we have to recursively print all nodes if they are not null
    {
        if (t == null) // base condition
        {
            return;
        }

        parent += t.letter; // prepend parent to child
        if (t.fullword) {
                // only print when you reach a full word
            System.out.println(parent);
        }

        for (int i = 0; i < 26; i++) {
                // recurse into each child, prepending the parent's string
            print_all(t.links[i],parent);
        }
    }
void print_all(三元组、字符串父级)
//从这里我们必须递归地打印所有节点,如果它们不是空的
{
if(t==null)//基本条件
{
返回;
}
parent+=t.letter;//将父级前置到子级
if(t.fullword){
//只有当你达到一个完整的单词时才打印
System.out.println(父级);
}
对于(int i=0;i<26;i++){
//递归到每个子级,在父级字符串前面加上前缀
打印所有(t.links[i],父级);
}
}

您父母的信在根的每个分支上只打印一次;最好只将父字符串传递给子字符串,并仅在到达完整单词时打印

阅读代码:

    void read_trie(String find) // pass the intial string
    {
        int len = find.length();
        int i = 0;

        TrieNode temp = root; // copy root

        String match = "";
        while (i != len) // go until the string matches
        {
            int t = find.charAt(i);
            t = t - 97;

            temp = temp.links[t];
            if (temp==null) break;
            match += temp.letter;

            i++;
        }

        // Remove the last letter since it's added in the print code below
        if (match.length()>0) match = match.substring(0, match.length()-1);
        print_all(temp, match); // pass the node
    }
打印代码:

    void print_all(TrieNode t, String parent) 
            // from here we have to recursively print all nodes if they are not null
    {
        if (t == null) // base condition
        {
            return;
        }

        parent += t.letter; // prepend parent to child
        if (t.fullword) {
                // only print when you reach a full word
            System.out.println(parent);
        }

        for (int i = 0; i < 26; i++) {
                // recurse into each child, prepending the parent's string
            print_all(t.links[i],parent);
        }
    }
void print_all(三元组、字符串父级)
//从这里我们必须递归地打印所有节点,如果它们不是空的
{
if(t==null)//基本条件
{
返回;
}
parent+=t.letter;//将父级前置到子级
if(t.fullword){
//只有当你达到一个完整的单词时才打印
System.out.println(父级);
}
对于(int i=0;i<26;i++){
//递归到每个子级,在父级字符串前面加上前缀
打印所有(t.links[i],父级);
}
}

谢谢。但当输入字符串包含3个或更多字母时,它不起作用。例如:如果我通过了
deh
我应该得到
dehradun
,但我得到了
ehradun
等等..@ManojKumar这是另一个问题,看看你的阅读代码。您需要将“匹配字符串”从read_-trie传递到print_-trie。@ManojKumar更新了我的回答谢谢。但当输入字符串包含3个或更多字母时,它不起作用。例如:如果我通过了
deh
我应该得到
dehradun
,但我得到了
ehradun
等等..@ManojKumar这是另一个问题,看看你的阅读代码。您需要将“匹配字符串”从read_-trie传递到print_-trie。@ManojKumar更新了我的答案