Searching for Data In this lab we'll compare the time it takes to search for an item in a list vs searching for it in a binary search tree. Part 1: Populating and Searching a List Let's start off in our main.py file. First import the random module, then implement the following functions: • populateList(n, s):This function takes as a parameter a positive integer n, and a positive integer s and returns a list of length n containing a shuffling of the numbers 0 to n - 1. Prior to shuffling use random.seed (s) to seed the random number generator. We're doing this so we can have reproducible results. Then use the random.shuffle(1st) to shuffle your list. • searchLength(lst, n):This function takes as a parameter a list 1st and a positive integer n, which is the value to be searched. The function returns how many elements of 1st had to be inspected before finding n. If n was not encountered, it will return the length of 1st. The first two unit tests will test running the functions and obtaining expected results. Part 2: Populating Binary Search Tree from a List You have been provided a module called BST. This module contains a modified implementation of a Binary Search Tree (BST class) and associated Node class. The BST class has the following public methods: • Constructor append (self, value):Creates a Node with the value value and inserts it into the BST. • _contains__(self, value): Returns True if value is in the BST and False otherwise. • searchLength(self, value): returns how many elements had to be inspected before finding value as the data in a node or hitting a leaf node during the search. init_(self): Creates an empty binary search tree. Add to your main script a function called listToBST(lst) that takes a list 1st, appends its elements to an empty Binary Search Tree in the order provided (e.g. as encountered in 1st), and returns that Binary Search Tree object. The next set of unit tests will test running this function and obtaining expected result.
Please help in Python 3.
Given code for BST.py
## Node class
class Node():
def __init__(self, val):
self.__value = val
self.__right = None
self.__left = None
def setLeft(self, n):
self.__left = n
def setRight(self, n):
self.__right = n
def getLeft(self):
return self.__left
def getRight(self):
return self.__right
def getValue(self):
return self.__value
# BST class
class BST():
def __init__(self):
self.__root = None
def append(self, val):
node = Node(val)
if self.__root == None:
self.__root = node
return
current = self.__root
while True:
if val <= current.getValue():
if current.getLeft() == None:
current.setLeft(node)
return
else:
current = current.getLeft()
else:
if current.getRight() == None:
current.setRight(node)
return
else:
current = current.getRight()
def searchLength(self, val):
if self.__root == None:
return -1
current = self.__root
length = 0
while current != None:
if current.getValue() == val:
return length + 1
elif val < current.getValue():
length += 1
current = current.getLeft()
else:
length += 1
current = current.getRight()
return length
def __contains__(self, val):
if self.__root == None:
return False
current = self.__root
while current != None:
if current.getValue() == val:
return True
elif val < current.getValue():
current = current.getLeft()
else:
current = current.getRight()
return False
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images