I am building a Tree class that consists of inner nodes (denoted by the class Inner) and leaf nodes (denoted by the class Node).
class Node(object):
def __init__(self,bits,data):
self.bits = bits
self.data = data
class Inner(object):
def __init__(self):
self.bits = ''
self.c0 = None
self.c1 = None
class Tree(object):
def __init__(self):
self.root = None
def insert_Item(self,key,datastr):
#code goes here
I can insert both leafs and inner nodes using insert method.
t = Tree()
t.insert('1111', 'A')
t.insert('1110', 'B')
The problem occurs with recursive formulation of insert method. I cannot call self.root.c0.insert()
or self.root.c1.insert()
assuming self.root.c0 and self.root.c1 points to Inner nodes. This is because Inner class does not have insert function.
What can i do to make insert method work on all three classes in a recursive fashion? Similarly i cannot do tree traversal as i get Error that Inner object has no attribute 'data'