Java树数据结构? [英] Java tree data-structure?

查看:207
本文介绍了Java树数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



具体来说,我需要表示以下内容:




  • 任何节点上的树可以有任意数量的孩子

  • 每个节点(根之后)只是一个String (其孩子也是字符串)

  • 我需要能够获得所有的孩子(某种列表或字符串数​​组)给出一个表示给定节点的输入字符串



有没有可用的结构,或者我需要创建自己的(如果这样的实现建议会很棒)。

解决方案

这里:

  public class Tree< T& ; {
private Node< T>根;

public Tree(T rootData){
root = new Node< T>();
root.data = rootData;
root.children = new ArrayList< Node>>();
}

public static class Node< T> {
私人T数据;
private Node< T>父母
private List< Node< T>>孩子
}
}

这是一个基本的树结构,可用于 String 或任何其他对象。实现简单的树可以很容易地执行你所需要的。



所有你需要添加的是添加,删除,遍历和构造函数的方法。 Node Tree 的基本构建块。


Is there a good available (standard Java) data structure to represent a tree in Java?

Specifically I need to represent the following:

  • The tree at any node can have an arbitrary number of children
  • Each node (after the root) is just a String (whose children are also Strings)
  • I need to be able to get all the children (some sort of list or array of Strings) given an input string representing a given node

Is there an available structure for this or do I need to create my own (if so implementation suggestions would be great).

解决方案

Here:

public class Tree<T> {
    private Node<T> root;

    public Tree(T rootData) {
        root = new Node<T>();
        root.data = rootData;
        root.children = new ArrayList<Node<T>>();
    }

    public static class Node<T> {
        private T data;
        private Node<T> parent;
        private List<Node<T>> children;
    }
}

That is a basic tree structure that can be used for String or any other object. It is fairly easy to implement simple trees to do what you need.

All you need to add are methods for add to, removing from, traversing, and constructors. The Node is the basic building block of the Tree.

这篇关于Java树数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆