I want to create a binary tree by iterating through a loop. I know how to write a very basic binary tree.
class Tree(object):
def __init__(self):
self.left = None
self.right = None
self.data = None
root = Tree()
root.data = 75
root.left = Tree()
root.left.data = 95
root.right = Tree()
root.right.data = 64
root.left.left = Tree()
root.left.left.data = 32
root.left.right = Tree()
root.left.right.data = 93
root.left.left = Tree()
root.right.left.data = 32
root.left.right = Tree()
root.right.right.data = 93
print(root.data)
This is tedious handtyping it, and if I were to have a list of numbers:
list = [1,2,3,4,5,6,7]
and put it through a loop to create a binary tree in this order so:
1
2 3
4 5 6 7
How would I write that? and because I'm using this to calculate the sum of all the paths, how do you navigate/iterate through a binary tree: