{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

homework8_001 - 15-121 FALL 2009[CORTINA HOMEWORK 8 due...

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

View Full Document Right Arrow Icon
15-121 FALL 2009 [CORTINA] HOMEWORK 8 - due Friday, November 13 by 11:59PM ELECTRONIC HANDIN AVAILABLE BY MONDAY 5PM PROBLEMS (10 pts) For the following problems that require recursion, your solution may not use iterative looping constructs like for and 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 ABCDEFGHIJK and 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 mirror that 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 BinarySearchTree class. E is assumed to be Comparable . What does the following mystery method return in general, assuming the data values
Background image of page 1

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

View Full Document Right Arrow Icon
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 BinarySearchTree class. E is 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. Operation Balanced BST Non-balanced BST Traverse Find Add Remove 6. (1.5 pts) Rewrite the find method for the BinarySearchTree class 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.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}