# Merkle Trees in Action

Merkle Trees come to life when you see them built step by step. In the animation below, we start with **eight different strings** of varying lengths, each passed through the **SHA-256 hash function** to produce **leaf nodes** of identical length.

<figure><img src="/files/KqzioYnEZkx1hFWVxt5c" alt=""><figcaption></figcaption></figure>

Even a **tiny change** in a string — like a single punctuation mark or capital letter — results in a completely **different hash output**. This shows how sensitive hash functions are to input changes.

With only eight strings, it’s hard to grasp just how unlikely **hash collisions** are, but mathematically, the odds are less than **8 in a million-million-millions**. That means it’s practically impossible for two different strings to produce the same hash.

Any attempt to **modify or replace** one of the original data strings instantly changes the corresponding hash value and, as a result, the entire branch leading to the **Merkle Root**.

***

#### **Constructing the Merkle Tree**

In the next animation, you can see how all eight strings are **combined to form a complete Merkle Tree**.\
Each layer above is created by **concatenating and hashing** the values of the layer below, until a single **Merkle Root** represents the entire dataset.

It’s important to note that **order matters**. If two hashes, say `1-2`, are concatenated in reverse order (`2-1`), the resulting hash will be **completely different**. This ensures that every dataset produces a **unique** Merkle Root.

***

#### **Adding New Elements**

Now, watch what happens when a **ninth data element** is added to the set.\
Because there are now an **uneven number of leaves**, the new node must be handled differently to keep the tree balanced.

<figure><img src="/files/rb4x2drJSMuWHSIpA6bN" alt=""><figcaption></figcaption></figure>

Notice how the **existing interior node values** from the original eight elements **don’t change**. Only the right-hand branch on the upper layer adjusts to include the new leaf.

This branch is concatenated with the **former Merkle Root**, generating a **new Merkle Root** on an additional (fourth) layer of the tree.

***

#### **Handling Even and Odd Data Sets**

When a **tenth element** is added, the **first layer** becomes even again — but the **second layer** now has five child nodes.

Four of these remain unchanged from the previous nine-element tree, while the fifth is newly generated.

As the dataset grows:

* A tree with **16 elements** forms two perfectly **balanced branches**.
* Adding a **17th element** again makes the structure uneven, turning the **former Merkle Root** into the left-hand branch of the new tree.

<figure><img src="/files/Ao4IQ3fzrYy406N1FrLg" alt=""><figcaption></figcaption></figure>

***

#### **Key Takeaway**

Merkle Trees dynamically **scale and adapt** as new data is added.\
Their layered, hash-based structure ensures that:

* Every change, even a single bit, is instantly reflected in the Merkle Root.
* The tree remains efficient and secure, no matter how large the dataset grows.
* Only minimal recalculations are needed when new data enters the system.

This adaptability makes Merkle Trees an essential part of Bitcoin’s design — allowing it to handle vast amounts of transaction data **securely and efficiently**.


---

# Agent Instructions: 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:

```
GET https://hub.bsvblockchain.org/higher-learning/bsv-academy/bitcoin-primitives-merkle-trees/the-merkle-tree/merkle-trees-in-action.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
