插入Trie数据结构的方法

时间:2015-11-20 08:41:54

标签: java

我正在尝试实现Patricia Trie数据结构的插入方法,但我感觉我写了很多代码行。请有人告诉我在哪里可以调用方法insert(TrieNode nodeRoot, String s) rekursiv?

代码:

private void insert(TrieNode nodeRoot, String s) {

    int len1 = nodeRoot.value.length();
    int len2 = s.length();
    int len = Math.min(len1, len2);

    for (int index = 0; index < len; index++) {
        if (s.charAt(index) != nodeRoot.value.charAt(index)) {

            // In case the both words have common substrings and after the
            // common substrings the words are split.
            String samesubString = s.substring(0, index);
            String substringSplit1 = nodeRoot.value.substring(index);
            String substringSplit2 = s.substring(index);
            if (!samesubString.isEmpty()) {
                nodeRoot.value = samesubString;
            }

            TrieNode nodeLeft = new TrieNode(substringSplit1);
            nodeLeft.isWord = true;
            TrieNode nodeRight = new TrieNode(substringSplit2);
            nodeRight.isWord = true;

            if (nodeRoot.getNext() != null && !nodeRoot.getNext().isEmpty()) {
                checkTheValieAvialable(nodeRoot, s, nodeRight);

            } else {
                nodeRoot.next.add(nodeLeft);
                nodeRoot.next.add(nodeRight);
                for (TrieNode subword : nodeRoot.getNext()) {
                    System.out.println(nodeRoot.getValue() + "---"
                            + subword.getValue());
                }
            }

            break;

        } else if (index == (s.length() - 1)
                || index == (nodeRoot.value.length() - 1)) {
            // In case the node just needs one path since one word is
            // substring of the other.
            // For example (aba and abac)

            if (len1 > len2) {
                // root value is longer
                System.out.println("root value is longer");

                String samesubString = nodeRoot.value.substring(0,
                        index + 1);
                String different = nodeRoot.value.substring(index + 1);

                if (nodeRoot.getNext() != null
                        && !nodeRoot.getNext().isEmpty()) {
                    for (TrieNode subword : nodeRoot.getNext()) {
                        String subword2 = subword.getValue();
                        boolean contains = different.contains(subword2);
                        if (contains) {
                            String[] split = different.split(subword2);
                            TrieNode leaf1 = new TrieNode(split[1]);
                            leaf1.isWord = true;
                            subword.next.add(leaf1);
                            System.out.println("Test.");

                        }

                    }
                } else {

                    String substringSplit1 =  nodeRoot.value.substring(index + 1);
                     nodeRoot.value = samesubString;
                    TrieNode leaf = new TrieNode(substringSplit1);
                    leaf.isWord = true;
                    nodeRoot.next.add(leaf);

                    for (TrieNode subword : nodeRoot.getNext()) {
                        System.out.println(nodeRoot.getValue() + "---"
                                + subword.getValue());
                    }

                }

                String substringSplit1 = nodeRoot.value
                        .substring(index + 1);

                nodeRoot.value = samesubString;
                nodeRoot.isWord = true;
                TrieNode leaf = new TrieNode(substringSplit1);
                leaf.isWord = true;
                nodeRoot.next.add(leaf);

                for (TrieNode subword : nodeRoot.getNext()) {
                    System.out.println(nodeRoot.getValue() + "---"
                            + subword.getValue());
                }

            } else {
                // new inserted string value is longer. For example (abac and aba).
                System.out.println("instered is longer");

                String samesubString = s.substring(0, index + 1);
                String different = s.substring(index + 1);
                if (nodeRoot.getNext() != null
                        && !nodeRoot.getNext().isEmpty()) {
                    for (TrieNode subword : nodeRoot.getNext()) {
                        String subword2 = subword.getValue();
                        boolean contains = different.contains(subword2);
                        if (contains) {
                            String[] split = different.split(subword2);
                            TrieNode leaf1 = new TrieNode(split[1]);
                            leaf1.isWord = true;
                            subword.next.add(leaf1);
                            System.out.println("Test.");

                        }

                    }
                } else {

                    String substringSplit1 = s.substring(index + 1);

                    s = samesubString;
                    TrieNode parentLeaf = new TrieNode(s);
                    parentLeaf.isWord = true;

                    TrieNode leaf = new TrieNode(substringSplit1);
                    leaf.isWord = true;
                    nodeRoot.next.add(leaf);

                    for (TrieNode subword : nodeRoot.getNext()) {
                        System.out.println(nodeRoot.getValue() + "---"
                                + subword.getValue());
                    }

                }

            }

        } else {

            System.out.println("They are the same - " + index);

        }

    }

}

TrieNode类:

package patriciaTrie;

import java.util.ArrayList;

public class TrieNode {


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

    TrieNode(String value){
        this.value = value;

    }

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

    public void setNext(ArrayList<TrieNode> next) {
        this.next = next;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }  
}

1 个答案:

答案 0 :(得分:1)

使用递归时请考虑以下步骤:

  1. 基本情况
  2. 逻辑(如果有的话)
  3. 递归电话。
  4. 实施例。对于阶乘数:

    int fact(int n)
    {
       if(n==0 || n==1)
         return 1;   // Base condition
       return n * fact(n-1); // Recursive call
    }
    

    在Trie中应用相同的概念:

    1. 基本条件是:遍历路径时,如果我们到达了leaf,当前字符串不在trie中,则创建一个新的边或节点并为其添加剩余字符。
    2. 如果找到匹配的节点,则递归调用插入。如果匹配节点不存在,则创建具有公共父节点的新路径。
    3. 您可以从链接:http://www.geeksforgeeks.org/trie-insert-and-search/

      获取帮助

      递归处理问题的最佳方法是确定问题中的基本条件。