Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display flat data structure into hierarchical data structure (Java)?

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.

like image 502
harshalb Avatar asked Jul 12 '10 12:07

harshalb


People also ask

How do we display hierarchy hierarchical relationship in data?

Hierarchical data is shown in tree graphs; so called because of their similarity to a tree's structure (though a tree which has been turned upside down so that the root is at the top and the branches form below it).

What data structure will you use in the hierarchical model?

Hierarchical data is best used when: The data can be stored in a “tree” form with a clear parent and child structure.

What is hierarchical structure in Java?

Hierarchical Data Structures are non-linear data structures. These structures mainly represent data containing the hierarchical relationship between its elements, for example, records, trees, etc.

Which of the data structure is used for storing data items in hierarchical order?

Tree. A tree stores a collection of items in an abstract, hierarchical way.


2 Answers

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
like image 71
Matt N Avatar answered Oct 05 '22 12:10

Matt N


You could have a design inspired by Swing TreeModel.

EDIT When I say that, I mean you could use a class implementing a likewise interface; Noticeyou can even go as far as using directly this interface, as Swing is a part of standard JRE and is available everywhere standard Java is avaiable.

Furthermore, as it is an interface (and not a class), it's only a way for you to structure your calls. As a consequence, you can easily use it in a console based application.

like image 31
Riduidel Avatar answered Oct 05 '22 11:10

Riduidel