Recursive Tree Challenge

Look at the code provided below use to draw a tree using a recursive function.

Recursive Function


A recursive function is an alternative to using iteration. A function is a recursive function if:

  • It includes a call to itself,
  • It has a stopping condition to stop the recursion.

In the code given below the drawTree() function is a recursive function because:

  • It includes a call to itself (on line 10 and also on line 13)
  • It has a stopping condition on line 7 (it will stop recursing when level reaches 0)

Your Challenge


Tweak the code provided above to create different versions of recursive trees. You could investigate having three branches per node instead of two or using some random attributes so that the tree becomes less symmetrical.

recursive-tree-1

Share Button
Posted in Computer Science, Python - Intermediate, Python Challenges

Our Latest Book