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: 15121 FALL 2009 [CORTINA]HOMEWORK 8  due Friday, November 13 by 11:59PMELECTRONIC HANDIN AVAILABLE BY MONDAY 5PMPROBLEMS (10 pts)For the following problems that require recursion, your solution may not use iterative looping constructs like forand while. 1. (1 pt) a. Write the preorder, inorder and postorder traversals of the binary tree shown below. b. A binary tree has a preorder traversal of ABCDEFGHIJKand a postorder traversal of CEFDBJKIHGA. What is its inorder traversal? 2. (1.5 pts) A binary tree is modeled using the following class: public class BinaryTree<E> {protected BTNode<E> root;public void mirror() {// you will complete this method}...protected static class BTNode<E> {protected E data;protected BTNode<E> left;protected BTNode<E> right;...}}Complete the method named mirrorthat alters the tree so that the final tree is a mirror image of the original tree. For example, if we use on this method on the tree shown on the left, we end up with the tree shown on the right. (NOTE: You will need a helper method here.) 3. (1 pt) The mystery methods below are part of the BinarySearchTreeclass. Eis assumed to be Comparable. What does the following mystery method return in general, assuming the data values in the tree are not null? What is its worstcase runtime complexity in bigO notation if the binary search tree has n nodes? Explain your answers. public E mystery() {if (root == null) return null;return mystery(root);}private E mystery(BTNode<E> node) {if (node.right == null)return node.data;return mystery(node.right);}4. (1 pt) The mystery methods below are part of the BinarySearchTreeclass. Eis assumed to be Comparable. What does the following mystery method output in general, assuming the data values in the tree are not null? What is its worstcase runtime complexity in bigO notation if the binary search tree has n nodes? Explain your answers. public void mystery2() {return mystery2(root);}private void mystery2(BTNode<E> node) {if (node == null) return;mystery2(node.right);System.out.println(node.data);mystery2(node.left);}5. (2 pts) A binary search tree has n nodes. Determine the worstcase runtime complexity using BigO notation for the following operations on the binary search tree if the tree is balanced and if the tree is notbalanced. A binary tree is balanced if every level is full except possibly the last level. OperationBalanced BST Nonbalanced BSTTraverseFindAddRemove6. (1.5 pts) Rewrite the findmethod for the BinarySearchTreeclass discussed in lecture so that it is iterative, not recursive. 7. (2 pts) Use the remove algorithm discussed in class to remove a node from the binary search tree shown below. For each answer, give the postorder traversal of the resulting tree, and indicate which node is returned by the recursive call that finds the node to remove....
View
Full
Document
This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reidmiller during the Spring '09 term at Carnegie Mellon.
 Spring '09
 ReidMiller
 Recursion

Click to edit the document details