Note: You are responsible for protecting your solutions to these problems from being seen by other students either physically (e.g., by looking over your shoulder) or electronically. (More information can be found in the instructions for Programming Assignment 2.)
im = empty_image(12) make_square!(im,10) make_square!(im,6)



Now that we know how our tree should be layed out, we can write a
recursive function to draw the tree. The tree [412, [268,
2000]] should appear as the image at right. The code below will
do the drawing, once you've completed the missing pieces marked by
"...". Rather than typing it in by hand, you can copy it from the
file draw_tree.rb. Notice that
draw_tree_helper contains a puts statement to help you debug
your recursive calls.
Test your draw_tree function on the following examples:def draw_tree(tree) width = 7 # works for trees up to depth 2 image = empty_image(width) depth = 0 start = 0 finish = width-1 draw_tree_helper(tree,image,depth,start,finish) plot(image,20) end def draw_tree_helper(tree,image,depth,start,finish) puts "tree=#{tree.inspect} depth=#{depth} start=#{start} finish=#{finish}" mid = (start + finish) / 2 # draw the current node image[depth][mid] = [255,0,0] if tree.kind_of?(Array) then # recursively draw the children: FILL IN THE "..." PART draw_tree_helper(tree[0], ...) draw_tree_helper(tree[1], ...) end end
draw_tree("eureka")
draw_tree(["do", ["re","mi"]])
draw_tree([["do","re"], "mi"])
draw_tree([["fee","fie"], ["foe","fum"]])
You should have a pa10 directory, containing:
Zip up your directory and upload it using the autolab system. (The autolab system will accept submissions beginning on Friday until the deadline Tuesday night.)