A Merkle tree, also called a hash tree, is a binary tree where each leaf holds the hash of one data block and each parent holds the hash of its two children. The single hash at the top, the Merkle root, acts as a fingerprint for the whole data set. Changing any block changes the root.
Let H be a cryptographic hash function and let d1,…,dn be data blocks. A Merkle tree is a rooted binary tree whose i-th leaf is labeled H(di). Each internal node with left child L and right child R is labeled H(L∥R), where ∥ denotes concatenation. The root's label is the Merkle root. A membership proof (audit path) for a leaf is the ordered list of sibling labels on the path from that leaf to the root, each marked left or right. The proof's length equals the leaf's depth. In a perfect binary tree with n=2k leaves, every proof has exactly k=log2n hashes. When n is not a power of 2, the count depends on how the tree is built. If each level duplicates its last node whenever it has an odd count, as Bitcoin does, every proof has ⌈log2n⌉ hashes for n≥2. In an unbalanced tree, different leaves can have proofs of different lengths.
Worked Example
Problem: Four records have leaf hashes h1=4, h2=9, h3=2, h4=7. Use the toy hash H(x,y)=(2x+y)mod23, which is not secure and is used only for practice. Build the Merkle root. Then verify that leaf 3 belongs to the tree using its proof: h4=7 (right sibling), then h12 (left sibling). Finally, show what happens if leaf 3 is altered to 5. Step 1: Hash each pair of leaves, then hash the two results to get the root.
h12=(2⋅4+9)mod23=17,h34=(2⋅2+7)mod23=11,root=(2⋅17+11)mod23=45mod23=22 Step 2: The verifier knows only the leaf value 2, the proof [7,17], and the trusted root 22. The verifier combines the leaf with each proof hash in order and puts each sibling on its marked side. With n=4 leaves, this takes log24=2 hashes.H(2,7)=(4+7)mod23=11,H(17,11)=(34+11)mod23=22✓ Step 3: Now repeat the check with the altered leaf value 5. The recomputed root no longer matches the trusted root.
H(5,7)=(10+7)mod23=17,H(17,17)=51mod23=5=22 Answer: The Merkle root is 22. The proof for leaf 3 recomputes 22, so the leaf is verified. The altered leaf produces 5 instead of 22, so the tampering is detected. The verifier never needed h1 or h2 individually. Why It Matters
Bitcoin block headers store only a Merkle root of the block's transactions. A lightweight wallet can confirm that a payment is in a block of 1,024 transactions by checking just log21024=10 sibling hashes instead of downloading every transaction. Certificate Transparency logs use the same idea, and Git uses a related hash-linked structure. Merkle trees appear in college courses on cryptography, distributed systems, and data structures. Common Mistakes
Mistake: Treating the combining step as order-free, so that H(L∥R) is assumed to equal H(R∥L). Correction: Concatenation is not commutative, and neither are real hash functions. In the example, H(2,7)=11 but H(7,2)=16. A proof must record whether each sibling sits on the left or the right, and the verifier must follow that order.