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.
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
- factorial: !
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.
"+" / \ 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.
"/" / \ 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!)
cos(0)! = (cos(0))!
- The rfind method can search for a sub-string in a string.
- Use the math library's mathematical functions.
- The argument of the factorial will be a non-negative integer, but there is a continuous generalizations of the factorial, called gamma function. "
- Problem 1: 2 points
- Problem 2: 2 points
- Problem 3: 3 points