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.

## Recommended Posts:

- Binary Tree | Set 1 (Introduction)
- BK-Tree | Introduction & Implementation
- Palindromic Tree | Introduction & Implementation
- Persistent Segment Tree | Set 1 (Introduction)
- ScapeGoat Tree | Set 1 (Introduction and Insertion)
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Convert a given Binary tree to a tree that holds Logical AND property
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Sub-tree with minimum color difference in a 2-coloured tree
- Convert a Binary Tree into its Mirror Tree
- Minimum swap required to convert binary tree to binary search tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach

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.