<h3>When the number of transactions is not a power of two, how does the Bitcoin client represent the transactions?</h3>
<p>Each time around, it hashes each transaction with the one next to it, until there's only one left.<br />
<img src="https://i.sstatic.net/PRy1o.png" alt="2 transactions merkle tree example" /><br />
If there's an odd number of transactions, the last one is hashed again.
<img src="https://i.sstatic.net/T50KF.png" alt="3 transactions merkle tree example" /><br />
In this example, we start out with 11, then 6, then 3, then 2, then 1.
<img src="https://i.sstatic.net/FxSw0.png" alt="11 transactions merkle tree example" /></p>
<h3>What would happen if you made a differently-shaped merkle-tree?</h3>
<p>When sending blocks around, there's no way to specify the shape of the merkle tree. If it's different, the standard client will just reject it.</p>

CC BY-SA 3.0
2014-09-01T09:21:57.760Z
Small correction: when there are an odd number of elements in a level, the last one is hashed after appending it to itself. So usually hash6 of one level is H(hash12+hash13) of the level below, but it can be H(hash12+hash12) if hash12 is the last one.
Pieter Wuille
CC BY-SA 3.0
2014-09-01T15:15:46.273Z
@PieterWuille Huh. I hadn't noticed that. +1
Nick ODell
CC BY-SA 3.0
2014-09-01T03:08:16.340Z
2014-09-01T03:08:16.340Z
2020-06-16T11:10:03.350Z
Nick ODell
<p>The Bitcoin client builds a merkle tree to represent the transactions, then includes the root of that tree in the block header.<br>
<img src="https://i.sstatic.net/kYAwv.png" alt="merkle tree from bitcoin paper"><br>
But usually, the number of transactions is not a power of two. When that happens, how does the Bitcoin client represent the transactions in the tree?</p>

<p>If I wrote a custom mining client, which built a tree with a different "shape," (example below) what would happen?</p>

<p><img src="https://i.sstatic.net/cQzXb.png" alt="custom merkle tree"></p>

<block><merkle-tree>
What shape of merkle tree does the Bitcoin client build?
Nick ODell
# What shape of merkle tree does the Bitcoin client build?

(Asked by: username_0 on 2014-09-01)

The Bitcoin client builds a merkle tree to represent the transactions, then includes the root of that tree in the block header.  
![merkle tree from bitcoin paper](https://i.sstatic.net/kYAwv.png)  
But usually, the number of transactions is not a power of two. When that happens, how does the Bitcoin client represent the transactions in the tree?

If I wrote a custom mining client, which built a tree with a different "shape," (example below) what would happen?

![custom merkle tree](https://i.sstatic.net/cQzXb.png)

**Tags:** block, merkle-tree


---

## 1 answers

### Answer by username_0 on 2014-09-01. Score: 3 | Accepted answer

### When the number of transactions is not a power of two, how does the Bitcoin client represent the transactions?

Each time around, it hashes each transaction with the one next to it, until there's only one left.  
![2 transactions merkle tree example](https://i.sstatic.net/PRy1o.png)  
If there's an odd number of transactions, the last one is hashed again.
![3 transactions merkle tree example](https://i.sstatic.net/T50KF.png)  
In this example, we start out with 11, then 6, then 3, then 2, then 1.
![11 transactions merkle tree example](https://i.sstatic.net/FxSw0.png)

### What would happen if you made a differently-shaped merkle-tree?

When sending blocks around, there's no way to specify the shape of the merkle tree. If it's different, the standard client will just reject it.

> **Comments:**
> **username_1**: Small correction: when there are an odd number of elements in a level, the last one is hashed after appending it to itself. So usually hash6 of one level is H(hash12+hash13) of the level below, but it can be H(hash12+hash12) if hash12 is the last one.  
> **username_0**: @PieterWuille Huh. I hadn't noticed that. +1  

---

