{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

homework8 - 15-121 FALL 2009[REID-MILLER HOMEWORK 8 due...

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

View Full Document Right Arrow Icon
15-121 FALL 2009 [REID-MILLER] HOMEWORK 8 - due Thursday, November 12 by 11:59PM ELECTRONIC HANDIN AVAILABLE BY SUNDAY BY 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
Image of page 1

Info icon This 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) 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.
Image of page 2
i. Remove node 53 from the tree.
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern