class NodeQ: 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 class BSTO: def __init__(self); self -root = None def append(self,val): node = Node(val) if self.__rootNone: self.__root node return current-self.__root while True: if vaî <= current.getvalueO: if current.getLeft() == None: current.setLeft(node) return else: current - current.getLeft( else: if current.getRightO None: current.setRight(node) return else: current - current.getRightO def isin(self, val): if self.__rootNone: return False current-self.__root while current-None: if current.getValụe○ == va1: return True elif val < current.getValueC): current - current.getLeft( else: current - current.getRightQ

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

This code is for python

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

To get started create a function, populate, that takes as a parameter a positive integer n, populates a list of length with random integers in the range of [0, n] and returns that list.

Next create a function that takes as a parameter a list and an integer and returns True if that integer is found in the list and False if it is not.

Finally, in your main script call your function for a value of to get a list of length n and then go through that list and look for each element in that list within the list and print out that count.  Needless to say if you did everything right you should print out n

Part 2: Populating and Searching a Binary Search Tree

Download the provided BST.py module.   This module is a stripped down implementation of a Binary Search Tree and associated Node class.

The BST class has the following public methods:

  • Constructor - Creates an empty binary search tree.
  • append(value) - Creates a Node with the value value and inserts it into the BST.
  • isin(value) - Returns True if value is found in a Node of the BST and False otherwise.

Change your main script so that your populate function creates both a list and a Binary Search Tree with the same random elements and returns them both.  In addition, change your main script so that with in looks for all the values of the list within the list, that it also counts how many times those values exist in the BST and prints that number out as well.

Part 3: Timing

Now let's time how long it takes to look for all n values within both the list and the BST!  To do this, first import the time module.  The time module has a time() function that returns the system time.  Get this value before checking the list for all the values and then again after you're done checking the list.  Print out the difference in these times.  Now do the same thing for searching the BST!

Part 4: Plot n vs. time

Finally onto the cool stuff!  In the previous parts you were asked to just use some arbitrary value of n.  Now let's do everything you did in Part 3 but for a range of values of n.  In particular let's go from n=1 to n=10,000 in steps of 1000.   Keep track of the times for each of these runs for searching both the list and the BST so that you can plot them!As a reminder, we can use the matplotlib library to help us plot.  So first import matplotlib.pyplot as plt.  If you cannot import this module, then you may need to install (pip) matplotlib.Then you can make use of the following methods:

  • plt.plot(xvals,yvals,label='text')  -  Adds a plot of xvals vs yvals to the current figure with a label saying 'text'.  Doing this several times superimposes the plots.
  • plt.legend() - This will "enable" the labels to be shown as a legend.
  • plt.show() - This displays the figure.

Finally, plot vs average list search time and n vs average BST search time.

Attached is the BTS.py module

 

class NodeQ:
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
class BSTO:
def __init__(self);
self -root = None
def append(self,val):
node = Node(val)
if self.__rootNone:
self.__root node
return
current-self.__root
while True:
if vaî <= current.getvalueO:
if current.getLeft() == None:
current.setLeft(node)
return
else:
current - current.getLeft(
else:
if current.getRightO None:
current.setRight(node)
return
else:
current - current.getRightO
def isin(self, val):
if self.__rootNone:
return False
current-self.__root
while current-None:
if current.getValụe○ == va1:
return True
elif val < current.getValueC):
current - current.getLeft(
else:
current - current.getRightQ
Transcribed Image Text:class NodeQ: 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 class BSTO: def __init__(self); self -root = None def append(self,val): node = Node(val) if self.__rootNone: self.__root node return current-self.__root while True: if vaî <= current.getvalueO: if current.getLeft() == None: current.setLeft(node) return else: current - current.getLeft( else: if current.getRightO None: current.setRight(node) return else: current - current.getRightO def isin(self, val): if self.__rootNone: return False current-self.__root while current-None: if current.getValụe○ == va1: return True elif val < current.getValueC): current - current.getLeft( else: current - current.getRightQ
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 10 steps with 4 images

Blurred answer
Knowledge Booster
Time complexity
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
  • SEE MORE 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