This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Discrete Mathematics, Spring 2004 Homework 10 Sample Solutions 7.5 #3. Write an algorithm that stores n distinct words in a binary tree T of minimal height. Show that the derived tree T , as described in the text, has height lg( n + 1) . Solution . It should be noted first that the algorithm given in the text for creating a bi- nary search tree will almost never give you one of minimal height; consider, for example, what happens when you input a sequence that is already in increasing order into that algo- rithm. Here is a possible solution. The input will be a sequence s 1 ,... ,s n , and the output will be a binary search tree T storing the input. procedure optimal bst ( s,n ) sort s 1 ,... ,s n return( o bst ( s, 1 ,n )) end optimal bst procedure o bst ( s,i,j ) if i > j then return( null ) m := ( i + j ) / 2 T := optimal bst ( s,i,m 1) T := optimal bst ( s,m + 1 ,j ) Let T be the tree whose root contains s m Let the left subtree of T be T Let the right subtree of T be T return( T ) end o bst 7.5 #6. Draw a full binary tree with height 2 and nine terminal vertices, or explain why no such tree can exist. Solution . Recall that the number of terminal vertices t and the height h of a binary tree 1 must satisfy the inequality lg t h . But lg 9 > lg 8 = 3 > 2, so we cannot have a tree satisfying the given criteria. 7.5 #19. Let N h denote the minimum number of vertices in a balanced binary tree of height h . Show that N h = 1 + N h 1 + N h 2 , for h 0....
View Full Document