Homework 5: Trees and Heaps

Due: Saturday, April 28, 8:59:59pm


Instructions

You should submit answers to the following questions as either a text document or PDF format file. (Submitting any other format, MS Word, for example, will be grounds for receiving a 0.) The file must be named "hw5.txt" or "hw5.pdf". Copy the file into the hw5 directory of your shared submission directories to submit.

Where the question asks for you to draw a tree or heap, you can use any drawing app, or even draw freehand and scan/take a snapshot/screenshot. If you do that, it would be easiest to generate a PDF file with the pictures inserted inline.

The following tree diagram template (directly cut-and-pasteable, available for download in the file hw5-tree-template.txt, and also copyable from the submission folder directory "00Hw5" on the GL systems), contains an ASCII art skeleton for a binary tree. You can cut-and-paste that into your file and edit it to diagram your tree/heap. Be sure to replace the 'X' node names with appropriate values as required by the question.



                                X
                               / \
                   ___________/   \___________  
                  /                           \
                 /                             \
                X                               X               
               / \                             / \             
           ___/   \___                     ___/   \___
          /           \                   /           \       
         /             \                 /             \     
        X               X               X               X   
      _/ \_           _/ \_           _/ \_           _/ \_
     /     \         /     \         /     \         /     \
    X       X       X       X       X       X       X       X
   / \     / \     / \     / \     / \     / \     / \     / \
  X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X
 / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ 
 X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X


Question 1

An AVL tree uses the simple recursive structural constraint that the heights of the left and right subtrees of any node can differ by at most 1. However, this does not put a fixed constraint on the maximum difference in depth between any two given leaves of the AVL tree. In fact, the difference in depth between the shallowest and deepest leaves can be arbitrarily large given a large enough tree--but there are relative limits.

a) Determine the minimim possible depth of other leaves as a function of dmax, the depth of the deepest leaf. (I.e., state a claim like "the depth of the shallowest leaf can be no less than the depth of the deepest leaf minus 2 (dmax - 2))

b) Draw an AVL tree where there is at least a 3-level difference between some pair of leaves. Make sure that what you drew is a legal AVL tree!


Question 2

[If you are using the tree template, trim unnecessary branches, and replace all of the 'X's with either 'B' or 'r' (don't use uppercase 'R'--too similar to 'B')]

For this question, assume we are talking about the version of red-black trees with dummy leaf nodes, i.e., all leaf nodes are black, and leaf nodes do not contain data. Also, we here define the black depth of a leaf to be the number of black nodes in the path from the root to the leaf, including the root but not counting the leaf node itself. Recall that in a red-black tree, all leaves in our tree must have the same black depth.

a) What is the maximum true height, i.e., counting both red and black nodes, of a red-black tree with a black depth of 3? (Recall that the height of a tree is the number of edges between the root and the deepest leaf.) Another way of stating the question would be: what is the depth of the deepest possible leaf in a reg-black tree of black depth 3

b) What is the absolute mimimum number of internal nodes (i.e., not counting the dummy leaves) needed to construct a tree as described in (a) above?

c) Draw a diagram of your red-black tree from part (b). You do not have to draw the dummy leaf nodes.


Question 3

Using the keys 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 and 12, draw a binary max heap that stores these keys in some legal heap order, and has the keys 3, 2 and 1 in left-to-right order in the last three positions (i.e., the rightmost 3 nodes at the bottom-most level), such that after 2 consecutive calls to deleteMax(), the bottom-most level consists of the keys 1, 2 and 3, in that left-to-right order.



Question 4

[If you are using the tree template for either part (a) or (b) below, make sure to trim unnecessary branches, and replace all of the 'X's with real numerical values]

a) Using the keys 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 and 12, draw a binary min heap that stores these keys in some legal heap order, where deleting the key 6 would cause the replacement item to bubble up.

b) Using the keys 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 and 12, draw a binary min heap that stores these keys in some legal heap order, where deleting the key 8 would cause the replacement item to tricke down.

c) Is it possible to construct a single min heap where deleting a 6 would cause the replacement node to bubble up, but where instead deleting an 8 would cause trickling down? (The question is assuming we would delete either one or the other, but not both.) Explain why/why not.