What Is a Merkle Tree?
A Merkle tree (also called a binary hash tree) is a data structure where every leaf node is a hash of data, and every non-leaf node is a hash of its children. The tree ultimately produces a single hash at the top called the Merkle Root.
Named after Ralph Merkle, who patented the concept in 1979, Merkle trees are fundamental to blockchain technology. They allow efficient, secure verification of large data sets.
How Merkle Trees Work in Blockchain
Building the Tree
Imagine a block contains 4 transactions: TX₁, TX₂, TX₃, TX₄
Step 1: Hash each transaction (leaf nodes)
H₁ = SHA-256(TX₁)
H₂ = SHA-256(TX₂)
H₃ = SHA-256(TX₃)
H₄ = SHA-256(TX₄)Step 2: Hash pairs together (internal nodes)
H₁₂ = SHA-256(H₁ + H₂)
H₃₄ = SHA-256(H₃ + H₄)Step 3: Hash the final pair (Merkle Root)
Merkle Root = SHA-256(H₁₂ + H₃₄)Visual Structure
Merkle Root
/ \
H₁₂ H₃₄
/ \ / \
H₁ H₂ H₃ H₄
| | | |
TX₁ TX₂ TX₃ TX₄Why Merkle Trees Matter
1. Efficient Verification (Merkle Proofs)
To prove TX₃ is in a block, you don't need all 4 transactions. You only need:
- H₃ (the transaction hash)
- H₄ (its sibling — to compute H₃₄)
- H₁₂ (the other branch — to compute the root)
That's just 2 hashes instead of 4 transactions. For a block with 2,000 transactions, a Merkle proof requires only ~11 hashes (log₂ 2000 ≈ 11). This is the power of logarithmic scaling.
2. Simplified Payment Verification (SPV)
SPV allows lightweight clients (like mobile wallets) to verify transactions without downloading the entire blockchain:
- Download only block headers (80 bytes each, not full blocks)
- Request a Merkle proof for your transaction from a full node
- Verify the proof against the Merkle root in the block header
- Confirm the block header is part of the longest chain
This is how Bitcoin mobile wallets work — they don't need 500+ GB of blockchain data.
3. Tamper Detection
If any transaction is modified, its hash changes, which propagates up through the tree, changing the Merkle root. Since the Merkle root is included in the block header (which is included in the next block's hash), tampering with any transaction invalidates the entire chain from that point forward.
Merkle Trees in Bitcoin vs Ethereum
Bitcoin
- Uses a simple Merkle tree of transactions
- Merkle root stored in 80-byte block header
- Enables SPV for lightweight clients
- Witness data (SegWit) has its own Merkle tree
Ethereum
- Uses three Merkle Patricia Tries:
1. State trie: All account balances and contract storage
2. Transaction trie: All transactions in a block
3. Receipt trie: Transaction execution results (logs, gas used)
- More complex but enables richer state proofs
Edge Cases
Odd Number of Transactions
If there's an odd number of leaves at any level, the last hash is duplicated to make a pair:
3 transactions:
Root
/ \
H₁₂ H₃₃ ← H₃ is paired with a copy of itself
/ \ / \
H₁ H₂ H₃ H₃Empty Blocks
A block with only the coinbase (mining reward) transaction has a Merkle tree with a single leaf. The Merkle root equals the coinbase transaction hash.
Merkle Trees Beyond Blockchain
Merkle trees are used throughout computer science:
- Git: Every commit contains a Merkle tree of file hashes
- IPFS: Content addressing uses Merkle DAGs
- Certificate Transparency: Logs of SSL certificates
- Amazon DynamoDB: Anti-entropy protocol for data sync
- ZK proofs: Merkle tree inclusion proofs in zero-knowledge circuits
Build and Verify Merkle Trees
Understanding Merkle trees conceptually is good. Building them yourself is better. The Block Structure module inside the ZeroToBlock Bitcoin Proof of Work course lets you construct Merkle trees interactively, tamper with transactions to see how hashes propagate, and generate Merkle proofs — all in your browser. Want the bigger picture first? Learn Bitcoin, blockchain and Proof of Work interactively.