I have to write the an AVLTree in python, followed all the instructions and I feel I have it just right, only problem is I can't figure out how to display my tree. I have to use the test given below, as well as the 2 methods below in the TreeNode class, which are used to display the AVLTree. I'm not expecting any answers. I looked into my textbook to no avail. Looked online, found nothing to help me in figuring this out. Can anyone give me a nudge in the right direction??
if __name__ == "__main__":
bt = AVLTree()
while True:
c = 'a'
bt.display()
while(c not in 'iIdDfFeE'):
c=input("Enter choice: (i)nsert, (d)elete, (f)ind, (e)xit:")
c = c.rstrip()
if c in 'iI':
n = input("Enter integer to insert:")
try:
bt.insert(int(n))
except ValueError:
print ("BST insertion error")
elif c in 'dD':
n = input("Enter integer to delete:")
try:
bt.delete(int(n))
except ValueError:
print ("BST deletion error")
elif c in 'fF':
n = input("Enter integer to find:")
try:
bt.find(int(n))
except ValueError:
print ("BST find error")
elif c in 'eE':
break
def level(self, t):
TreeArray = [None]*(2**(self.height+2))
self.nodeToArray(TreeArray, 0)
TreeStringArray = []
#TreeString += str(5)
for h in range(self.height+2):
TreeString = ''
for item in TreeArray[2**h-1:2**(h+1)-1]:
TreeString += ' '*(2**(self.height+2-h))
if value is None:
TreeString += ' '
else:
TreeString += str(value)
TreeString += ' '*(2**(self.height+2-h)-1)
TreeStringArray.append(TreeString)
return TreeStringArray[t]
def nodeToArray(self, TreeArray, t):
TreeArray[t] = self.value
if self.left != None:
self.left.nodeToArray(TreeArray, 2*t+1)
if self.right != None:
self.right.nodeToArray(TreeArray, 2*t+2)