> For the complete documentation index, see [llms.txt](https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/16.-bsts/16.1-binary-search-trees.md).

# 16.1 Binary Search Trees

Linked Lists are great, but it takes a long time to search for an item, even if the list is sorted! What if the item is at the end of the list? That would take linear time!

We know that for an array, we can use binary search to find an element faster. Specifically, in $$\log n$$ time. For a short explanation of binary search, check out this [link](https://www.geeksforgeeks.org/binary-search/).

In binary search, we know the list is sorted, so we can use this information to narrow our search. First, we look at the middle element. If it is bigger than the element we are searching for, we look to the left of it. If it is smaller than the element we are searching for, we look to the right. We then look at the middle element of the respective halves and repeat the process until we find the element we are looking for (or don't find it because the list doesn't contain it).

But how do we run binary search for a linked list? We would have to traverse all the way to the middle in order to check the element there, which takes linear time just on its own!

One optimization we can implement is to have a reference to the middle node. This way, we can get to the middle in constant time. Then, if we flip the nodes' pointers, which allows us to traverse to both the left and right halves, we can decrease our runtime by half!

<figure><img src="https://2512475561-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBGDh1mPw1Aw5u4IlOFCn%2Fuploads%2Fgit-blob-275bd1b28190ccbb8e10c5c0f110f2e444e6b17f%2Fimage%20(57).png?alt=media" alt=""><figcaption><p>A linked list with a middle pointer</p></figcaption></figure>

But we can further optimize by adding pointers to the middle of each recursive half like so. Now, if you stretch this structure vertically, you will see a tree! This specific tree is called a **binary tree** because each juncture splits in 2.

<figure><img src="https://2512475561-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBGDh1mPw1Aw5u4IlOFCn%2Fuploads%2Fgit-blob-5d67e78ca5faad981cae7bf9f308570ca7087f78%2Fimage%20(11).png?alt=media" alt=""><figcaption><p>A linked list with recursive middle pointers is a binary tree!</p></figcaption></figure>

{% embed url="<https://www.youtube.com/watch?v=AcRKQOe0zYg>" %}


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/16.-bsts/16.1-binary-search-trees.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
