开发者

Java: find parent and children ....Tree, Loop, ArrayList, List, HashMap, ....what to do?

开发者 https://www.devze.com 2023-01-24 09:44 出处:网络
I\'m encouraged by so many warm-hearted Java experts that I dare to throw another question todayfor help (all my questions are from real life projects - and since I\'m taking a shortcut for learning J

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消