-
-
Save abe4x4/ac3855af82df99c30e7c756eaab6624a to your computer and use it in GitHub Desktop.
Merkle Tree Implementation Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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