簡體   English   中英

Trie數據結構的插入方法

[英]insert method of the Trie data structure

我正在嘗試實現Patricia Trie數據結構的insert方法,但是我有寫很多代碼行的感覺。 請有人告訴我在哪里可以將方法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. 基本條件
  2. 邏輯(如果有)
  3. 遞歸調用。

例如 對於階乘:

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

在Trie中應用相同的概念:

  1. 基本條件是:遍歷路徑時,如果到達葉子,則當前字符串不在Trie中,則創建一個新的邊或節點並向其中添加剩余字符。
  2. 如果找到匹配的節點,則遞歸調用插入。 如果不存在匹配的節點,請使用公共父節點創建新路徑。

您可以從以下鏈接獲取幫助: http : //www.geeksforgeeks.org/trie-insert-and-search/

遞歸解決問題的最佳方法是確定問題的基本條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM