Question: please try to explain how u got your answers? what was your thinking proces? these questions should be done in Python Assignment 2 (a: 2

Assignment 2 (a: 2 points; b: 2 points) Your goal is to implement a function t_depth(t) and t_is_balanced (t) in the file A2.py. M147 We define a data structure for binary trees in the following way: A binary tree is a graph with nodes and arcs. From every node there are exactly 2 outgoing arcs. In every internal node we store a natural number. In the leaves of the tree nothing is stored. Our internal representation of a binary tree is a nested dictionary (not the representation for directed graphs form the lecture slides). Every dictionary (except for the empty dictionaries in the leaves) has 3 keys called 'L' (for left right subtree) and 'V' (for value, i.e. the natural number stored in the node). Examples: tree0 = {} an empty tree (a single leave). 'R treel = {'L': {}, 'R': {}, 'V': 3) figure: treel tree2 = {'L': 'R': {'L': {}, 'R': {}, 'y': 21 {'L': {}, 'R': {'L': 'R': V': V : 1). 5} {}, (}. 6) figure: tree2 V': tree - {'L': {'L': (). R': {'L': ('L': (), 'R' ('L': {'L': R' (), ('L': () 'R': (). 'V': 9). 'R': (), 'V': 13), 2). 'V' 'R' (), 'V': 8), 'V'3). 'R': ('L': (). R' ('L': 0) R': (). 'V'. 1). 'V': 5) figure: tree a) We define the depth of a tree to be the number of nodes (containing a number, so not the leaves) from the root to the lowest node containing a number. In the last example the path to the lowest node is 5, 3, 8, 2, 13, 4, 9, so this tree has depth 7. The depths of all trees above are: tree0 depth o treel depth 1 tree 2 depth 3 tree depth 7 tree['1'] depth 6 Implement a recursive function t_depth(t) that returns the depth of tree t. b) We define a tree to be balanced if for every (sub)tree the absolute difference of the depth of the 'L' subtree and 'R' subtree is 0 or 1. tree0 balanced?: True treel balanced?: True tree 2 balanced?: True tree balanced?: False tree['L') balanced?: False Implement a recursive function t_is_balanced(t) that returns a boolean reporting whether t is balanced or not. Expected test program output: DEPTH: IS BALANCED?: True True True False False
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
