This community-built FAQ covers the “Recursive Tree Building” exercise from the lesson “Decision Trees”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Data Science
Machine Learning
FAQs on the exercise Recursive Tree Building
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (
) below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply (
) below!
Agree with a comment or answer? Like (
) to up-vote the contribution!
Need broader help or resources? Head here.
Looking for motivation to keep learning? Join our wider discussions.
Learn more about how to use this guide.
Found a bug? Report it!
Have a question about your account or billing? Reach out to our customer support team!
None of the above? Find out where to ask other questions here!
In part 7 of this exercise, we’re calling build_tree
inside its own definition - what is that doing exactly?
Is it the case that because there already is a return
statement in the definition, the function calls that up again? (seems to be the case from the output of the print call on it)
3 Likes
I don’t get it either! @toastedpitabread I know you are quite good with Python, could you help us? I get it that the objective is to return the branches with a Counter(labels) in the end, but I’m not getting how that translates into the tree we saw at the beginning of the exercise, I can’t visualize the following as a tree:
Splitting
--> Branch 0:
Counter({'unacc': 4})
--> Branch 1:
Splitting
--> Branch 0:
Counter({'good': 1})
--> Branch 1:
Counter({'acc': 1})
--> Branch 2:
Counter({'unacc': 1})
--> Branch 2:
Splitting
--> Branch 0:
Counter({'vgood': 1})
--> Branch 1:
Counter({'acc': 1})
--> Branch 2:
Counter({'acc': 1})
I can’t access the code because I don’t have pro but it sounds like it’s using a recursive function.
I know the comp sci path in CC has a chapter on recursion, and binary trees, so that might be helpful to look up too.
If one hasn’t dealt with recursion before I personally find it easiest to write out by hand what happens (maybe even making a simpler model at first to get it). Like, if this is the first time encountering recursion, it might be better to experiment with a small function that emulates the factorial notation in math recursively.
There’s a lot of videos online explaining recursion trees, here’s one from MIT:
2 Likes
Yea I don’t get it either, how can you have multiple returns in a function?
It’s not about having multiple returns, it’s about having recursive returns.
def hello(repeat):
if(repeat==0):
return
print("hello")
return(hello(repeat-1))
hello(5)
# output
# hello
# hello
# hello
# hello
# hello
1 Like