line art

Introduction to Algorithms and Data Structures, Fall 2004, Episode 5

line art
Fall 2004 Course Description Schedule Resources Students
line art

Lecture, September 27

Subject : Recursion, induction, recurrences. Binary trees.

Text : Handout on Recursion. CLRS chap. 4.1-3 pp. 62—75, Chap. Appendix B.4-B.5 pp. 1080-1090, Chap 4.1-2 pp. 62-72, Chap. 10.4 pp. 214-216, and Chap. 12.1-2. pp. 253-262. You may want to read the second handout Recurrent problems after the lecture.

Prerequisites : understanding of big-O notation, log n, definition of mathematical induction (the use will be exemplified during the lecture), basics of rooted trees (CLRS app. B5). Also it will be convenient to remember how Quicksort and Merge-Sort work.

Comments : Note that this is an exceptionally difficult episode. Try to do your best to prepare yourself. Start with skim-reading the handout on recursion. If you are unfamiliar with the recursion and induction principles, then read the handout thoroughly. We will not cover all details in the lecture, but it will greatly help you in the entire course. You are welcome to contact the teachers, if you need more explanation. Read the remaining material after you are comfortable with the handout.

There is a supplementary handout titled Recurrent Problems, which can be read after the lecture. This handout contains an alternative, perhaps more enjoyable, presentation of material on solving recurrences, as well as a wealth of additional exercises (some of them very difficult). We only recommend the second handout for advanced students, as it requires somewhat higher mathematical maturity.

Warning : If you had downloaded a note on recursion linked here before September 20, then please discard it. We are not using it in the course this year.

Time : Monday, September 27, 9:00–12:00 (3×45min), Room 2A12

Tutorial : 13:00–16:00 (3×45min), Room 2A12

line art

Assignments

The mandatory assignments should be handed in no later than October 8, at 13.00.

line art

Programming Assignments

Assignment 5.1

Files ruler.mp and star.mp contain implementations of fractal ruler and fractal star from the handout on Recursion, written in the METAPOST language. The comment on top of each file explains how you can execute the program to obtain a drawing in PostScript. You may want to experiment with parameters in order to change the shape drawn. In particular you may want to change the value of granularity in the comparison in line 34 of star.mp file. On my machine the program becomes slow for granularity of 0.2mm. Experiment with a range of values and note down the execution times (time command on Unixes is useful for this purpose). Draw a graph and guess what kind of function the asymptotic complexity is? Can you reach this result theoretically? (Hint: Assume that drawing a single rectangle takes always the same constant time, regardless of the size. Write a recurrence defining time complexity solve it and show what is the asymptotic running time of the algorithm.)

Assignment 5.2

A binary tree can be implemented in Java as follows:

class BinaryTree {
     public BinaryTree left, right;
     public int label;
     
     public BinaryTree(int l) {
          left=right=null;
          label=l;
     }
     public BinaryTree(BinaryTree ltree,BinaryTree rtree, int l) {
          left=ltree;
          right=rtree;
          label=l;
     }
}

The left (right) field contains the BinaryTree object of the respectively left and right subtree. The nil value denotes that there is no given subtree in a given node. In particular for any leaf node leaf it should be the case that leaf.left==leaf.right==nil.

  1. Construct in memory the tree presented on Fig. 12.1a in CLRS p. 254.
  2. Write a recursive procedure int sumlabels(BinaryTree t) that computes a sum of all labels in tree t.
  3. The height of the tree is defined in CLRS app. B5, p.1088. Height can be compute using the following recursive property (*): The height of the leaf node is 0. The height of an internal node in the tree is the greater of the two heights of the subtrees increased by 1. Use this recursive property to implement a method int height(BinaryTree t), computing height of tree t.
  4. Show that your implementation of height runs in time linear in the size of the tree (size of the tree is the number of its nodes). If your implementation runs in the time exponential in the size of the tree, then better improve it!
  5. Use mathematical induction to prove that property (*) does indeed hold for height of any rooted tree.
  6. Write a method int CountLeaves(BinaryTree t) that computes the number of leaves in tree t.
  7. Write a recursive method BinaryTree RandomBinaryTree(int h) that produces a randomly shaped binary tree of depth h with random labels.
  8. Write a method CompleteBinaryTree(int h), which produces a complete binary tree of depth h with random labels. You can use RandomBinaryTree and CompleteBinaryTree to test your other functions. Can you estimate (theoretically) what is the maximum depth of a complete binary tree, which can still fit in the main memory of your PC?
  9. Write a method InOrder which given a binary tree t produces a list (or array) containing the labels of tree nodes in the preorder traversal sequence.

Observe that in the pattern-speak, tree traversals are often somewhat silly referred to as the visitor pattern.