java 树形数据结构,如何在Java中实现树形数据结构?

这篇博客介绍了如何在Java中创建一个基本的树结构,用于存储任意数量子节点的字符串值。文章提供了一个名为`Tree`的类,包含一个内部类`Node`,每个节点都有一个数据字段和一个子节点列表。为了操作这个树结构,你需要添加添加、删除和遍历的方法。这个简单的树结构可以方便地获取给定节点的所有子节点及其字符串值。
摘要由CSDN通过智能技术生成

Is there any standard Java library class to represent a tree in Java?

Specifically I need to represent the following:

The sub-tree at any node can have an arbitrary number of children

Each node (after the root) and it's children will have string value

I need to get all the children (some sort of list or array of Strings) of a given node and it's string value(i.e. a method that will take a node as input and return all the string values of the children node as output)

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

解决方案

Here:

public class Tree {

private Node root;

public Tree(T rootData) {

root = new Node();

root.data = rootData;

root.children = new ArrayList>();

}

public static class Node {

private T data;

private Node parent;

private List> 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值