How Trees Are Stored in Data Structure?


Larry Thompson

In data structures, a tree is a hierarchical data structure that is widely used in computer science and programming. It is an abstract model of a hierarchical structure, where each node in the tree can have zero or more child nodes.

What is a Tree?

A tree consists of nodes connected by edges. The topmost node in the tree is called the root, and it does not have any parent node. Each node can have any number of child nodes, but each child node can have only one parent.

Unlike arrays or linked lists, which are linear data structures, trees are non-linear data structures. They are primarily used to represent hierarchical relationships between elements.

Storing Trees in Data Structures

There are several ways to store trees in data structures. Two commonly used methods are:

1. Array Representation

An array representation of a tree stores the elements of the tree in an array using sequential ordering.

  • Root Node: The first element of the array represents the root node.
  • Child Nodes: For any given node at index i, its left child can be found at index 2i+1, and its right child at index 2i+2.

This representation is useful when the tree is complete or almost complete because it allows for efficient access to elements using their indices. However, it may waste memory if the tree is sparse or unbalanced.

2. Linked Representation

In linked representation, each node of the tree contains a reference to its child nodes.

  • Node Structure: Each node contains two components – one for storing the data and another for storing references to its child nodes.
  • Child Pointers: The child pointers can be implemented using pointers or references in programming languages.

This representation is more flexible and memory-efficient than the array representation. It allows for easy traversal of the tree and handles sparse or unbalanced trees effectively. However, it requires additional memory for the pointers.

Tree Traversal

Once a tree is stored in a data structure, various traversal algorithms can be used to access or manipulate the elements of the tree. Some commonly used tree traversal algorithms are:

  • Depth-First Traversal: In this type of traversal, we visit each node of the tree starting from the root and explore as far as possible along each branch before backtracking.
  • Breadth-First Traversal: In this type of traversal, we visit all the nodes at each level of the tree before moving to the next level.

The choice of traversal algorithm depends on the specific application requirements and the problem being solved using the tree structure.


Trees are an essential data structure used in various applications, including database systems, file systems, hierarchical organization structures, and more. Understanding how trees are stored in data structures is crucial for efficient manipulation and retrieval of data within a hierarchical context.

Whether you choose to use an array representation or a linked representation depends on factors such as memory efficiency, ease of traversal, and specific application requirements. Tree traversal algorithms further enhance our ability to work with trees effectively.

In summary, trees provide an organized way to represent hierarchical relationships between elements and are a fundamental concept in computer science and programming.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy