Assignment 4 (traversal and optimisation)
This assignment requires you to submit programs in Python that you have written yourself to the automarker, https://www.automarker.cs.auckland.ac.nz. Your implementation must be from first principles and cannot use an existing library methods that might solve the problem (eg performs graph operations etc). The automarker runs on a Linux box. Read the automarker help and FAQ for more details. Please submit only Python source code (.py extensions only).
1. Arithmetic trees 30 marks
You are given an input file with multiple pairs of input lines. The first line of each pair is a tree given as a predecessor array. The second line is the value at the corresponding node. Values at leaf nodes (nodes with no children) are integers. At non-leaf nodes, the two possible values are + or ∗. The tree represents an arithmetic expression where the value at a non-leaf node u is the sum of values at the children of u in the case of +, or the product of values at the children of u in the case of ∗. You need to calculate the value at each node and output the calculated value at the root. The tree is not constrained to be binary. Input format: Input consists of m pairs of lines of comma separated values, so 2m lines in total. The first line is each pair is a comma separated list of integers representing a tree in predecessor array format where −1 represents null. The second line in each pair is a comma separated list of integers and the symbols + and ∗. The ith item on the list is the value or operator at the ith node in the tree. For example:
-1,0,0,0,1,1
+,*,2,3,0,7
2,0,-1,0
+,3,*,3
Output format: For each pair of input lines, output a line containing the value calculated at the root of the tree. For the example input above, output would be:
6
...