Solution: Use Alternative Tree Models

Let's get introduced to the alternative tree models for the Naive Trees antipattern.

The alternative tree models for an Adjacency List are discussed below.

Alternate tree models

There are several alternatives to the Adjacency List model of storing hierarchical data. These are:

  • Path Enumeration
  • Nested Sets
  • Closure Table

Path Enumeration

Path Enumeration is used to manage hierarchical data in SQL. It is simple to create and maintain a tree in Path Enumeration, but it can be slower than Adjacency List, due to its use of the LIKE closure.

Nested Sets

Nested Sets are used to manipulate hierarchical data in SQL. This method is faster as compared to Path Enumeration. It uses a unique way to represent the data. The data is represented in a hierarchy using nsleft and nsright with each node.

Closure Tables

Closure Tables are used to store the data. It uses an elegant way to query hierarchical data in SQL. This method creates a new table which stores the information of the ancestors and the descendants.

Explanation of alternate tree models

Examples of these designs in use are given in the following three lessons. In each of these lessons, we will solve the scenario provided earlier, storing and querying a tree-like collection of comments. All of these three alternate tree models are explained in detail in the upcoming lessons.

These solutions take some getting used to. They may seem more complex than Adjacency List at first, but they make some tree operations easier than the Adjacency List design. If our application needs to perform any of these operations, then these alternate designs are a better choice than Adjacency List.

Get hands-on with 1200+ tech skills courses.