What is the best datastructure for hierarchical data representation in java?

application of tree data structure
hierarchical tree structure, with example
hierarchical data structure example
java hierarchical tree data structure
data hierarchy in java
data structures in java
tree with multiple child nodes python
graph data structure in java example

I need to create a data structure in java which can represents hierarchy of data. Example use case is shown in the image below.

an organization hierarchy

In my case only the leaves level will have data, and the internal nodes should act like indexes. I should be able to get data from the data structure using multiple keys(Composite keys).

Is it okay to use nested maps, or should I implement an m way tree(B tree/B+ tree) for this use case.

you can implement Trie for this use case. Iterate over composite key and return data if found.

class definition:

public class TrieNode {
    private HashMap<String, TrieNode> children;
    private Data data;
    private boolean isLeaf;

   // ...

find query will look like:

public Data find(List<String> compositeKey) {
    TrieNode current = root;
    for (String key: compositeKey) {
        TrieNode node = current.getChildren().get(key);
        if (node == null) {
            return null;
        current = node;
    if(current.isLeaf()) {
       return current.getData();
    } else {
       return null;

insert will look like:

public void insert(List<String> compositeKey, Data data) {
    TrieNode current = root;

    for (String key: compositeKey) {
        current = current.getChildren()
          .computeIfAbsent(key, c -> new TrieNode());

What is the best datastructure for hierarchical data representation in , you can implement Trie for this use case. Iterate over composite key and return data if found. class definition: public class TrieNode { private  A hashing function is the Hierarchical data structure in java which is used to convert a big input key to a small integer value which is more practical. In hash function maps the keys to values. Every key is associated with its value. There is a possibility that one or more values have the same key.

If the structure of the nested data is constant you could use normal classes with properties.

If the structure is dynamic I would use Maps, the interface and ignore the implementation.

About using using a custom tree structure, if you can use classes, thats better. If you use Maps, I would start with a HashMap and if you find it to be a problem your can replace your Map to something else later.

Hierarchical Data Structure in Java, Representation of Binary Tree. A binary tree is represented by a pointer on the topmost node in a tree, if the nodes in the tree are empty  Java tree data-structure? Basically, there is nothing in the standard libs that offers a Tree representation out of the box, except for the JTree in the swing package. You can either roll your own (some tips offered in the linked answer), or use that one, which works well actually.

Obviously you have to use Tree like data structure. Here is the sample code of that.

High level code idea

class Entity{  
    // declare you attributes and below two properties

    List<Entity> children;

    boolean isleafNode;// for parent node its 'false' and for leaf node it will 'true'


hierarchical data structure – Java, SQL and jOOQ., Posts about hierarchical data structure written by lukaseder. Polymorphism and encapsulation claim that the implementation should be hidden It's not a pattern that is best suited for visiting large and complex data structures with lots of  Java enums are typically used to hold array like data. This tip shows how to use enum for hierarchical structures.

Lecture 9: Generic trees and other data structures, However a hierarchical structure can be used not only to represent data accurately but also organize it efficiently. A good example of this is the binary search tree. An array is the simplest and most widely used data structure. Other data structures like stacks and queues are derived from arrays. Here’s an image of a simple array of size 4, containing elements (1, 2, 3 and 4). Each data element is assigned a positive numerical value called the Index, which corresponds to the position of that item in the array. The majority of languages define the starting index of the array as 0.

Everything you need to know about tree data structures, This post is to help you better understand the Tree Data Structure and to clarify An organization's structure is another example of a hierarchy. The data structure is a particular way of organizing data in a computer. The developer must choose the appropriate data structure for better performance. If the developer chooses a bad data structure, the system does not perform well. This article explains each data structure’s advantages and usage. Linked List

The top data structures you should know for your next coding interview, 30 July 2018 / #Java Sometimes interview questions explicitly mention a data structure, for data structures is essential even if you're just trying to get better at your Here's a visual representation of the internal structure of a linked list: A tree is a hierarchical data structure consisting of vertices (nodes)  Data Structures Tutorial. Data Structures (DS) tutorial provides basic and advanced concepts of Data Structure. Our Data Structure tutorial is designed for beginners and professionals. Data Structure is a way to store and organize data so that it can be used efficiently.

  • You should implement proper classes, as the different hierarchy levels have a set type (university -> faculty -> field).
  • refer stackoverflow.com/questions/3522454/java-tree-data-structure
  • Thanks for responding. In my usecase only the leaf level will have values, internal nodes should act like indexes.
  • lavivienpost.com/build-hierarchy-tree