Blockchains · 4 min read
Merkel Tree: What is it, and how does it work?
A Merkle Tree is a data structure used to verify the integrity of large amounts of data efficiently.

A hash function is like a fingerprint machine. It takes any input, such as a file, a text, or a number, and produces a unique output, called a hash, that is much shorter than the input. The hash is like a fingerprint of the input. It is tough to find two different inputs that have the same hash. And it is impossible to get the original input from the hash.
0A6DF48A27EF7DB48B213DF3DBA84FAD1BB4FD9B47568DA1F570C067D9A4867F. If you change even one pixel in the file, the hash will be completely different, such asC69E60E932F3D0C6C2363B68301E202551C09123A71F639E6AB3BC8F847BE4AF. So you can use the hash to verify that the file is the same as before.But what if you have many files? Calculating and storing the hash for each file separately would be tedious. That's where Merkle trees come in handy.
A Merkle tree is like a family tree of hashes. You start with the hashes of each file as the leaves of the tree. Then you pair up the hashes and combine them to get new hashes. You repeat this process until you reach the top of the tree, where you have only one hash left. This hash is called the Merkle root.
But how do you verify a specific file? You don't need to download and check all the files in the tree. You only need to download and check a few hashes along the path from the file to the Merkle root. This path is called a Merkle proof.

0a6df4, its sibling's hash H B (ea12e7), their parent's sibling's hash H CD (b582ae), and the Merkle root H ABCD (7bd24f).Then you can calculate H AB by combining H A and H B and calculate H ABCD by combining H AB and H CD . If H ABCD matches the Merkle root, you can be sure that."cat.jpg" is correct.
You can use the same analogy to verify if the transaction present in the block is tempered by verifying the Merkle root instead of cat.jpg or dog.txt. It'll be a bunch of transactions at the leaf nodes.
Please note: I have truncated the hash value to 6 characters in the above example to make the diagram look clean.What if I only have three leaf nodes? How will I create a Merkle Tree?
This scenario can quickly be addressed by duplicating one node, thus making the total number of nodes four.
For example, suppose you have transactions. T A, T B, and T C. You can calculate their hashes. H A, H B, and H C. Then you can duplicate H C to get H C'. Afterward; you can pair up and combine the hashes to get H AB, H CC’, and H ABCC’. The Merkle root is H ABCC’.
Summing up, Merkle trees benefit blockchain and other distributed systems, where many computers must share and verify large amounts of data. By using Merkle trees, they can save bandwidth, storage, and computation time.
Here's a quick Java example to show how you can create your own Merkle tree:
Merkle Example JAVA CodeOutput of the above code:
d1da3503d679f032134b4330768d31e67813fcfe2824fceed93f8185a405bdf9Merkle Root:
1a898c7fee0e46647e55d5f9874f090e5ed76726acf39308527a0bba22a34b3eThank you for taking the time to read my article.
Part 2: Merkle Tree: A Blockchain Perspective
Blockchains · 8 minMerkle Tree: How Blockchain Ensures Data Integrity and SecurityIn a blockchain context, a Merkle tree is constructed for the transactions within a block.
Comments
Please be respectful.