【问题标题】:Trie Data Structure Insertion and Compression implementation in Java using Concurrent Hash Map使用 Concurrent Hash Map 在 Java 中实现 Trie 数据结构插入和压缩
【发布时间】:2020-07-16 18:10:48
【问题描述】:

尝试实现 Trie 数据结构添加元素,然后尝试将其减少为 trie 压缩机制。有一个特定的用例,由于它的实现永远作为一个无限循环运行,我们可以看看它,看看什么可能是最好的基本条件。

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class TrieNode {

    private String value;
    private ConcurrentHashMap<String, TrieNode> children;
    private boolean isWord;

    public TrieNode(String value){
        this.value = value;
        this.children = new ConcurrentHashMap<String, TrieNode>();
    }

    public static void sortbykey(ConcurrentHashMap<String, TrieNode> map) {
        ArrayList<String> sortedKeys = new ArrayList<String>(map.keySet());

        Collections.sort(sortedKeys);

    }

    public static ArrayList<TrieNode> hashMapValues (ConcurrentHashMap<String, TrieNode> map) {
        ArrayList<TrieNode> nodeList = new ArrayList<TrieNode>(map.values());
        return nodeList;
    }

    public ArrayList<TrieNode> getChildren() {
        sortbykey(this.children);
        return hashMapValues(this.children);
    }

    public ConcurrentHashMap<String, TrieNode> getChildrenMappings()
    {
        return this.children;
    }

    public void addChild(int numCharacters,String value)
    {
        //If we have arrived at the root level
        if(value.length() == numCharacters)
        {
            this.isWord = true;
        }
        //If the value still has more characters then add a child
        else
        {
            TrieNode nextNode = null;
            String nextCharacter = Character.toString(value.charAt(numCharacters));
            if(this.children.get(nextCharacter)==null)
            {
                String nextPrefix = value.substring(0,numCharacters+1);
                nextNode = new TrieNode(nextPrefix);
                this.children.put(nextCharacter, nextNode);
            }
            numCharacters++;

            this.children.get(nextCharacter).addChild(numCharacters,value);
        }

    }


    public void reduce(){

                for(ConcurrentHashMap.Entry<String,TrieNode> child : this.children.entrySet()){

                    child.getValue().reduce();

                    //Get rid of original child mapping
                    this.children.remove(child.getKey());

                    //Replace with a new mapping which may include multiple characters if my child has been combined with other children
                    String newMapping = child.getValue().getValue().substring(this.getValue().length());

                    this.children.put(newMapping, child.getValue());

                }

                if(this.children.size() == 1){

                    for(ConcurrentHashMap.Entry<String,TrieNode> child : this.children.entrySet()){

                        //If I only have a single child, replace it with his children if I'm not also a word on my own
                        if(!this.isWord()){

                            //Get rid of the extra child node
                            this.children.remove(child.getKey());

                            //Take the child nodes value, word status and children
                            this.setValue(child.getValue().getValue());
                            this.setIsWord(child.getValue().isWord());

                            for(ConcurrentHashMap.Entry<String,TrieNode> grandchild : child.getValue().getChildrenMappings().entrySet()){
                                this.children.put(grandchild.getKey(),grandchild.getValue());
                            }

                        }
                    }

                }

    }

    public boolean isWord()
    {
        return this.isWord;
    }

    public void setIsWord(boolean isWord){
        this.isWord = isWord;
    }

    public String getValue()
    {
        return this.value;
    }

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

    public static void main(String[] args){


        TrieNode tN = new TrieNode("");


        tN.addChild(0, "www.corriere.it/27esimaora/laquila");
        tN.addChild(0, "www.corriere.it/27esimaora");
        tN.addChild(0, "www.corriere.it/cronache/sesso-e-amore/");
        tN.addChild(0, "www.corriere.it/foto-gallery/cronache/sesso-e-amore/");

        tN.reduce();        

        ConcurrentHashMap<String, TrieNode> childrenMappings = tN.getChildren().get(0).getChildren().get(0).getChildren().get(0).getChildrenMappings();


        for(Map.Entry<String, TrieNode> children : childrenMappings.entrySet()){

            System.out.println(children.getKey() + " = " + children.getValue().getChildrenMappings() + children.getValue().getValue() + " "+ children.getValue().isWord);


        }

        System.out.println(tN.getChildren());
    }
}

【问题讨论】:

  • 代码太多,无法查看哥们。您能告诉我们什么操作/调用哪个方法导致无限循环吗?
  • 感谢您查看@ArunGowda。后来实际解决了。是的,所需的实现非常复杂。

标签: java hashmap trie concurrenthashmap


【解决方案1】:

我修改了您的代码并添加了一个条件,即只有当新映射不等于当前子级时,才删除当前子级并添加新映射。

我也不确定您最终要打印什么。因此我添加了我的打印语句。

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

class TrieNode {

    private String value;
    private ConcurrentHashMap<String, TrieNode> children;
    private boolean isWord;

    public TrieNode(String value){
        this.value = value;
        this.children = new ConcurrentHashMap<String, TrieNode>();
    }

    public static void sortbykey(ConcurrentHashMap<String, TrieNode> map) {
        ArrayList<String> sortedKeys = new ArrayList<String>(map.keySet());

        Collections.sort(sortedKeys);

    }

    public static ArrayList<TrieNode> hashMapValues (ConcurrentHashMap<String, TrieNode> map) {
        ArrayList<TrieNode> nodeList = new ArrayList<TrieNode>(map.values());
        return nodeList;
    }

    public ArrayList<TrieNode> getChildren() {
        sortbykey(this.children);
        return hashMapValues(this.children);
    }

    public ConcurrentHashMap<String, TrieNode> getChildrenMappings()
    {
        return this.children;
    }

    public void addChild(int numCharacters,String value)
    {
        //If we have arrived at the root level
        if(value.length() == numCharacters)
        {
            this.isWord = true;
        }
        //If the value still has more characters then add a child
        else
        {
            TrieNode nextNode = null;
            String nextCharacter = Character.toString(value.charAt(numCharacters));
            if(this.children.get(nextCharacter)==null)
            {

                String nextPrefix = value.substring(0,numCharacters+1);
                nextNode = new TrieNode(nextPrefix);
                this.children.put(nextCharacter, nextNode);
            }
            numCharacters++;

            this.children.get(nextCharacter).addChild(numCharacters,value);
        }

    }


    public void reduce(){

        for(ConcurrentHashMap.Entry<String,TrieNode> child : this.children.entrySet()){

            child.getValue().reduce();


            //Get rid of original child mapping
            //this.children.remove(child.getKey());

            //Replace with a new mapping which may include multiple characters if my child has been combined with other children
            String newMapping = child.getValue().getValue().substring(this.getValue().length());

            // only if the new mapping is not equal.
            if (!newMapping.equals(child.getKey())) {
                this.children.remove(child.getKey());
                this.children.put(newMapping, child.getValue());
            }
            //this.children.put(newMapping, child.getValue());

        }

        if(this.children.size() == 1){

            for(ConcurrentHashMap.Entry<String,TrieNode> child : this.children.entrySet()){

                //If I only have a single child, replace it with his children if I'm not also a word on my own
                if(!this.isWord()){

                    //Get rid of the extra child node
                    this.children.remove(child.getKey());

                    //Take the child nodes value, word status and children
                    this.setValue(child.getValue().getValue());
                    this.setIsWord(child.getValue().isWord());
                    for(ConcurrentHashMap.Entry<String,TrieNode> grandchild : child.getValue().getChildrenMappings().entrySet()){
                        this.children.put(grandchild.getKey(),grandchild.getValue());
                    }

                }
            }

        }

    }

    public boolean isWord()
    {
        return this.isWord;
    }

    public void setIsWord(boolean isWord){
        this.isWord = isWord;
    }

    public String getValue()
    {
        return this.value;
    }

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

    public static void main(String[] args){


        TrieNode tN = new TrieNode("");


        tN.addChild(0, "www.corriere.it/27esimaora/laquila");
        tN.addChild(0, "www.corriere.it/27esimaora");
        tN.addChild(0, "www.corriere.it/cronache/sesso-e-amore/");
        tN.addChild(0, "www.corriere.it/foto-gallery/cronache/sesso-e-amore/");

        tN.reduce();

        System.out.println(" root " + tN.getValue());
        System.out.println(" first level child 1 " + tN.getChildren().get(0).getValue());
        System.out.println(" first leve child 2 " + tN.getChildren().get(1).getValue());
        System.out.println(" second level child 1 " + tN.getChildren().get(1).getChildren().get(0).getValue());
        System.out.println(" first  level child 3 " + tN.getChildren().get(2).getValue());

        /*ConcurrentHashMap<String, TrieNode> childrenMappings = tN.getChildren().get(0).getChildren().get(0).getChildren().get(0).getChildrenMappings();


        for(Map.Entry<String, TrieNode> children : childrenMappings.entrySet()){

            System.out.println(children.getKey() + " = " + children.getValue().getChildrenMappings() + children.getValue().getValue() + " "+ children.getValue().isWord);


        }

        System.out.println(tN.getChildren());
        */
    }
}

【讨论】:

  • @Amit 请告诉我们这是否解决了您面临的问题?
  • 您好@jainsuyogj,非常感谢您的见解。这个问题后来得到了解决,我在上面创建了一个完全原始的映射。是的,无限循环是由于更新了与父节点相同的子映射。我正在循环我要更新到的相同映射。因此,创建原始映射列表集并减少它们对我有用。该方法似乎与您所做的相似,是的,这也将解决此问题。非常好,非常感谢您对此的帮助。
猜你喜欢
  • 2021-11-11
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
  • 1970-01-01
相关资源
最近更新 更多