Homework08

Problem 1: New Tree Methods

• Write an is_path(self) method for the class Tree which returns True if the tree is a long path without a junction. False otherwise. The tree is a path if all of the nodes except the root have at most one branches.
• Write a path(self) method for the class Tree which returns all paths from the leaves to the root of a given binary tree.

Problem 2: Proper Calculator

Write modify the calculator in the exercise to handle parantheses such that it calculates the input based on the parentheses.

Example:

2+(7*3) --> 23
(2+7)*3 --> 27

Problem 3: Calculator Function

You have to improve the calculator's Node class even further. You have to treat the functions:

• sin, cos

Example:

cos(0)*sin(5+6*3)
• factorial: !

example:

(2*3)!

You can treat these functions as unary operations and the operator is the name of the function. This means that in the expression tree you can have "sin", "cos" or "!" in a node an they have only one outgoing edge (child) in the tree, that is their operands.

The nodes "sin" and "cos" should have a right member, which is expression what is inside the function. This is because the argument of the sin (or cos) is on the right-hand-side of the symbol itself.

Example "1+cos(5-3)":

"+"
/ \
1  "cos"
\
"-"
/ \
5   3

The "!" node should have a left member, which is the argument of the factorial. This is because the argument is on the left-hand-side of the factorial symbol.

Example "1/(2*3)!":

"/"
/  \
1   "!"
/
"*"
/ \
2   3

The trigonometric functions should have a higher precedence, than any other operations. Example:

cos(1)^2 = (cos(1))^2

The factorial should have a higher precedence than the binary operations, but lower than the trigonometric functions. Example:

1 + 2! = 1 + (2!)

and

cos(0)! = (cos(0))!

Help:

• The rfind method can search for a sub-string in a string.
• Use the math library's mathematical functions.

Remark:

• The argument of the factorial will be a non-negative integer, but there is a continuous generalizations of the factorial, called gamma function. "

Note

• Problem 1: 2 points
• Problem 2: 2 points
• Problem 3: 3 points