Created
March 13, 2019 17:25
-
-
Save joxer/0b64e0b6b6572aad644ba0169a7c8079 to your computer and use it in GitHub Desktop.
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
import math | |
class Node: | |
def __init__(self, value, left, right): | |
self.value = value | |
self.left = left | |
self.right = right | |
def __str__(self): | |
return str([str(self.value), str(self.left) if self.left != None else None, str(self.right) if self.right != None else None ]) | |
array = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] | |
power = int(math.log( len(array)/ math.log(2))) | |
def build_tree(array, power, idx): | |
if(idx-1 > len(array)-1): | |
return build_tree(array, power-1,idx- pow(2,power-1)) | |
print(array,power,idx-1) | |
root = Node(array[idx-1], None, None) | |
if(power > 0): | |
root.left = build_tree(array, power-1, idx - pow(2, power-1)) | |
root.right = build_tree(array, power-1, idx + pow(2, power-1)) | |
return root | |
print(build_tree(array, power+1 , pow(2,power+1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment