Question: Python 3.x object orientated programming a9q2.py: import KVTreeNode as TN def member_prim(tnode, key): Purpose: Check if value is stored in the binary search tree.
Python 3.x object orientated programming

a9q2.py:
import KVTreeNode as TN def member_prim(tnode, key): """ Purpose: Check if value is stored in the binary search tree. Preconditions: :param tnode: a KVTreeNode with the BST property :param key: a key Postconditions: none :return: a pair True, value if key is in the tree a pair False, None if the key is not in the tree """ if tnode is None: return False, None elif tnode.key is key: return True, tnode.value elif key """ Insert a new key,value into the binary search tree. Preconditions: :param tnode: a KVTreeNode with the BST property :param key: a key Postconditions: If the key is not already in the tree, it is added. If the key is already in the tree, the old value is replaced with the given value. Return :return: tuple (flag, tree) flag is True if insertion succeeded; tree contains the new key-value flag is False if the value is already in the tree, the value stored with the key is changed """ return False, None def delete_prim(tnode, key): """ Delete a value from the binary search tree. Preconditions: :param tnode: a KVTreeNode with the BST property :param key: a key Postconditions: If the key is in the tree, it is deleted. The tree retains the binary search tree property. If the key is not there, there is no change to the tree. Return :return: tuple (flag, tree) flag is True if deletion succeeded; tree is the resulting without the value flag is False if the value was not in the tree, tree returned unchanged """ return False, None
KVTtreenode.py:
# A KVTreeNode is a simple container with four pieces of # information: # key: the key for the node # value: the contained information # left: a reference to another KVTreeNode, or None # right: a reference to another KVTreeNode, or None # Implementation notes: # This ADT implementation uses a Python class class KVTreeNode(object): def __init__(self, key, value, left=None, right=None): """ Create a new KVTreeNode for the given data. Pre-conditions: key: A key used to identify the node value: Any data value to be stored in the KVTreeNode left: Another KVTreeNode (or None, by default) right: Another KVTreeNode (or None, by default) """ self.value = value self.left = left self.right = right self.key = key
bstprism.py:
import TreeNode as TN def member_prim(tnode, target): """ Purpose: Check if value is stored in the binary search tree. Preconditions: :param tnode: a binary search tree :param target: a value Postconditions: none :return: True if value is in the tree """ if tnode is None: return False elif tnode.data is target: return True elif target """ Insert a new value into the binary search tree. Preconditions: :param tnode: a TreeNode with the BST property :param value: a value Postconditions: If the value is not already in the tree, it is added Return :return: tuple (flag, tree) flag is True if insertion succeeded; tree contains the new value flag is False if the value is already in the tree, tree unchanged """ if tnode is None: return True, TN.TreeNode(value) else: if tnode.data is value: return False, tnode elif value """ Delete a value from the binary search tree. Preconditions: :param tnode: a TreeNode with the BST property :param target: a value Postconditions: If the value is in the tree, it is deleted. If the value is not there, there is no change to the tree. Return :return: tuple (flag, tree) flag is True if deletion succeeded; tree is the resulting without the value flag is False if the value was not in the tree, tree returned unchanged """ def delete(tnode): if tnode is None: return False, tnode else: cval = tnode.data if cval == target: return reconnect(tnode) elif target
Treenode.py:
class TreeNode(object): def __init__(self, data, left=None, right=None): """ Create a new treenode for the given data. Pre-conditions: data: Any data value to be stored in the treenode left, right: Another treenode (or None, by default) Post-condition: none """ self.data = data self.left = left self.right = righta9q2scoring located on pastebin:
https://pastebin.com/u7zPyRfB
Question 2 (15 points) Purpose: To adapt some working code to a slightly modified purpose Degree of Difficulty: Moderate In class we discussed binary search trees, and basic operations on them. The code for these operations can be found in the file bstprim.py on the Assignment 9 Moodle page. In this question, you will adapt the bstprim.py file to use the key-value TreeNode class. As we also discussed in class, the KVTreeNode class is variant of the treenode class. The key-value treenode allows us to organize the data according to a key, and store a data value associated with it. You can find the implementation in file KVTreeNode. py Adapt the primitive BST functions from bstprim.py to use the KVTreeNode class. The KVTreeNodes in the tree should have the binary search tree property on the keys, but not the values. The functions you need to adapt are as follows member_prim(t,k) Returns the tuple True, v, if the key k appears in the tree, with associated value v. If the key k does not appear in the tree, return the tuple False, None insert-prim(t, kv) Stores the value v With the key k in the Table t . If the key k is already in the tree, the value v replaces the value currently associated with it. In this case, it returns the tuple (False, t) even though t did not change structure in this case . If the key is not already in the tree, the key and value are added to the tree. In this case the function returns the tuple (True, t2). Here, t2 is the same tree as t, but with the new key, value added to it. delete prim(t,k) If the key k is in the tree t, delete the node containing k (and its value) from the tree and return the pair (True, t2) where t2 is the tree after deleting the key-value pair, return the pair (False, t) if the key k is not in the given treet (t is unchanged) List of files on Moodle for this question a9q2.py-partially completed bstprim.py - the BST operations, using TreeNode TreeNode- The simpler BST treenode class. For use with bstprim.py KVTreeNode The key-value BST treenode class. For use with a9q2.py . a9q2score.py - A script that will help you check your progress on a9q2.py Question 2 (15 points) Purpose: To adapt some working code to a slightly modified purpose Degree of Difficulty: Moderate In class we discussed binary search trees, and basic operations on them. The code for these operations can be found in the file bstprim.py on the Assignment 9 Moodle page. In this question, you will adapt the bstprim.py file to use the key-value TreeNode class. As we also discussed in class, the KVTreeNode class is variant of the treenode class. The key-value treenode allows us to organize the data according to a key, and store a data value associated with it. You can find the implementation in file KVTreeNode. py Adapt the primitive BST functions from bstprim.py to use the KVTreeNode class. The KVTreeNodes in the tree should have the binary search tree property on the keys, but not the values. The functions you need to adapt are as follows member_prim(t,k) Returns the tuple True, v, if the key k appears in the tree, with associated value v. If the key k does not appear in the tree, return the tuple False, None insert-prim(t, kv) Stores the value v With the key k in the Table t . If the key k is already in the tree, the value v replaces the value currently associated with it. In this case, it returns the tuple (False, t) even though t did not change structure in this case . If the key is not already in the tree, the key and value are added to the tree. In this case the function returns the tuple (True, t2). Here, t2 is the same tree as t, but with the new key, value added to it. delete prim(t,k) If the key k is in the tree t, delete the node containing k (and its value) from the tree and return the pair (True, t2) where t2 is the tree after deleting the key-value pair, return the pair (False, t) if the key k is not in the given treet (t is unchanged) List of files on Moodle for this question a9q2.py-partially completed bstprim.py - the BST operations, using TreeNode TreeNode- The simpler BST treenode class. For use with bstprim.py KVTreeNode The key-value BST treenode class. For use with a9q2.py . a9q2score.py - A script that will help you check your progress on a9q2.py
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
