Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Binary Expression Tree in Python. 1. Your program can parse a valid expression into a binary tree and calculate the final result. [2pts] For example,
Binary Expression Tree in Python.
1. Your program can parse a valid expression into a binary tree and calculate the final result. [2pts] For example, given the input string: ((2 (3+2)) +5)/2) The calculated result will be 7-5 Note that this example is for illustrative purposes, and during the marking process you may be givena different string to test your solution. 2. [3pts] Your program can visualise the generated binary tree The expected output tree for the input shown under (1) above is as follows (you can print it on the screen, and you don't need to print the lines connecting the tree nodes if it is not convenient) 2 3 2 3. Your program can save the binary tree into a file. After you quit your program, you can restart your program and reload the tree from the same file into memory and visualise it again. 2pts] You may consider using the pickle package for serialisation. You may also use other packages or could write your own functions for serialisation. in Your program should be able to report an error message if an input string is not a valid one. Specifically, your program should report an error message "not a valid expression" for each of the following invalid expressions 4. [2pts] "three operands within one pair of brackets" only one operand within one pair of brackets" "three operands within one pair of brackets" "no outer brackets" "bracket missing" "bracket missing" "operator missing between 1(2+3)" (4*3*2) (2*4) (3+2) (2+3)(4*5) To be awarded marks for this task, you don't need to report the exact error messages for the above expressions; reporting "not a valid expression" will be sufficient. Note 5. Further to Task 4, your program can additionally report why the expression is not valid 1pt] The following expressions would have the corresponding error messages as shown Not a valid expression, wrong number of operands Not a valid expression, wrong number of operands Not a valid expression, wrong number of operands Not a valid expression, brackets mismatched Not a valid expression, brackets mismatched Not a valid expression, bracket mismatched Not a valid expression, operator missing. (4*3*2) (2*4) (3+2) (2+3)(4*5) Final Notes You should document your code well so that not only your code is well written, but also your idea and detailed steps are provided along with the code. Poorly documented code may result in a lower mark. 1. Your program can parse a valid expression into a binary tree and calculate the final result. [2pts] For example, given the input string: ((2 (3+2)) +5)/2) The calculated result will be 7-5 Note that this example is for illustrative purposes, and during the marking process you may be givena different string to test your solution. 2. [3pts] Your program can visualise the generated binary tree The expected output tree for the input shown under (1) above is as follows (you can print it on the screen, and you don't need to print the lines connecting the tree nodes if it is not convenient) 2 3 2 3. Your program can save the binary tree into a file. After you quit your program, you can restart your program and reload the tree from the same file into memory and visualise it again. 2pts] You may consider using the pickle package for serialisation. You may also use other packages or could write your own functions for serialisation. in Your program should be able to report an error message if an input string is not a valid one. Specifically, your program should report an error message "not a valid expression" for each of the following invalid expressions 4. [2pts] "three operands within one pair of brackets" only one operand within one pair of brackets" "three operands within one pair of brackets" "no outer brackets" "bracket missing" "bracket missing" "operator missing between 1(2+3)" (4*3*2) (2*4) (3+2) (2+3)(4*5) To be awarded marks for this task, you don't need to report the exact error messages for the above expressions; reporting "not a valid expression" will be sufficient. Note 5. Further to Task 4, your program can additionally report why the expression is not valid 1pt] The following expressions would have the corresponding error messages as shown Not a valid expression, wrong number of operands Not a valid expression, wrong number of operands Not a valid expression, wrong number of operands Not a valid expression, brackets mismatched Not a valid expression, brackets mismatched Not a valid expression, bracket mismatched Not a valid expression, operator missing. (4*3*2) (2*4) (3+2) (2+3)(4*5) Final Notes You should document your code well so that not only your code is well written, but also your idea and detailed steps are provided along with the code. Poorly documented code may result in a lower markStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started