A Merle tree is a data structure that stores the hash of the combined children in each parent. It is also known as a hash tree.
Merkle root servers as a summary for all data.
It starts with the hash of leaf nodes. Then we combine the hash of the leaf node to get the hash of the parent nodes.
When the last leaf node does have a pair, i.e. when we have an old number of leaf nodes.
This can be handled in many ways. Duplication of the leaf is the most widely used approach.
Duplication: The last leaf node makes pair with itself.
It is an efficient and secure way to verify the data.
Take a scenario where Computer A send data to computer B.
Case 1: When we don't have hash or Merkle tree
computer A sends a file to computer B. How will computer B know whether it got all files?
One way is to send the entire file again and compare both the copies, but what if the file size is huge?.
Case 2: When we have hash
Computer A will send the file plus the hash of that file. On receiving the file, Computer B will also calculate the hash of that file. If both hashes match, computer B received the correct file.
What if hashes are different? We know files are not correct but don't know what is wrong. We need to send the entire file again.
Case 3: When we have Merkle root.
Like case 2: computer A will send the file with the Merkle root, and computer B will calculate the Merkle root.
If it matched, it means the files are correct. If not matched, computer B will request the entire Merkle tree and compare node by node. In this way, computer B will be able to identify which part of the file is wrong and request that portion of file only. This saves our network bandwidth.
There is no central authority in the Blockchain network. Each node has the responsibility to ensure the blocks are valid.
Merkle Trees are a solution to this issue