Data Structures & Algorithms

Exam 2

Sample 3, 75 Minutes


Notes:
  1. Write your name, social security number, and section in which you want your exam returned on each sheet.
  2. Begin the answer to each question on a new sheet.


  1. [18] The classes MyBinaryTreeNode and MyBinaryTree are given below. Objects of type MyBinaryTree are linked binary trees.

    public class MyBinaryTreeNode { Object element; BinaryTreeNode leftChild; // left subtree BinaryTreeNode rightChild; // right subtree } public class MyBinaryTree { BinaryTreeNode root; // root node // code you write will come here }
    You are to write a public method maxHeightDifference(). The invocation x.maxHeightDifference() returns 0 if the binary tree x is empty; otherwise, it returns the maximum difference in the heights of the left and right subtrees of any node in the tree.

    x.maxHeightDifference() = maxy is a node of x {|height(left subtree of y)
                                                - height(right subtree of y)|}


    (a)
    [16] Write Java code for the public method maxHeightDifference. You may define, implement and create additional methods and variables as needed. You may use Java's methods Math.max and Math.abs. Math.max(a,b) returns the larger of a and b, and Math.abs(a) returns the absolute value of a. You may not create any new nodes, new instances of MyBinaryTree, or invoke any methods (other than Math.max and Math.abs) for which you have not provided code. (Hint: use recursion.)

    (b)
    [2] What is the time complexity of your code as a function of the number of nodes in the binary tree?



  2. (a)
    [4] A 10 element complete binary tree is represented by the array [20, 15, 12, 8, 10, 6, 2, 4, 7, 1]. Draw the complete binary tree. Is this complete binary tree a max heap? Why?

    (b)
    [4] There are eight players named a through h in a tournament. Their values are [1, 7, 4, 3, 5, 8, 9, 2]. When two players play a match, the one with larger value wins. Draw the winner tree for the tournament. Label internal nodes with player names and external nodes with player values and names.

    (c)
    [4] Draw the loser tree (including overall winner) for the tournament described in (b). Label internal nodes with player names and external nodes with player values and names.



  3. (a)
    [8] Suppose you have a binary tree whose data fields are single characters. When the data fields of the nodes are output in inorder, the output is ABCDEFGHIJ, and when they are output in level order, the output is BAIDJCFEHG. Draw the binary tree showing the data in each node and the pointers between nodes. Show the steps used to arrive at the result.

    (b)
    [8] Draw the height biased max leftist tree that results when the max element is removed from the following height biased max leftist tree (use the strategy used in the text). Show the steps used to arrive at the result.

                     60
                    /  \   
                   /    \ 
                  55    48
                 /  \  /  \
               50  52  20  30
               /            
              40          
    


    (c)
    [4] Consider the following binary search tree. Label each node with its balance factor. Is this tree an AVL search tree? Why?

                     30                        
                    /  \                      
                   /    \                   
                  20    50                
                 /  \  /  \              
               10  25  40  60           
               / \     /   / 
              5  15  35   55            
                          /
                         54
    
    Solutions