I want to create a binary tree from a dictionary of parents (keys) and their children (values, as tuples (one_child, second_child)):
{1:(2,3), 2:(4,5), 4:(6, None), 3:(7,8), ...} #they are in random order
The binary tree should be created without using a recursion.
My Node class:
class Node:
def __init__(self,key):
self.left = None
self.right = None
self.val = key
What was I trying for was: What I tried was:
self.root = self.Node(found_root)
parents = list(dictionary)
p = 0
while (p != len(parents)-1):
curr_node = self.Node(parents[p], self.Node(dictionary.get(parents[p])[0]),self.Node(dictionary.get(parents[p])[1]))
p += 1