-1

Given 4 trees in a dictionary form:

tree0 = {} 

tree1 = {'L': {}, 'R': {}, 'V': 3} 

tree2 = {'L': {'L': {}, 'R': {}, 'V': 2}, 'R': {'L': {}, 'R': {'L': {}, 'R': {}, 'V': 6}, 'V': 1}, 'V': 5}

tree = {'L': {'L': {},  'R': {'L': {'L': {}, 'R': {'L': {'L': {}, 'R': {'L': {}, 'R': {}, 'V': 9}, 'V': 4}, 'R': {}, 'V': 13}, 'V': 2}, 'R': {}, 'V': 8}, 'V': 3}, 'R': {'L': {}, 'R': {'L': {}, 'R': {}, 'V': 6}, 'V': 1}, 'V': 5} 

How can I make a function depth_t(t), that takes as argument one of the trees above, that determines the depth of the tree recursively, without using a class? I have been struggling with this. The only thing I got was: if len(t)==0: return 0 Can somebody give me some tips?

The depths of all trees above are:

tree0 depth 0

tree1 depth 1

tree2 depth 3

tree depth 7

tree['L'] depth 6

Thanks in advance!

Neeraj
  • 975
  • 4
  • 10
Ashin
  • 1
  • Does this answer your question? [Know the depth of a dictionary](https://stackoverflow.com/questions/23499017/know-the-depth-of-a-dictionary) – jbflow Apr 09 '21 at 11:11
  • 1
    What happened when you tried to write code to solve the problem? Stack Overflow is not a code-writing service, and we cannot simply "give you some tips" on an assignment because *we have no idea why you aren't able to solve it yourself*. Please read [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – Karl Knechtel Apr 09 '21 at 11:12

1 Answers1

0

Since I'm feeling that this is a homework question, I will give you a hint: if you were somehow able to get the depth of the left and right children of a tree, then how would you combine them to calculate the depth of the tree? Say we have depth_right and depth_left. what would be the expression calculating the total depth? Once you know that, and since the left and right children are smaller instances of the problem, you can call the function recursively on the left and right children to calculate depth_left and depth_right and the problem is solved.

ahmadPH
  • 135
  • 11