Data Structures & Algorithms

Exam 2, Sample 2

75 Minutes


Points allocated to each part are shown in square brackets. Answers will be graded on correctness as well as efficiency, elegance, and other measures of quality.

  1. An abbreviated version of the classes BinaryTreeNode and LinkedBinaryTree of the text is given below.

    public class BinaryTreeNode { // package visible data members Object element; BinaryTreeNode leftChild; // left subtree BinaryTreeNode rightChild; // right subtree } public class LinkedBinaryTree implements BinaryTree { // instance data member BinaryTreeNode root; // root node }
    You are to write a public method elementAtLevel(int theLevel) which returns null if the binary tree has no element at level theLevel; otherwise, it returns an element at this level.
    (a)
    [16] Write Java code for the public method elementAtLevel. You may define and implement additional methods as needed. You may not create or delete any nodes or invoke any methods 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. There are eight players named a through h in a tournament. Their values are [4, 7, 3, 6, 5, 1, 8, 2]. When two players play a match, the one with smaller value wins.
    (a)
    [4] Draw the winner tree for the tournament. Label internal nodes with player names and external nodes with player values and names.
    (b)
    [4] Draw the loser tree for the tournament. Label internal nodes with player names and external nodes with player values and names.


  3. (a)
    [4] Draw a tree that represents a set of elements. The depth of your tree should be at least 7. The tree need not be one that results from the use of either the weight or the height rule. Show the tree that results following the operation Find(e), where e is an element at level 7 and we are using path halving.
    (b)
    [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 preorder, the output is BAHCEDGFJI. Draw the binary tree showing the data in each node and the pointers between nodes. Show the steps used to arrive at the result.
    (c)
    [8] Draw the height biased max leftist tree that results when the height biased max leftist trees below are melded by following their rightmost paths (use the strategy used in the meld algorithm of the text). Show the steps used to arrive at the result.

                     60                        70
                    /  \                      /  \
                   /    \                    /    \
                  55    48                  61    54
                 /  \  /  \                /  \   / \
               50  52  20  30             58  59  53 45
               /                          /
              40                         56
    
Solutions