Consider the following classes and in particular the "insert" algorithm.
Consider the following classes and in particular the "insert"
import random
class Node:
def __init__(self, element):
self.firstSuccessor = None
self.secondSuccessor = None
self.element = element
def getElement(self):
return self.element
def setElement(self, element):
self.element = element
def getFirstSuccessor(self):
return self.firstSuccessor
def getSecondSuccessor(self):
return self.secondSuccessor
def setFirstSuccessor(self, firstSuccessor):
self.firstSuccessor = firstSuccessor
def setSecondSuccessor(self, secondSuccessor):
self.secondSuccessor = secondSucesor
def print(self):
if self.getFirstSuccessor() is not None:
self.getFirstSuccessor().print()
print(str(self.getElement())+',', end = '')
if self.getSecondSuccessor() is not None:
self.getSecondSuccessor().print()
class Structure:
def __init__(self):
self.initial = None
def insert(self, x):
list = None
for i in range(0, len(x)):
self.initial = self._insert(x[i], self.initial)
if self.initial is not None:
self.initial.print()
def _insert(self, x, t):
if t is None:
t = Node(x)
elif x < t.getElement():
t.setFirstSuccessor(self._insert(x, t.getFirstSuccessor()))
elif x > t.getElement():
t.setSecondSuccessor(self._insert(x, .getSecondSuccessor()))
else:
raise Exception('Duplicate element')
return t
Show which is the worst and best case by modeling with equations and solving them. Also show an example with data for the best and worst case using the data structure graphically.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps