---
title: 'CST: Recursive Query Tree Generation'
url: https://www.emergentmind.com/topics/context-split-tree-cst
type: topic
---

# CST: Recursive Query Tree Generation

The Context-Split-Tree (CST) is a recursive, LLM-driven algorithm for constructing binary trees over a textual context to generate multi-granularity, context-driven query–context pairs. Central to the AugCon framework, CST enables automated large-scale generation of supervised fine-tuning (SFT) data for large language models (LLMs), spanning a range of granularities from macro-level to fine-grained queries. By integrating LLM-based context splitting and question generation at each node, CST achieves structural coverage of all meaningful context scales and forms the foundation for downstream SFT data quality improvements [2405.16579].

## 1. Formal Structure and Definition

Let $C$ be an initial textual context, segmented into atomic sentences $S_1,\,\ldots,\,S_n$. CST constructs a binary tree $T = (V, E)$, where each node $v \in V$ comprises:
- a contiguous sub-context $C_v \subseteq C$
- a query $q_v$ tailored to $C_v$’s semantic granularity

The split operation $\text{Split}(C) = (C_1, C_2, q)$ invokes an LLM with a specially designed prompt to:
- Generate a question $q$ directly answerable from $C$
- Partition $C$ into semantically coherent, minimally overlapping sub-contexts $C_1$ and $C_2$

For $C$ of $n$ sentences, index $i \,(1 \leq i < n)$ is selected. Sub-contexts $C_1 = S_1,\ldots,S_i$ and $C_2 = S_{i+1},\ldots,S_n$ are formed. The process recurses on $C_1$ and $C_2$ while $|C| \geq \lambda$ (a tunable minimum-length threshold) and the split is non-degenerate ($|C_1|, |C_2| < |C|$), terminating with leaf nodes when conditions fail. Each node’s depth coheres with its granularity: root-level (macro), intermediate (conceptual), and deeper (detail).

A key structural property is that for $C$ of $n$ sentences, the CST contains exactly $2n - 1$ nodes, each corresponding to a query–context pair:
\[
|V| = 2n - 1
\]
This determinism ensures an exhaustive yet non-redundant traversal of granularity.

## 2. Recursive Construction and Algorithmic Workflow

The CST algorithm proceeds recursively as outlined below:
```python
def ContextSplitTree(C, Data):
    if len(C) < λ:
        return
    (C1, C2, q) = LLM(I_CST, C)
    Data.append((C, q))
    if len(C1) >= len(C) or len(C2) >= len(C):
        return
    ContextSplitTree(C1, Data)
    ContextSplitTree(C2, Data)

# Main orchestration
Data = []
for C in corpus:
    ContextSplitTree(C, Data)
```
The process is interleaved with a filtering mechanism to enforce an upper bound $N$ on queries per context. If fewer than $N$ high-quality, diverse queries survive post-scoring, CST is re-invoked until the requirement is met. This approach guarantees coverage at multiple context scales.

## 3. Complexity and Resource Considerations

Let $n$ be the sentence count of initial context $C$. In the degenerate worst case (each split partitions off only one sentence), tree depth is $O(n)$, and node count remains $|V| = 2n - 1$. For balanced splits, depth is $O(\log n)$; node count is still linear $O(n)$. Each node entails one LLM inference for question and sub-context generation, incurring total cost:
- Time: $O(n)$ times the cost of a single LLM inference with context size at most $|C|$
- Space: $O(n)$ storage for (context, query) pairs

Parameter $\lambda$ governs granularity, with lower $\lambda$ leading to deeper trees and more fine-grained queries. *This suggests that tuning $\lambda$ enables precise control over output diversity and data volume.*

## 4. CST in Data Filtering and Contrastive Scoring

Upon generating a candidate pool $Q_{\text{All}}$ of (context, query) pairs per context, CST integrates with a lightweight contrastive-learning-based scorer $\text{Sc}(C, q)$. This scorer is trained as follows:
- Positive samples $q^+$: from CST+few-shot prompt
- Negative samples $q^-$: from prompts with degraded instruction/fewer shots

Contrastive loss:
\[
\mathcal{L} = - \mathbb{E}_{(C, q^+, q^-)} \Big[\log\,\sigma\big(\text{Sc}(C, q^+) - \text{Sc}(C, q^-)\big)\Big]
\]
with $\sigma$ the sigmoid function.

During inference, all $(C, q)$ are scored and sorted. The top-ranked queries are selected, skipping any whose ROUGE-L F1 with previously selected queries exceeds $0.7$ (to ensure diversity), until $N$ slots are filled. If insufficient diverse queries remain, CST is rerun.

## 5. Illustrative Example on a Toy Text

For the passage: "The profits of the contemporary global value chains (GVC) form a V-shape, also known as the ‘smile curve’. At one end are R & D and design; at the other end are services and marketing; processing sits in the middle. Profits at the ends are 20–25%; in the middle only 5%."

CST constructs the following (abbreviated) tree:
- **Node 1 (whole passage):** "Why do entrepreneurs worldwide strive to move up the value chain?"
  - **C₁ (first half):** "What are the key components of the contemporary global value chains?"
    - **C₁₁:** "What does the global value curve look like?" (leaf; no further split)
    - **C₁₂:** "What is the structure of the smile curve?"
      - **C₁₂₁:** "What lies in the middle of the smile curve?" (leaf)
  - **C₂ (second half):** Questions about low-profit-margin industries

In total, CST produces 8 binary-tree nodes and corresponding context–query pairs. The top 4 are retained post-filtering, covering macro to specific details.

## 6. Empirical Evaluation and Metrics

CST yields substantial improvements in SFT data quality and downstream model performance. Key empirical findings:
- **Human evaluation on DailyM test:**
  - Query Realism: 4.37 (CST) vs 4.05 (best prior)
  - Query Diversity: 4.68 (CST) vs 4.13
- **Automatic QA benchmarks (fine-tuning Llama3-70B):**

  | Benchmark         | Acc (CST, AugCon) | Acc (Context-Instruct) |
  |-------------------|-------------------|------------------------|
  | SQuAD1.1          | 0.336             | 0.314                  |
  | TriviaQA          | 0.849             | 0.825                  |
  | DROP              | 0.350             | 0.334                  |
  | WebGLM-QA BS      | 0.924             | 0.885                  |

- **Granularity distribution (CST vs Context-Instruct):**

  | Type        | CST (%) | Context-Instruct (%) |
  |-------------|---------|----------------------|
  | Detail      | 37.8    | 17.9                 |
  | Concept     | 35.3    | 63.4                 |
  | Macro       | 26.9    | 18.7                 |

- **Ablations (on TriviaQA):**
  - Removing CST: Accuracy drops from 0.849 to 0.793
  - Removing contrastive filter: 0.828
  - Removing fidelity module: 0.833

- **Compute-matched comparison (80 A100 GPU hours):**
  - AugCon wins over ETRC 64.5% and over Context-Instruct 60.3% under GPT-4 refereeing

These results demonstrate that CST’s recursive, tree-structured coverage of context enables generation of highly diverse, realistic, and fidelity-aligned SFT data, with significant impact on the quality of LLM fine-tuning outcomes [2405.16579].

Source: https://www.emergentmind.com/topics/context-split-tree-cst