> For the complete documentation index, see [llms.txt](https://richardweiyang-2.gitbook.io/kernel-exploring/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://richardweiyang-2.gitbook.io/kernel-exploring/00-index-3/07-interval_tree.md).

# Interval Tree

Interval Tree(区段树)在内存管理中有用到，比如rmap和vma中。

一直觉得有点神秘，今天来看一下。

## 是附带属性的红黑树

本质上区段树是一个红黑树，在红黑树的基础上增加了两个信息：

* leftmost: 保存了整个树中最左叶子
* subtree: 每个节点增加当前子树内区间最大值

## 优势

和红黑树相比，区段树的优势在与查找某区间内有较差的节点时，可以借助新增的这两个信息加速查找。

具体参考区段树提供的api：

* interval\_tree\_iter\_first()
* interval\_tree\_iter\_next()

## 测试代码

内核中有相关的测试代码，lib/interval\_tree\_test.c。这样可以看出简单的使用方法。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://richardweiyang-2.gitbook.io/kernel-exploring/00-index-3/07-interval_tree.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
