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

Viewed 22465

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.

5 Answers
Related