MapTreeStructure导出为地图

时间:2015-04-12 14:20:42

标签: java dictionary tree

以下代码旨在将树数据结构导出到Map<String, String>,以便以后更容易操作。但有趣的是toString()方法完美无缺,但toMap()方法有一个缺少的父A,孩子B 。任何人都有任何想法?

public static void main(String[] args) {

        MutableTree<String> tree = new MappedTreeStructure<String>();
        tree.add("A", "B");
        tree.add("A", "C");
        tree.add("C", "D");
        tree.add("E", "F");
        System.out.println(tree);

        Map<String, String> myMap = tree.toMap();
        if (myMap != null) {
            for (Map.Entry<String, String> entry : myMap.entrySet()) {
                System.out.println("parent: " + entry.getKey() + ", child: "
                        + entry.getValue());
            }
        }

    }

    private final Map<N, N> nodeParent = new HashMap<N, N>();
    private final LinkedHashSet<N> nodeList = new LinkedHashSet<N>();

    @Override
    public boolean add(N parent, N node) {

        boolean added = nodeList.add(node);
        nodeList.add(parent);
        if (added) {
            nodeParent.put(node, parent);
        }
        return added;
    }

    @Override
    public boolean remove(N node, boolean cascade) {
        if (!nodeList.contains(node)) {
            return false;
        }
        if (cascade) {
            for (N child : getChildren(node)) {
                remove(child, true);
            }
        } else {
            for (N child : getChildren(node)) {
                nodeParent.remove(child);
            }
        }
        nodeList.remove(node);
        return true;
    }

    @Override
    public List<N> getRoots() {
        return getChildren(null);
    }

    @Override
    public N getParent(N node) {
        return nodeParent.get(node);
    }

    @Override
    public List<N> getChildren(N node) {
        List<N> children = new LinkedList<N>();
        for (N n : nodeList) {
            N parent = nodeParent.get(n);
            if (node == null && parent == null) {
                children.add(n);
            } else if (node != null && parent != null && parent.equals(node)) {
                children.add(n);
            }
        }
        return children;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        dumpNodeStructure(builder, null, "- ");
        return builder.toString();
    }

    @Override
    public Map<String, String> toMap() {
        Map<String, String> map = new HashMap<String, String>();
        dumpNodeToMap(map, null);
        return map;
    }

    private void dumpNodeToMap(Map<String, String> map, N node) {
        if (node != null) {
            map.put((String) getParent(node), node.toString());
        }
        for (N child : getChildren(node)) {
            dumpNodeToMap(map, child);
        }
    }

    private void dumpNodeStructure(StringBuilder builder, N node, String prefix) {

        if (node != null) {

            builder.append(prefix);
            builder.append(node.toString());
            builder.append('\n');
            prefix = "    " + prefix;
        }

        for (N child : getChildren(node)) {
            dumpNodeStructure(builder, child, prefix);
        }

    }

控制台上的输出如下:

- A
    - B
    - C
        - D
- E
    - F

parent: null, child: E
parent: A, child: C
parent: C, child: D
parent: E, child: F

对于引用,这些是使用的两个接口类:

public interface MutableTree <N extends Serializable> extends Tree<N> {
    public boolean add (N parent, N node);
    public boolean remove (N node, boolean cascade);
    Map<String, String> toMap();
}

public interface Tree <N extends Serializable> extends Serializable {
    public List<N> getRoots ();
    public N getParent (N node);
    public List<N> getChildren (N node);
}

1 个答案:

答案 0 :(得分:1)

您的toMap方法返回一个Map<String, String>,其键是父元素名称,其值是单个子元素。这意味着当键为“A”时,只能存储一个子元素,并且这被设置为找到的最后一个子元素,在本例中为“C”,覆盖指向“A”的条目。 “B”。

相反,您的toMap方法需要返回Map<String, List<String>>,该List从每个父节点(例如“A”)映射到子元素的List,例如“B”和“C”。显然,如果List只包含一个子元素,那就没问题,但如果有多个子元素,它必须是一个列表。

创建String parentNode = getParent(node).toString(); List<String> childElements = null; if(map.contains(parentNode) { // List of child elements already exists, so get it from the Map. childElements = map.get(parentNode); } else { // List of child elements does not yet exist, so create a new List // and add it to the Map. childElements = new ArrayList<>(); map.put(parentNode, childElements); } childElements.add(node.toString()); 项而不是单个项的常用模式如下所示:

{{1}}