solution

Define a maximum and a minimum function which return the maximum and minimum value in a binary search tree.

A mathematical expression in infix notation has the operators placed between the operands

 

Order Now
SKU: assim16

Question One

A mathematical expression in infix notation has the operators placed between the operands.   With such a notation, it is necessary to use parentheses to change the order in which the operators are evaluated.  An example of an expression in infix notation is:

2 * (3 + 7)

which evaluates to 20.

In this question, you must write a function called q1() that evaluates infix expressions.  The usual four mathematical operators should be supported (+, -, *, /), however there are three additional operators that must also be supported:

example

 

Relative to the four standard operators, the exponentiation operator (^) has higher precedence, and the minimum and maximum operators (<, >) have lower precedence.  The following table summarises the precedence of all of the operators:

2nd

 

In expressions consisting of several operators of the same precedence, the evaluation order is left to right.  Some more examples are shown below:

23

 

The input to your q1() function will be a string.  Each token in the string (whether an operator or an operand) will have a single space character on either side of it.  You do not need to check the validity of the input string format – you can assume this will be correct.

 

As an example, the code fragment on the left below should produce the output on the right:

print

 

 

Question Two

Use the built‐in Python turtle to draw the following shape (you can see the turtle sitting in the middle of the shape, facing up, after the shape is drawn):

dra

 

Actually, your program should not always draw the shape above.  The example above is a “Level 5” version of the shape.  The user should be prompted for which level of the shape they would like to draw (with 1 being the smallest valid input).  Examples of several smaller versions of the shape are shown below:

level

 

The size of the inner “I” shape, which is the complete Level 1 shape, is 200 pixels wide and 200 pixels high.  This inner “I” is the same size in all versions of the shape, even though the complete shape grows larger as the level increases.  Your program should start by asking the user to enter the level they would like to draw:

Draw shape at level:

Once the drawing of the shape at the specified level is complete, the turtle should end up in the center of the shape facing north.

 

 

Question Three

A binary search tree can be constructed from a list of nodes by adding the nodes to the tree in the order they appear in the list.  For example, given this list:

[42, 31, 39, 67, 6, 100, 88]

if we add these values, in the order they appear from left to right, to a binary search tree we will generate the following tree:

42

 

Task A

Define a function called create_tree_from_list which takes a list of values as input, and produces a binary search tree with those values as the data items in the nodes.  You will need to define a BinarySearchTree class to represent the nodes in the tree (you may like to extend the BinarySearchTree class shown in lectures).

 

As an example, the following function call:

my_tree = create_tree_from_list([42, 31, 39, 67, 6, 100, 88])

should create the binary search tree shown in the diagram above.

 

Task B

Define a maximum and a minimum function which return the maximum and minimum value in a binary search tree.  Both functions are passed a binary search tree as an input, and should return the corresponding data value.  For example, the code below:

my_tree = create_tree_from_list([42, 31, 39, 67, 6, 100, 88])

print(“Smallest value =”, minimum(my_tree))

print(“Largest value =”, maximum(my_tree))

 

should produce the output:

Smallest value = 6

Largest value = 100

 

Task C

Define a size and a height function which return the number of nodes in the binary search tree, and the height of the binary search tree.  Note – in this case, the height of the tree is defined as the number of nodes on the longest path in the tree from the root node to a leaf node.

For example, the code below:

my_tree = create_tree_from_list([42, 31, 39, 67, 6, 100, 88])

print(“Number of nodes =”, size(my_tree))

print(“Height of tree =”, height(my_tree))

 

should produce the output:

Number of nodes = 7

Height of tree = 4

 

Task D

Define a function called breadth_first which takes a binary search tree as input, and which returns a list of the data values in the tree in breadth-first order (i.e. as read from top to bottom, and left to right).

For example, the code below:

my_tree = create_tree_from_list([42, 31, 39, 67, 6, 100, 88])

print(“Breadth first =”, breadth_first(my_tree))

 

should produce the output:

Breadth first = [42, 31, 67, 6, 39, 100, 88]

 

Task E

Define a function called mirror which takes a binary search tree as input, and which modifies that tree by mirroring its structure.  After mirroring, the tree should look like it is being viewed in a mirror.

my_tree = create_tree_from_list([42, 31, 39, 67, 6, 100, 88])

mirror(my_tree)

43

 

Question Four

A hashtable has been created, and is using linear probing with the hash function:

h(key) = key % size

where size is the capacity of the hashtable.

 

A list of keys are then inserted, in order, into the hashtable.  Once the keys have been inserted, you need to calculate the worst index position in the hashtable.  That is, the index position in the hashtable that will generate the longest probe sequence.

 

You must write a function called q4() which will be passed two inputs:

the size of the hashtable

the list of keys to be inserted, in order

 

Your function must return a list which contains the worst index position (or positions, if there are multiple indexes that would lead to equally long probe sequences – in this case the index values in the list must be in increasing order).

 

For example, the following code

values = [25, 32, 88, 10, 35, 11]

worst = q4(11, values)

print(worst)

 

should produce the output:

[10]

 

As another example, the code:

values = [4, 9, 12, 3, 7, 26, 16, 20, 11]

worst = q4(13, values)

print(worst)

 

should produce the output:

[3, 7, 11]

 

Question Five

For this question, you need to add one more function to your Q3.py source file.

Define a function called create_tree_from_structured_list which takes a list of length 3 that represents a binary search tree, and creates the corresponding tree.

For example, the following list of length 3 (note that the second and third elements in the list are also lists of length 3):

[10, [5, [2, None, None], [7, None, None]], [999, None, [15, None, None]]]

unambiguously represents the binary search tree:

rou

 

Likewise, this list of length 3:

[‘200’, [‘100’, None, None], [‘300’, None, None]]

unambiguously represents the binary search tree:

cap

 

Your create_tree_from_structured_list function (which you should define along with the other binary search tree functions in Q3.py) should create a binary search tree, given the input list.

As an example, the following function call:

my_tree = create_tree_from_structured_list(

[‘200’, [‘100’, None, None], [‘300’, None, None]])

should create the second binary search tree shown above.

 

Question Six

You are going on holiday, and having a difficult time trying to plan how you will spend your time.  So many activities, so little time!

You have a list of various activities that you would like to do, and each activity takes a certain amount of time.  However, you also have a fixed amount of time available, and you can’t start a new activity if you don’t have time to finish it.  You don’t mind which activities you do, but you don’t want to waste time.

Essentially, you want to pick a specific set of activities where the total time required is as close as possible to the time you have available.

For example, let’s say you have 700 minutes available, and there are 8 activities you are considering, each taking the following times:

155, 183, 281, 192, 111, 267, 154, 134

Let’s say you choose these four activities: 183, 111, 267 and 134.  Choosing these four activities means you will only waste 5 minutes (as 700 – (183 + 111 + 267 + 134) = 5).  In fact, this is the best that you can do – there are no other combinations of activities you can take that will waste less time.

Define a function called q6() which is passed two inputs:

the total amount of time you have available

a list of the times needed for the various activities

 

Your function should calculate and return the smallest amount of time that will be wasted on your holiday if you select the right combination of activities.

For example, the following function call:

wasted = q6(700, [155, 183, 281, 192, 111, 267, 154, 134]) print(wasted)

will produce the output:

5

and the following function call:

wasted = q6(100, [50, 20, 10, 39]) print(wasted)

 

will produce the output:

1

Reviews

There are no reviews yet.

Be the first to review “Define a maximum and a minimum function which return the maximum and minimum value in a binary search tree.”

Your email address will not be published. Required fields are marked *