Implementing Tree Data Structures in Databases: A Comparative Analysis of Adjacency List, Materialized Path, and Nested Set Models

Dec 04, 2025 · Programming · 12 views · 7.8

Keywords: Tree Data Structure | Database Design | Adjacency List Model | Materialized Path Model | Nested Set Model

Abstract: This paper comprehensively examines three core models for implementing customizable tree data structures in relational databases: the adjacency list model, materialized path model, and nested set model. By analyzing each model's data storage mechanisms, query efficiency, structural update characteristics, and application scenarios, along with detailed SQL code examples, it provides guidance for selecting the appropriate model based on business needs such as organizational management or classification systems. Key considerations include the frequency of structural changes, read-write load patterns, and specific query requirements, with performance comparisons for operations like finding descendants, ancestors, and hierarchical statistics.

Introduction

Storing and querying tree data structures (also known as hierarchical structures) in relational databases is a classic yet complex challenge. These structures are widely used in organizational charts, product categorization systems, comment threads, file directories, and more. The user's requirement to "implement a customizable tree structure with an unknown number of levels" essentially demands a database model that can flexibly adapt to dynamically changing hierarchical depths. This paper systematically analyzes three mainstream implementation approaches: the adjacency list model, materialized path model, and nested set model, illustrating their core mechanisms through code examples.

Adjacency List Model

The adjacency list model is the most intuitive approach, representing parent-child relationships by adding a foreign key column that references the same table. For instance, in a table storing organizational structure, it can be designed as follows:

CREATE TABLE organizational_structure (
    node_id INT PRIMARY KEY,
    node_name VARCHAR(100),
    parent_id INT,
    FOREIGN KEY (parent_id) REFERENCES organizational_structure(node_id)
);

The advantages of this model include simplicity and efficient insert/delete operations, which only require updating a single record. However, its drawbacks emerge when querying entire subtrees or calculating node depths, often necessitating recursive queries that may cause performance bottlenecks in some database systems. For example, retrieving a node and all its descendants requires a recursive CTE (Common Table Expression):

WITH RECURSIVE subtree AS (
    SELECT node_id, node_name, parent_id
    FROM organizational_structure
    WHERE node_id = 1  -- Assuming root node ID is 1
    UNION ALL
    SELECT o.node_id, o.node_name, o.parent_id
    FROM organizational_structure o
    INNER JOIN subtree s ON o.parent_id = s.node_id
)
SELECT * FROM subtree;

As noted in Answer 1, the adjacency list model suits scenarios with frequent structural changes, but it is crucial to decouple business entities (e.g., employees) from the tree structure itself to avoid unnecessary updates due to entity modifications.

Materialized Path Model

The materialized path model encodes hierarchical information by storing the full path from the root to each node, typically using a delimiter (e.g., slash) to concatenate node IDs. A sample table structure is:

CREATE TABLE materialized_path_tree (
    node_id INT PRIMARY KEY,
    node_name VARCHAR(100),
    path VARCHAR(255)  -- e.g., "1/3/7" represents node 7 under node 3, which is under root node 1
);

Querying all descendants of a node becomes highly efficient with the LIKE operator matching path prefixes:

SELECT * FROM materialized_path_tree
WHERE path LIKE '1/3/%';  -- Find all descendants of node 3

This model excels in read operations, particularly for applications that frequently query subtrees or hierarchical levels. However, inserting or moving nodes requires updating the path fields of affected nodes, which can incur write overhead. Resources mentioned in Answer 2 elaborate on implementing this model in MySQL.

Nested Set Model

The nested set model uses left and right values to encode the tree structure, where each node corresponds to an interval (lft, rgt), and child intervals are entirely contained within parent intervals. The table structure is designed as:

CREATE TABLE nested_sets_tree (
    node_id INT PRIMARY KEY,
    node_name VARCHAR(100),
    lft INT NOT NULL,
    rgt INT NOT NULL
);

This representation enables highly efficient queries for subtrees, ancestors, or node counts without recursion. For example, to find a node and all its descendants:

SELECT * FROM nested_sets_tree
WHERE lft BETWEEN (SELECT lft FROM nested_sets_tree WHERE node_id = 3)
AND (SELECT rgt FROM nested_sets_tree WHERE node_id = 3);

The downside of the nested set model is that inserting, deleting, or moving nodes requires adjusting the left and right values of many records, leading to higher write costs. Thus, as emphasized in Answer 1, it is more suitable for read-intensive scenarios. Works by Joe Celko and Itzik Ben-Gann provide in-depth theoretical support for this model.

Key Considerations for Model Selection

Based on the summary in Answer 1, selecting a database implementation for tree data structures should focus on evaluating the following three dimensions:

  1. Frequency of Structural Changes: If the tree's topology changes often (e.g., frequent additions, deletions, or moves), the adjacency list model is advantageous due to its simple update logic. However, avoid over-coupling business logic with structure; for example, in organizational charts, separate position nodes from employee information to maintain structural stability.
  2. Read-Write Load Characteristics: For applications with frequent write operations, the adjacency list or materialized path models may be more appropriate; for read-intensive queries (e.g., regularly retrieving entire subtrees), the nested set model offers better performance. Answer 2 points out that the adjacency list model can be inefficient for building whole trees, while the nested set model is flexible and efficient.
  3. Types of Query Requirements: Clarify the information needed from the structure, such as finding nodes and all their children, counting child nodes under specific conditions, or traversing ancestor paths. The nested set model excels at range queries, the materialized path model facilitates path matching, and the adjacency list model suits recursive traversals.

In practice, combining multiple models or using database-specific extensions (e.g., SQL Server's hierarchyid data type) can help balance requirements.

Conclusion

When implementing customizable tree data structures, there is no single "best" solution; instead, it requires trade-offs based on specific application contexts. The adjacency list model, with its simplicity, suits dynamically changing structures; the materialized path model performs well in path-based queries; and the nested set model provides efficient support for read operations. Developers should refer to resources in Answer 1 and Answer 2 to deeply understand each model's principles and make informed decisions through performance testing. Looking ahead, innovations in technologies like graph databases may offer new approaches for storing and querying tree structures.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.