Recursive Tree
Brian's Notes from late July 2016:
- Think about adding to 6.1.1 the sentence "TREE 4 100 should do the same thing as TREE4 100" because we're really not clear enough about what the level input means.
The branching pattern on many plants has fractal structure: the smaller parts are like miniature copies of the whole plant.
A fractal is an infinite repeating pattern made up of copies (or slight variations) of the same shape. In this picture, the green branch is (essentially) the same shape as the entire picture.

In this lab, you will build a tree using blocks within blocks to create the branches. Each branch will be a smaller copy of essentially the same tree.
State transparency—putting everything (sprite, pen, etc.) back exactly as it was when you started—is important when blocks depend on other blocks. Usually, this means returning the sprite to the same position and direction, and the pen to the same color and size, as they were.
- Make a
tree 3
block that uses the tree 2
block as branches.
- Make a
tree 4
block that uses the tree 3
block as branches.
-
If you like, make a
tree 5
block that uses the tree 4
block. Running
should produce a result like this:
These blocks all look essentially the same except that tree 5
uses tree 4
, while tree 4
uses tree 3
, and so on. So it makes sense to wonder if we can replace all these blocks with a single tree
block, using a variable for the number that changes.
Using a block inside of itself is called recursion.
-
This is the general idea for the recursive version of
tree
. Build it:
-
Now run
tree level: 9 size: 50
. It doesn't work yet. All you see is:
Try to debug the script. What's going wrong?
tree 1 size: (size)
, so that it later changes the hard coded numbers into a variabletree (level) size: (size)
. Then text needs to be brought in line.