Homework8 - 15-121 FALL 2009[REID-MILLER]HOMEWORK 8 due Thursday November 12 by 11:59PMELECTRONIC HANDIN AVAILABLE BY SUNDAY BY 5PMPROBLEMS(10

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-121 FALL 2009 [REID-MILLER]HOMEWORK 8 - due Thursday, November 12 by 11:59PMELECTRONIC HANDIN AVAILABLE BY SUNDAY BY 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 worst-case runtime complexity in big-O 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 worst-case runtime complexity in big-O 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 worst-case runtime complexity using Big-O notation for the following operations on the binary search tree if the tree is balanced and if the tree is not-balanced. A binary tree is balanced if every level is full except possibly the last level. OperationBalanced BST Non-balanced BSTTraverseFindAddRemove6. (1.5 pts) Rewrite the findmethod for the BinarySearchTreeclass discussed in lecture so that it is iterative, not recursive. 7. (2 pts) a. Use the remove algorithm discussed in class to remove a node from the binary search tree shown below. Each time you remove a node, give the postorder traversal of the resulting tree, and indicate which node is returned by the recursive call that finds the node to remove. i.Remove node 53 from the tree....
View Full Document

This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reid-miller during the Spring '09 term at Carnegie Mellon.

Page1 / 6

Homework8 - 15-121 FALL 2009[REID-MILLER]HOMEWORK 8 due Thursday November 12 by 11:59PMELECTRONIC HANDIN AVAILABLE BY SUNDAY BY 5PMPROBLEMS(10

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online