Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python 3.x Starter code: # Defines functions for primitive Binary Search Tree data structure # # A Primitive Binary Tree is defined as follows: #

Python 3.x

image text in transcribed

Starter code:

# Defines functions for primitive Binary Search Tree data structure # # A Primitive Binary Tree is defined as follows: # 1. The value None is a primitive binary tree; # None is an empty tree. # 2. If lt and rt are primitive binary trees, and d is any value # TreeNode(d, lt, rt) is a primitive binary tree. # A Primitive Binary Tree t satisfies the Binary Search Tree (BST) # property if all of the following hold: # 1. The key stored at TreeNode t is greater than any key # in t's left subtree (if any) # 2. The key stored at TreeNode t is less than any key # in t's right subtree (if any) # 3. t's left subtree satisfies the BST property # 4. t's right subtree satisfies the BST property 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 
 

KVTreenode.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 

Other code if necessary:

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 = right 
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 KVTreellode 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,k,v) 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 tree t (t is unchanged)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxxviii Special Issue On Database And Expert Systems Applications Lncs 11250

Authors: Abdelkader Hameurlain ,Roland Wagner ,Sven Hartmann ,Hui Ma

1st Edition

3662583836, 978-3662583838

More Books

Students also viewed these Databases questions

Question

A configuration developer needs to configure a certifictae

Answered: 1 week ago

Question

f. Did they change their names? For what reasons?

Answered: 1 week ago