Recursive Tree, Part 2

  1. That tree2a block is pretty long and repetitive. But we can simplify it if we notice that in two places it has a move forward/move back pair of blocks, and that this is what tree1 does! So we can use tree1 to shorten tree2. Compare the code below to convince yourself that the new tree2 will work the same as the original tree2a.

    tree1
    tree2a (old version)
    tree2 (new version)
    There should be an image here
    There should be an image here
    There should be an image here
    Note that the tree blocks inside this tree2 script are tree1 blocks, not tree2 blocks! So there's no mysterious tree-using-tree situation here; it's not unusual for one already-written block to be used in another  block's script.
  2. Why are we multiplying size by 0.75?
  3. What would happen if we used a negative size?
  4. Why is the right turn amount 30 degrees instead of 15?
  5. Make a tree3 block that uses the tree2 block, on the same pattern, and see if you get the result that you expect.
  6. Make a tree4 block that uses the tree3 block and try it out.
  7. If you can stand it, make a tree5 block that uses the tree4 block and try it out.

This would be a good time to save your project.

  1. These blocks all look exactly the same, don't they? So it makes sense to wonder if we can replace them all with a single tree block. Here's the big idea: We can write a tree block that uses itself in its own script provided that it knows how many levels it's expected to draw!  So, in addition to the size input, it'll have a levels input:

    tree (_) levels, size (_)

    In the earlier steps, tree3 used tree2; tree2 used tree1. If you actually had the patience to work up to a tree437 block, what block would it call for its two branches? To generalize the pattern, tree will use tree, but reducing the number of levels by 1:

    recursive tree, first version

    For example, given a levels input of 5, tree will call two trees with a levels input of 4. Those two trees in turn will each call two more trees with a levels input of 3, and so on.

    Now try the tree block. What happens? Why does it happen? When you know the answer, go to the next activity.