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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Persistent Segment Tree | Set 1 (Introduction)
- ScapeGoat Tree | Set 1 (Introduction and Insertion)
- Palindromic Tree | Introduction & Implementation
- BK-Tree | Introduction & Implementation
- Binary Tree | Set 1 (Introduction)
- Convert a Binary Tree into its Mirror Tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Check if a binary tree is subtree of another binary tree | Set 1
- Convert a given tree to its Sum Tree
- Binary Tree to Binary Search Tree Conversion
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Check if a binary tree is subtree of another binary tree | Set 2
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Check whether a binary tree is a full binary tree or not
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Minimum swap required to convert binary tree to binary search tree
- Binary Tree | Set 3 (Types of Binary Tree)

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.