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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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

Part 3: Comparing Performance of Lists and BSTS as a function of the number of elements.
Now time for the main part of your script!
We want to see how the number of comparisons change for both a list and BST as the number of elements increases.
In the main part of your script do the following:
1. Create empty lists to store the results of average number of comparisons for a list and BST
2. for n in range(1, 1000, 100)
1. Set sumcountlist = 0, sumcountbst = 0 and numruns
%3D
2. for s in range(1, 5)
1. Create a list of length n using your populateList function.
2. Create a BST from that list, using your 1listToBST function
3. for v in range(n)
1. Use your searchLength function to determine how many comparisons to find v in your list. Add this to your
sumcountlist
2. Use the searchLength method of the BST class to determine how many comparisons to find v in your BST. Add
this to your sumcountbst
3. Update numruns
3. Computer the average number of comparisons by dividing sumcountlist and sumcountbst by the total number of times
you contributed to these sums (numruns) and append these to your lists storing the average number of comparisons.
3. Output your results as:
Average Search Length for List: <display list for the average list comparisons>
Average Search Length for BST: <display list for the average BST comparisons>
Transcribed Image Text:Part 3: Comparing Performance of Lists and BSTS as a function of the number of elements. Now time for the main part of your script! We want to see how the number of comparisons change for both a list and BST as the number of elements increases. In the main part of your script do the following: 1. Create empty lists to store the results of average number of comparisons for a list and BST 2. for n in range(1, 1000, 100) 1. Set sumcountlist = 0, sumcountbst = 0 and numruns %3D 2. for s in range(1, 5) 1. Create a list of length n using your populateList function. 2. Create a BST from that list, using your 1listToBST function 3. for v in range(n) 1. Use your searchLength function to determine how many comparisons to find v in your list. Add this to your sumcountlist 2. Use the searchLength method of the BST class to determine how many comparisons to find v in your BST. Add this to your sumcountbst 3. Update numruns 3. Computer the average number of comparisons by dividing sumcountlist and sumcountbst by the total number of times you contributed to these sums (numruns) and append these to your lists storing the average number of comparisons. 3. Output your results as: Average Search Length for List: <display list for the average list comparisons> Average Search Length for BST: <display list for the average BST comparisons>
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(lst) 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:
(self):Creates an empty binary search tree.
value): Creates a Node with the value value and inserts it into the BST.
(self, value):Returns True if value is in the BST and False otherwise.
• Constructor
init
append(self,
_contains
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.
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.
Transcribed Image Text: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(lst) 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: (self):Creates an empty binary search tree. value): Creates a Node with the value value and inserts it into the BST. (self, value):Returns True if value is in the BST and False otherwise. • Constructor init append(self, _contains 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. 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.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education