Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3.x object orientated programming a9q2.py: KVTtreenode.py: bstprism.py: T

ID: 3918090 • Letter: P

Question

Python 3.x object orientated programming

a9q2.py:

KVTtreenode.py:

bstprism.py:

Treenode.py:

a9q2scoring 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

Explanation / Answer

KVTreenode.py

def create(key, value, left=None, right=None):

    """

    Create a new kvtreenode for the given data.

    Pre-conditions:

       

    Post-condition:

        none

    Return:

        the treenode created

    """

    return {'key':key, 'value':value, 'left':left, 'right':right}

def get_key(node):

    """

    Retrieve the contents of the data field.

    Pre-conditions:

        node: a node created by create()

    Post-conditions:

        none

    Return

        the data value stored previously in the node

    """

    return node['key']

def get_value(node):

    """

    Retrieve the contents of the data field.

    Pre-conditions:

        node: a node created by create()

    Post-conditions:

        none

    Return

        the data value stored previously in the node

    """

    return node['value']

def get_left(tnode):

    """

    Retrieve the contents of the left field.

    Pre-conditions:

        tnode: a kvtreenode created by create()

    Post-conditions:

        none

    Return

        the value stored in left field

    """

    return tnode['left']

def get_right(tnode):

    """

    Retrieve the contents of the right field.

    Pre-conditions:

        tnode: a kvtreenode created by create()

    Post-conditions:

        none

    Return

        the value stored in right field

    """

    return tnode['right']

def set_value(tnode, val):

    """

    Set the contents of the data field to val.

    Pre-conditions:

        tnode: a node created by create()

        val: a data value to be stored

    Post-conditions:

        stores the new data value, replacing the existing value

    Return

        none

    """

    tnode['value'] = val

def set_left(tnode, val):

    """

    Set the contents of left field to val.

    Pre-conditions:

        tnode: a kvtreenode created by create()

        val:   a kvtreenode, or the value None

    Post-conditions:

        stores the val in left field, replacing the existing value

    Return

        none

    """

    tnode['left'] = val

def set_right(tnode, val):

    """

    Set the contents of right field to val.

    Pre-conditions:

        tnode: a treenode created by create()

        val:   a treenode, or the value None

    Post-conditions:

        stores the val in right field, replacing the existing value

    Return

        none

    """

    tnode['right'] = val