Introduction to Merkle Tree

Merkle tree also known as hash tree is a data structure used for data verification and synchronization.
It is a tree data structure where each non-leaf node is a hash of it’s child nodes. All the leaf nodes are at the same depth and are as far left as possible.

It maintains data integrity and uses hash functions for this purpose.

Hash Functions:
So before understanding how Merkle trees work, we need to understand how hash functions work.
A hash function maps an input to a fixed output and this output is called hash.
The output is unique for every input and this enables fingerprinting of data.
So, huge amounts of data can be easily identified through their hash.

This is a binary merkel tree, the top hash is a hash of the entire tree.

  • This structure of the tree allows efficient mapping of huge data and small changes made to the data can be easily identified.
  • If we want to know where data change has occurred then we can check if data is consistent with root hash and we will not have to traverse the whole structure but only a small part of the structure.
  • The root hash is used as the fingerprint for the entire data.

For a Binary Merkel tree

Operation Complexity
Space O(n)
Searching O(logn)
Traversal O(n)
Insertion O(logn)
Deletion O(logn)
Synchronization O(logn)

Applications:

  • Merkle trees are useful in distributed systems where same data should exist in multiple places.
  • Merkle trees can be used to check inconsistencies.
  • Apache Cassandra uses Merkle trees to detect inconsistencies between replicas of entire databases.
  • It is used in bitcoin and blockchain.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.