HOMEWORK 4

QUESTIONS

  1. Read the following recursive implementation for Fibonacci numbers and answer the below questions.
    UPDATE:You should show all the complete execution tree, including duplicate calls.

    
    int f(int n)
    {
       if (n <= 1)
          return n;
       return f(n - 1) + f(n - 2);
    }
    
    1. Show the execution tree tracing for f(5). Use backslash and forward slash to represent right and left children; also use sufficient enough spacing to make your answer readable!
    2. How many number of base case calls are happening in this recursion? Or in other words, what is the work done?
    3. How many unique number of calls are happening?

  2. Answer the following questions regarding the following data structures:

    
    i.     9        ii.     5     iii.   4       iv.  4       v.   10    
          / \              /            /|\          / \           / \  
         7   8            0            3 4 5        1  -2         3   12
        /                /                           \ /         / \  
       6                -5                            2         1   4
    

    1. Which are valid trees? (List all correct answers.)
    2. Which are valid binary trees? (List all correct answers.)
    3. Which are valid binary search trees (BSTs)? (List all correct answers.)
    4. List the nodes of tree v. above in the order in which a pre-order traversal would visit them.
    5. List the nodes of tree v. above in the order in which a post-order traversal would visit them.
    6. List the nodes of tree v. above in the order in which an in-order traversal would visit them.
    7. List the nodes of tree v. above in the order in which a level-order traversal would visit them.

  3. Consider the following implementation of a node in a binary tree:
    
    class BinaryTreenode<T> {
        private T data;
        private BinaryTreenode<T> leftChild;
        private BinaryTreenode<T> rightChild;
    
        public BinaryTreenode(T item) {
            data = item;
            leftChild = null;
            rightChild = null;
        }
    
        public T getData() {
            return data;
        }
        
        public BinaryTreenode<T> getLeft() {
            return leftChild;
        }
        
        public BinaryTreenode<T> getRight() {
            return rightChild;
        }
        ...
    }
    

    Implement the following countLeaves function that determines the number of leaves of the binary tree. Assume that first call to this function will be with root node.
    For full credit, your code:

    • must not modify the tree.
    • must not be more than 10 lines of code and may be fewer.
    
    class BinaryTree<T> {
        BinaryTreenode<T> root;
        
        public int countLeaves() { return countLeaves(root); }
        private int countLeaves(BinaryTreenode<T> node) {
        ...
        }
    }
    

  4. Answer the following questions about a standard binary search tree. Make sure that your tree is properly readable on the CSL machine.
    1. Show the binary search tree that results from inserting the following sequence of integers into a tree that is initially empty:
          34, 32, 35, 37, 31, 36, 33, 38
          
    2. Show the binary search tree that results from inserting the following sequence of integers into a tree that is initially empty:
          9, 23, 12, 6, 10, 5, 14, 3, 1, 4
          
    3. Show the binary search tree (one of them would do) that results from deleting 32 from the tree in part a.
    4. Show the binary search tree (one of them would do) that results from deleting 9 from the tree in part b.

  5. Answer the following questions about a binary tree with N nodes. Recall that a binary tree is height-balanced if, for every node, the height of the left or right subtree is at most one more than the height of the other subtree. Equivalently, the height of the tree is as small as possible.

    Hint: You will need the following mathematical functions: floor and ceiling. Try out different example cases before finializing your answer.

    1. What is the maximum height of the tree?
    2. What is the maximum number of leaves?
    3. What is the approximate maximum height of the tree if it is balanced?
    4. What is the maximum number of leaves if the tree is height-balanced?

  6. Consider the same implementation of a node in a binary tree as in question 3. Assume that you also have the following height method implemented.
    
    class BinaryTree<T> {       
        BinaryTreenode<T> root;
            
        public int height(BinaryTreenode<T> node) {
            //Assume this method is implemented for you
        }
        boolean isBalanced(BinaryTreenode<T> node) { 
            ...
            //COMPLETE THIS METHOD
        }                                                                                                                                                                  
    }       
    
    Complete the implementation of isBalanced method that determines if the binary tree is height-balanced or not.
    For full credit, your code:

    • must not modify the tree.
    • may use any required functions from java.lang.Math.
    • must not be more than 15 lines of code and may be fewer.
     
    boolean isBalanced(BinaryTreenode<T> node) {
    

SUBMISSION