Merkle Tree is a data structure where every non-leaf node contains the hash of the labels of its child nodes, and the leaves have their own values (or key/value pair) hashed. Because of this characteristic, Merkle Trees are used to verify that two or more parties have the same data without exchanging the entire data collection. For more information about Merkle Trees and other use cases you can visit its Wikipedia article: https://en.wikipedia.org/wiki/Merkle_tree
This module implements a binary Merkle Tree that is built based on a list of
{Key, Value}
pairs. The tree is sorted but might not be balanced.
┌───────────────┐ │ Root │ ┌─────────────────│Hash(AA1 + BB2)│───────────────┐ │ └───────────────┘ │ │ │ │ │ │ │ │ │ ┌─────────────┐ ┌─────────────┐ │ AA1 │ │ BB2 │ ┌────│Hash(A1 + B1)│──────┐ ┌──│Hash(C1 + D1)│────────┐ │ └─────────────┘ │ │ └─────────────┘ │ │ │ │ │ │ │ │ │ ┌───────────┐ ┌───────────┐ ┌───────────┐ ┌───────────┐ │ A1 │ │ B1 │ │ C1 │ │ D1 │ ┌─│Hash(A + B)│─┐ ┌─│Hash(C + D)│─┐ ┌─│Hash(E + F)│─┐ ┌─│Hash(G + H)│─┐ │ └───────────┘ │ │ └───────────┘ │ │ └───────────┘ │ │ └───────────┘ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ │ A │ │ B │ │ C │ │ D │ │ E │ │ F │ │ G │ │ H │ │Hash(A)│ │Hash(B)│ │Hash(C)│ │Hash(D)│ │Hash(E)│ │Hash(F)│ │Hash(G)│ │Hash(H)│ └───────┘ └───────┘ └───────┘ └───────┘ └───────┘ └───────┘ └───────┘ └───────┘Every leaf node will have its
left
and right
pointers pointing to
nil
, and it will contain a hash based on the key and value. The inner
nodes will point to their respective leaf nodes children, and its hash will
be Hash(LeftHash + RightHash)
.
hash() = binary()
inner() = #inner{key = key() | undefined, hash = hash(), height = non_neg_integer(), min_key = key(), max_key = key(), left = tree(), right = tree()}
key() = binary()
tree() = inner() | nil
value() = binary()
build/1 | Creates a tree from a list of {Key, Value} pairs. |
diff/2 | Returns the list of Key that are different between the given trees. |
keys/1 | Returns a sorted list of all keys from the given tree. |
Creates a tree from a list of {Key, Value}
pairs.
Returns the list of Key
that are different between the given trees.
Returns a sorted list of all keys from the given tree.
Generated by EDoc, Sep 2 2016, 12:40:03.