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

Did you like this challenge?

Click on a star to rate it!

Average rating 4 / 5. Vote count: 21

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!