简体   繁体   English

在Java中显示树层次结构及其值

[英]Display tree hierarchy with their values in Java

I have to create a tree structure with a parent node having many children, and each child can also have their children. 我必须创建一个具有许多子节点的父节点的树结构,并且每个子节点也可以具有其子节点。 Each node will be identified by their unique id and name. 每个节点将通过其唯一的ID和名称进行标识。

So when I enter any id from the hierarchy, all node-ids and their child node-ids with their names should get printed. 因此,当我输入层次结构中的任何ID时,应打印所有节点ID及其名称的子节点ID。 How can I achieve this(any sample code would help) and which collection should be used from collection framework or do I have to use plain data structures in Java? 如何实现这一点(任何示例代码都会有帮助)以及应从集合框架中使用哪个集合,还是必须在Java中使用纯数据结构?

One possible way is to create your own tree structure (node with children) and wrap it into another structure that tracks nodes' ids. 一种可能的方法是创建自己的树结构(带有子节点的节点)并将其包装到另一个跟踪节点ID的结构中。 Something like that: 像这样:

public class Tree<I, A> {
    private final HashMap<I, Node<I, A>> map = new HashMap<>();
    private final Node<I, A> root;

    public Tree(I id, A value) {
        root = new Node<>(id, value);
        map.put(id, root);
    }

    public void addChild(I parentId, I id, A value) {
        Node<I, A> parent = map.get(parentId);
        Node<I, A> child = new Node<>(id, value);
        parent.children.add(child);
        map.put(id, child);
    }

    public A getById(I id) {
        return map.get(id).value;
    }

    public String subtreeToString(I id) {
        return map.get(id).toString();
    }

    private static class Node<I, A> {
        private final I id;
        private final A value;
        private final ArrayList<Node<I, A>> children = new ArrayList<>();

        private Node(I id, A value) {
            this.id = id;
            this.value = value;
        }

        private void print(int depth, PrintWriter pw) {
            for (int i = 0; i < depth; i++) {
                pw.print("\t");
            }
            pw.println("[" + id + ", " + value + "]");
            for (Node<I, A> child : children) {
                child.print(depth + 1, pw);
            }
        }

        @Override
        public String toString() {
            StringWriter writer = new StringWriter();
            print(0, new PrintWriter(writer));
            return writer.toString();
        }
    }
}

Usage: 用法:

Tree<Integer, String> tree = new Tree<>(1, "Bob");

tree.addChild(1, 2, "John");
tree.addChild(1, 3, "James");
tree.addChild(2, 4, "David");
tree.addChild(2, 5, "Alice");

System.out.println(tree.subtreeToString(1));
System.out.println(tree.subtreeToString(2));

Output: 输出:

[1, Bob]
    [2, John]
        [4, David]
        [5, Alice]
    [3, James]

[2, John]
    [4, David]
    [5, Alice]

您将必须制作自己的树结构,并且在打印时,只需在父节点上调用toString(),然后从该节点上在子节点上调用toString()等。

There might be some structure ready for the task, but on the other hand: this task is very straightforward to do yourself – just do a recursive in-order walk down the tree: 可能已经为该任务准备了一些结构,但另一方面:此任务很容易自己完成–只需按顺序递归地沿树走即可:

  1. Start at the root node 从根节点开始
  2. If there is a left node: enter it and go to 2. 如果有左节点:请输入并转到2。
  3. Else: print the content. 否则:打印内容。
  4. If there is a right node: enter it and go to 2. 如果有正确的节点:请输入并转到2。
  5. Else: return 其他:返回

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM