如何将平面数据结构显示为分层数据结构(Java)? [英] How to display flat data structure into hierarchical data structure (Java)?
问题描述
我最近在这个工作的实际测试中面临了这个问题。假设你有一个这样的平面数据结构:
**类别** **姓名** **父母**
1电子产品0
2电视1
3 21inch 2
4 23inch 2
5液晶显示器2
6播放器1
7 mp3player 6
8 vcd播放器6
9 dvd播放器6
10 hd质量8
现在,从上述平面数据结构我们想显示如下所示分层树结构。
-Electronics
| -Television
| | -21英寸
| | -23英寸
| | -lcd显示
| -Player
| | -mp3player
| | -vcdplayer
| | | -HD显示
| | -DVD播放器
然后如果我在我的数组中添加另一个条目,如:
11测试3
那么它应该显示测试
条目刚刚低于 21inch
。
ArrayList ,并且能够遍历到第二级,但不能在第三级。那么这样做的完美方法是什么?
谢谢
编辑:
我被要求使用基于DOS的Java应用程序构建此概念。
下面是一些使用递归在层次结构中列出的示例代码。 Item类有一个子列表。诀窍是将新的孩子添加到正确的父母。这是我创建的方法:
public Item getItemWithParent(int parentID){
项目结果= null ;
if(this.categoryID == parentID){
result = this;
} else {
for(Item nextChild:children){
result = nextChild.getItemWithParent(parentID);
if(result!= null){
break;
}
}
}
返回结果;
}
可能是一种更有效的方法,但这样做有效。
然后,当您要向层次结构中添加新项目时,请执行以下操作:
public void addItem(int categoryID,String name,int parentID){
Item parentItem = findParent(parentID);
parentItem.addChild(new Item(categoryID,name,parentID));
}
private Item findParent(int parentID){
return rootNode.getItemWithParent(parentID);
}
对于实际显示,我只是传递一个标签级如何进入标签,然后为每个孩子增加如下所示:
public String toStringHierarchy(int tabLevel){
StringBuilder builder = new StringBuilder(); (int i = 0; i< tabLevel; i ++){
builder.append(\t);
}
builder.append( - +名称);
builder.append(\\\
); (item nextChild:children)
{
builder.append(nextChild.toStringHierarchy(tabLevel + 1));
}
return builder.toString();
}
哪个给我这个:
-electronics
-Television
-21inch
-Test
-23inch
-LCD显示
-player
-mp3player
-vcd player
-hd quality
-dvd player
I have recently faced this question in a practical test for a job .
Suppose you are given a flat data structure like this :
**Category** **Name** **Parent**
1 electronics 0
2 Television 1
3 21inch 2
4 23inch 2
5 LCD display 2
6 player 1
7 mp3player 6
8 vcd player 6
9 dvd player 6
10 hd quality 8
Now from the above flat data structure we want to show something like the below hierarchical tree structure .
-Electronics
| -Television
| | -21 inch
| | -23 inch
| | -lcd display
| -Player
| | -mp3player
| | -vcdplayer
| | | -HD display
| | -DVD player
Then If I am adding another entry to my array like :
11 Test 3
then it should show Test
entry just below 21inch
.
So for this sort of thing I am currently using ArrayList
and have been able to traverse till second level but can't do so for third level . So what is the perfect way for doing this ?
Thanks
EDIT :
I was asked to build this concept using DOS based Java application only.
Here is some sample code that lists them in a hierarchy using recursion. The Item class has a List of children. The trick is adding any new children to the right parent. Here is the method I created to do this:
public Item getItemWithParent(int parentID){
Item result = null;
if(this.categoryID == parentID){
result = this;
} else {
for(Item nextChild : children){
result = nextChild.getItemWithParent(parentID);
if(result != null){
break;
}
}
}
return result;
}
There is probably a more efficient way, but this works.
Then, when you want to add new items to your hierarchy, do something like this:
public void addItem(int categoryID, String name, int parentID) {
Item parentItem = findParent(parentID);
parentItem.addChild(new Item(categoryID, name, parentID));
}
private Item findParent(int parentID) {
return rootNode.getItemWithParent(parentID);
}
For the actual display, I just pass in a "tab level" that says how far to tab in, then increment it for each child like this:
public String toStringHierarchy(int tabLevel){
StringBuilder builder = new StringBuilder();
for(int i = 0; i < tabLevel; i++){
builder.append("\t");
}
builder.append("-" + name);
builder.append("\n");
for(Item nextChild : children){
builder.append(nextChild.toStringHierarchy(tabLevel + 1));
}
return builder.toString();
}
Which gives me this:
-electronics
-Television
-21inch
-Test
-23inch
-LCD display
-player
-mp3player
-vcd player
-hd quality
-dvd player
这篇关于如何将平面数据结构显示为分层数据结构(Java)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!