Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aenhsaihan/694513a4a93bec6fd56d1d712550321e to your computer and use it in GitHub Desktop.
Save aenhsaihan/694513a4a93bec6fd56d1d712550321e to your computer and use it in GitHub Desktop.
Merkle Tree Implementation Solution
def build_root(self, iterable):
collection = list(iterable)
assert(len(collection) != 0)
if len(collection) % 2 != 0:
collection.append(collection[-1])
collection = [self.__Node(self.digest(x)) for x in collection]
return self.__build_root(collection)
def __build_root(self, collection):
size = len(collection)
if size == 1:
return collection[0]
if size % 2 == 0:
collection.append(self.__Node(collection[-1].value, left=collection[-1].left, right=collection[-1].right)
next_level = []
for i in range(0, size – 1, 2):
digest = self.digest(collection[i].value + collection[i+1].value)
node = self.__Node(digest, left=collection[i], right=collection[i+1]
next_level.append(node)
return self.__build_root(next_level)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment