liveBook · Manning

Tree

As explained in Retrieval Augmented Generation, The Foundational Ideas

An entity tree is a hierarchical data structure whose nodes represent entities and their relationships across levels.

A tree organizes entities or text into nested levels, replacing a flat list with relationships between leaves, intermediate summaries, and a root. In hierarchical retrieval, this structure preserves context across granular and broad questions. section 10.0 section 10.2

A recursive retrieval tree is built from the bottom up:

  • Leaf nodes retain original chunks for specific factual questions. section 10.2
  • Cluster nodes group semantically similar chunks and store LLM-written summaries. section 10.2
  • Higher layers repeatedly group and summarize lower layers until a root summarizes the corpus. section 10.2

Choosing the retrieval level

A query asking for a contract's specific termination fee can match a leaf chunk, while a query about broad termination conditions can match a summary node. This reduces incorrect specificity and incomplete answers caused by flat retrieval. section 10.0 section 10.2

Listing 10.1 RaptorNode.
import uuid
from typing import List, Optional, Set, Dict, Any
class RaptorNode:
    """A single node in the RAPTOR tree."""
    def __init__(self, text: str, level: int,
                 embedding: List[float],
                 children: Optional[Set[str]] = None,
                 metadata: Optional[Dict[str, Any]] = None):
        self.id = str(uuid.uuid4())  #A
        self.text = text  #B
        self.level = level  #C
        self.embedding = embedding  #D
        self.children = children if children else set()  #E
        self.parents = set()  #F
        self.metadata = metadata if metadata else {}  #G
    def add_child(self, child_id: str):
        self.children.add(child_id)
    def add_parent(self, parent_id: str):
        self.parents.add(parent_id)
    def is_leaf(self) -> bool:
        return self.level == 0 and len(self.children) == 0

Use the RaptorNode listing to inspect node text, hierarchy level, embeddings, children, parents, and leaf detection. Soft clustering can give one node multiple parents, making the implementation a directed acyclic graph rather than a strict tree. section 10.4.1

How Retrieval Augmented Generation, The Foundational Ideas covers it

How deep it goes
Explains recursive construction, node roles, and retrieval behavior, then extends the strict-tree model to a DAG implementation.
Examples
Uses textbook and museum hierarchies, contract queries, and a Python RaptorNode implementation.
What it assumes
Assumes basic text chunks and embeddings while defining leaves, summaries, clustering, parents, and children.
What you can do after
Builds a recursive index that routes specific and broad questions to suitable tree levels.
How it connects
Connects hierarchical data structures with semantic similarity, clustering, summarization, embeddings, retrieval, and directed acyclic graphs.

Also explained in

Compare how 14 books explain tree →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Retrieval Augmented Generation, The Foundational Ideas. The section links point to the parts of the book it draws on.