build-a-merkle-tree / lesson-06.md
Lesson 06 · Building the tree

One level up

One level of the tree becomes the next by pairing adjacent hashes and hashing each pair into a parent. Today you build one level up, for an even number of nodes.

The goal

Turn a level with an even number of hashes into the next level by pairing and hashing adjacent hashes.

Start here - the target
TO DO
Scenario: An even level pairs up into the next level
Giventhe level [0x00063049, 0x6bfe63ee, 0x96a8ad3c, 0x68cf0725]
WhenpairUp combines each adjacent pair with HashNode
Thenit returns [0xebb8e925, 0x1cde9a86]
Andelement 0 is HashNode of the first two hashes and element 1 is HashNode of the last two
Background

Going up one level is a single idea repeated: take the level’s hashes two at a time, left then right, and hash each pair into a parent with HashNode. Four hashes become two; two would become one. The parents keep their left-to-right order, so the tree stays aligned with the data underneath.

Today assumes an even count so every hash has a partner - the four-leaf case lands on exactly two parents. What happens when a level has an odd number of nodes, and the last one is left without a partner, is the very next lesson. For now, pin the clean even case; it is the heart of the build loop.

Make it work
func pairUp(level []Hash) []Hash {
var next []Hash
for i := 0; i+1 < len(level); i += 2 {
next = append(next, HashNode(level[i], level[i+1]))
}
return next
}
CheckpointDONE
An even level collapses into the next level up by pairing and hashing. Commit and stop here.