Python 3.x Starter File: a9q2.py: Other Code that you may need KVTtreenode.py: b
ID: 3918977 • Letter: P
Question
Python 3.x
Starter File:
a9q2.py:
Other Code that you may need
KVTtreenode.py:
bstprism.py:
Treenode.py:
Question 3 (6 points): Purpose: To implement the Table ADT, as described in class. Degree of Difficulty: Should be Easy In this question, you'll implement the full Table class, as we discussed in class. The Table class has the following methods: size0 Returns the number of key-value pairs in the Table. is empty) Returns True if the Table t is empty, False otherwise. insert(k.v) Stores the value v with the key k in the Table. If the key k is already in the Table, the value v replaces any value already stored for that key given key: otherwise return the tuple False, None. ?f the key k is not in the Table. retrievelk) If the key k is in the Table, return a tuple True, value, where value is the value stored with the delete(k) If the key k is in the Table, delete the key-value pair from the table and return True, return False A starter file has been provided for you, which you can find on Moodle named a9q3.py. The-init- method is already implemented for you, and all of the others have an interface and a trivial do-nothing To complete this question, you should import a9q2 your solution to Question 2 into the Table ADT. Your Table methods can call the primitive BST functions. Your Table methods may have to do a few house- keepingitems (such as change the size attribute when inserting or deleting), but most of the work is done by your solution to Question 2. List of files on Moodle for this question a9q3.py partially completed a9q3score.py - A script that will help you check your progress on a9q3.py.Explanation / Answer
here is your modified code : -------------------->>>>>>>>>>>>>
import a9q2 as prim
class Table(object):
def __init__(self):
self.__root = None
self.__size = 0
def size(self):
"""
Purpose:
Return the size of the table.
Return:
:return: the number of key,value pairs in the table
"""
return self.__size
def is_empty(self):
"""
Purpose:
Indicate whether the given table is empty.
Return:
:return: True if the table is empty
"""
return self.__root == None and self.__size == 0
def retrieve(self, key):
"""
Return the value associated with the given key.
Preconditions:
:param key: a key
Postconditions:
none
Return
:return: True, value if the key appears in the table
False, None otherwise
"""
val,value = prim.member_prim(self.__root,key)
return val, value
def insert(self, key, value):
"""
Insert a new key, value into the table.
Preconditions:
:param key: a unique key for the value
:param value: a value
Postconditions:
If the key is not already in the table, it is added to the table
If the key is already there, change the value
Return
:return: True if the key,value was inserted
False if the value of an existing key was changed
"""
val,self.__root = prim.insert_prim(self.__root,key,value);
return val
def delete(self, key):
"""
Delete a given key and its associated value from the table.
Preconditions:
:param key: a unique key for the value
Postconditions:
If the key is not in the table, no change to the table
If the key is in the table, remove it
Return
:return: True if the key,value was deleted
"""
val,self.__root = prim.delete_prim(self.__root,key)
return val
def in_order(self):
"""
Returns a string of the keys showing the in-order sequence
"""
def in_order_prim(kvtnode):
if kvtnode is None:
return " "
else:
before = in_order_prim(kvtnode.left)
this = '('+str(kvtnode.key)+','+str(kvtnode.value)+')'
after = in_order_prim(kvtnode.right)
return before + this + after
return in_order_prim(self.__root)
v = Table()
v.insert("DJ","Dhananjay")
v.insert("RJ","Rahul Jockey")
v.delete("DJ")
print(v.in_order())
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.