Java:查找父级和子级......Tree,Loop,ArrayList,List,HashMap,......做什么?

发布于 2024-10-02 03:34:33 字数 912 浏览 0 评论 0原文

受到这么多热心Java专家的鼓励,我今天敢于提出另一个问题来寻求帮助(我的所有问题都来自现实生活项目 - 而且由于我走的是学习Java的捷径,所以我比走捷径更困难Java的正常课程)..

这是我想要实现的数据结构(最终结果应该是一个以id为键的TreeMap)

id (int)   Characteristic (int)  parent(by id) immediate children (by id)
1           1                    {}            {2,3,4}
2           99                   {1}           {}
3           99                   {1}           {}
4           2                    {1}           {5,6}
5           99                   {2}           {}
6           3                    {2}           {7,8}
7           99                   {3}           {}
8           4                    {3}           {x,xx, ...}
....
....

前两列是已知的,后两列需要通过编程获取。

规则如下:

1)Characteristicc 为 1 的项是祖先,永远不会有父项。

2)特征为99的物品永远不会有孩子。

3) 具有特征 (2 - 9) 的项目可以有父项和子项,并且它们的子项可以有子项(必须大于它们或 99)或没有子项 (99)。例如,2 可以有 99 和 3 作为孩子,但 3 不能有 2 作为孩子)

谢谢

I'm encouraged by so many warm-hearted Java experts that I dare to throw another question today for help (all my questions are from real life projects - and since I'm taking a shortcut for learning Java, I have more difficulties than taking a normal course of Java)..

Here is the data structure I want to achieve (the final result should be a TreeMap with id as the key)

id (int)   Characteristic (int)  parent(by id) immediate children (by id)
1           1                    {}            {2,3,4}
2           99                   {1}           {}
3           99                   {1}           {}
4           2                    {1}           {5,6}
5           99                   {2}           {}
6           3                    {2}           {7,8}
7           99                   {3}           {}
8           4                    {3}           {x,xx, ...}
....
....

The first two columns are known, the last two columns need to be obtained programmatically.

Here are the rules:

1) item with Characteristc as 1 is ancestor and will never have parent.

2) items with Characteristic as 99 will never haven childen.

3) items with Characteristics (2 - 9) can have parent and children and their children can have children (that must be greater than them or 99) or no children (99). For example, 2 can have 99 and 3 as children, but 3 cannot have 2 as their child)

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长安忆 2024-10-09 03:34:33

你不是在问问题,约翰。但这里你需要考虑面向对象。像这样创建一个类:

Item:
   int id
   int characteristic
   Item parent
   List<Item> children

无论您使用什么数据结构,都可以使用上述对象。保持对象与数据结构的解耦非常重要。您稍后可能会决定需要地图或树木集。现在您所要做的就是创建一个包含所有 Item 对象的树集。

您的问题与树集无关,更多的是如何使用干净的面向对象创建这些对象。

You are not asking a question John. But you need to think object orientation here. Create a class like so:

Item:
   int id
   int characteristic
   Item parent
   List<Item> children

You can use the above object regardless in what data structure you use. Its important to keep objects decoupled from data structures. You may later decide you need a map or treeset. Now all you have to is a create a treeset with all Item objects.

Your question has nothing do with treeset and more how to create these objects using clean object orientation.

不…忘初心 2024-10-09 03:34:33

您可能会混淆树和地图的概念。 Java Map 提供了一个接口,允许您使用(通常)另一种类型的对象作为键来映射一堆对象。就您而言,您似乎将使用长整型作为键。具体的Map可以是HashMap,也可以是TreeMap,两者都有自己的优点——HashMap检索通常更快,但TreeMap具有排序键的优点。但您不能强制 TreeMap 创建特定树结构。它会自行管理树的形状。为了表示您的树项目,您需要某种树节点对象,支持 addChild、getChildren、getParent 等操作。如果您使用树节点对象构建树,那么您可以轻松地将这些对象添加到映射中并通过 id 检索它们。

You might be conflating the concepts of a tree and a map. A Java Map provides an interface that allows you to map a bunch of objects using (usually) another type of object as the key. In your case, it appears you'll be using Longs as keys. The concrete Map can be either a HashMap or a TreeMap, each of which has its own advantages -- HashMap retrieval is usually faster, but TreeMap has the advantage of sorted keys. But you can't force TreeMap to create a particular tree structure. It'll manage its tree shape itself. To represent your tree items you'll need some kind of tree node object, supporting operations like addChild, getChildren, getParent. If you build your tree using tree node objects, then you can easily add those objects to a map and retrieve them by id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文