Question: Question 2 : a . Implement the following function: def create _ chain _ bst ( n ) This function gets a positive integer n
Question :
a Implement the following function:
def createchainbstn
This function gets a positive integer n and returns a binary search tree with n
nodes containing the keys n The structure of the tree should be one
long chain of nodes leaning to the right.
For example, the call createchainbst should create a tree of the
following structure with the values inside its nodes in a valid order:
Implementation requirement: In order to create the desired tree, your
function has to construct an empty binary search tree, and can then only make
repeated calls to the insert method, to add entries to this tree.b In this section, you will show an implementation of the following function:
def createcompletebstn
createcompletebst gets a positive integer n where n is of the form
nk
for some nonnegative integer k
When called it returns a binary search tree with n nodes, containing the keys
n structured as a complete binary tree.
Note: The number of nodes in a complete binary tree is k
for some non
negative integer k
For example, the call createcompletebst should create a tree of the
following structure with the values inside its nodes in a valid
order:
You are given the implementation of createcompletebst:
def createcompletebstn:
bst BinarySearchTreeMap
additemsbst n
return bst
You should implement the function:
def additemsbst low, high
This function is given a binary search tree bst and two positive integers low
and high low high
When called, it adds all the integers in the range low high into bst
Note: Assume that when the function is called, none of the integers in the range
low high are already in bst
Hints:
c Before coding, try to draw the binary search trees structure and entries
that createcompletebstncreates for n and n
It would be easier to define additems recursively.
Analyze the runtime of the functions you implemented in sections a and b
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
