Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Trie:插入方法-在两种情况下有所不同_Java - Fatal编程技术网

Java Trie:插入方法-在两种情况下有所不同

Java Trie:插入方法-在两种情况下有所不同,java,Java,我正在尝试实现Paricia trie数据结构的插入方法。我处理过许多案件,但目前我被困在案件中,无法区分这两种情况: 案例1:插入以下3个字符串: abaxlymn、abaxyz、aba 我可以用下面的代码实现这个案例 案例2:插入以下3个字符串: abafg,abara,a 在第二种情况下,我不知道如何区分第一种情况和第二种情况,因为我需要一个线索来知道何时应该将不同的子字符串ab附加到childern边以获得abfa,abra。最后,将ab作为子节点添加到节点a。请看下图 代码: pac

我正在尝试实现Paricia trie数据结构的插入方法。我处理过许多案件,但目前我被困在案件中,无法区分这两种情况:

案例1:插入以下3个字符串:

abaxlymn、abaxyz、aba
我可以用下面的代码实现这个案例

案例2:插入以下3个字符串:

abafg,abara,a

在第二种情况下,我不知道如何区分第一种情况和第二种情况,因为我需要一个线索来知道何时应该将不同的子字符串
ab
附加到childern边以获得
abfa,abra
。最后,将
ab
作为子节点添加到节点
a
。请看下图

代码:

package patriciaTrie;

import java.util.ArrayList;
import java.util.Scanner;

public class Patricia {

    private TrieNode nodeRoot;
    private TrieNode nodeFirst;

    // create a new node
    public Patricia() {
        nodeRoot = null;

    }


    // inserts a string into the trie
    public void insert(String s) {
        if (nodeRoot == null) {
            nodeRoot = new TrieNode();
            nodeFirst = new TrieNode(s);
            nodeFirst.isWord = true;
            nodeRoot.next.add(nodeFirst);

        } else {

            // nodeRoot.isWrod = false;
            insert(nodeRoot, s);
        }

    }

    private String checkEdgeString(ArrayList<TrieNode> history, String s) {
        StringBuilder sb = new StringBuilder();

        for (TrieNode nextNodeEdge : history) {
            int len1 = nextNodeEdge.edge.length();
            int len2 = s.length();
            int len = Math.min(len1, len2);
            for (int index = 0; index < len; index++) {
                if (s.charAt(index) != nextNodeEdge.edge.charAt(index)) {
                    break;

                } else {
                    char c = s.charAt(index);
                    sb.append(c);
                }
            }
        }

        return sb.toString();
    }

    private void insert(TrieNode node, String s) {

        ArrayList<TrieNode> history = new ArrayList<TrieNode>();
        for (TrieNode nextNodeEdge : node.getNext()) {
            history.add(nextNodeEdge);
        }
        String communsubString = checkEdgeString(history, s);
        System.out.println("commun String: " + communsubString);
        if (!communsubString.isEmpty()) {

            for (TrieNode nextNode : node.getNext()) {
                if (nextNode.edge.startsWith(communsubString)) {

                    String substringSplit1 = nextNode.edge
                            .substring(communsubString.length());
                    String substringSplit2 = s.substring(communsubString
                            .length());
                    if (substringSplit1.isEmpty() && !substringSplit2.isEmpty()) {
                        // 1. case: aba, abaxyz

                    } else if (substringSplit2.isEmpty()
                            && !substringSplit1.isEmpty()) {
                        // 2. case: abaxyz, aba
                        ArrayList<TrieNode> cacheNextNode = new ArrayList<TrieNode>();
                        System.out.println("node edge string is longer.");
                        if (nextNode.getNext() != null && !nextNode.getNext().isEmpty()) {
                            for (TrieNode subword : nextNode.getNext()) {                               
                                subword.edge = substringSplit1.concat(subword.edge); //This line
                                cacheNextNode.add(subword);
                            }
                            nextNode.getNext().clear();
                            nextNode.edge = communsubString;
                            nextNode.isWord = true;
                            TrieNode child = new TrieNode(substringSplit1);
                            child.isWord = true;
                            nextNode.next.add(child);
                            for(TrieNode node1 : cacheNextNode){
                                child.next.add(node1);
                                System.out.println("Test one");

                            }
                            cacheNextNode.clear();


                        }else{
                            nextNode.edge = communsubString;
                            TrieNode child = new TrieNode(substringSplit1);
                            child.isWord = true;
                            nextNode.next.add(child);
                            System.out.println("TEST");
                        }


                    } else if(substringSplit1.isEmpty() && substringSplit2.isEmpty()){
                        //3. case: aba and aba.
                        nextNode.isWord = true;

                    }else {
                        // 4. Case: abauwt and abaxyz
                        //if(nextNode.getNext().isEmpty())

                    }
                    break;
                }
            }

        } else {
            // There is no commun substring.
            System.out.println("There is no commun substring");
            TrieNode child = new TrieNode(s);
            child.isWord = true;
            node.next.add(child);
        }

    }

    public static void main(String[] args) {
        Patricia p = new Patricia();
        Scanner s = new Scanner(System.in);
        while (s.hasNext()) {
            String op = s.next();
            if (op.equals("INSERT")) {
                p.insert(s.next());
            } 

        }

    }

    class TrieNode {

        ArrayList<TrieNode> next = new ArrayList<TrieNode>();
        String edge;
        boolean isWord;

        // To create normal node.
        TrieNode(String edge) {
            this.edge = edge;

        }

        // To create the root node.
        TrieNode() {
            this.edge = "";

        }

        public ArrayList<TrieNode> getNext() {
            return next;
        }

        public String getEdge() {
            return edge;
        }

    }

}
package patriciaTrie;
导入java.util.ArrayList;
导入java.util.Scanner;
公共级帕特里夏{
私人三节点节点;
私人三节点第一;
//创建一个新节点
公共帕特里夏(){
nodeRoot=null;
}
//将字符串插入到trie中
公共空白插入(字符串s){
if(nodeRoot==null){
nodeRoot=新的三节点();
nodeFirst=新的三节点;
nodeFirst.isWord=true;
nodeRoot.next.add(nodeFirst);
}否则{
//nodeRoot.isWrod=false;
插入(nodeRoot,s);
}
}
私有字符串checkedEstring(ArrayList历史记录,字符串s){
StringBuilder sb=新的StringBuilder();
for(三节点下一节点:历史){
int len1=nextNodeEdge.edge.length();
int len2=s.length();
int len=Math.min(len1,len2);
对于(int index=0;index