set6_sol

set6_sol - CS112 Spring 2011 Problem Set 6 Binary Search...

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

View Full Document Right Arrow Icon
CS112 Spring 2011: Problem Set 6 Binary Search Tree and AVL Tree ------------------------------------------------------------------------ 1. Given the following sequence of integers: 5, 15, 35, 10, 70, 25, 30, 75 Starting with an empty binary search tree, insert this sequence of integers one at a time into this tree. Delete 35 from the final tree: find the node (y) the largest value in its left subtree, write y's value over 35, and delete y. How much work in all (locating 35, then locating y , then deleting y) did it take to complete the deletion? Assume 2 units of work to perform an item-to-item comparison, and 1 unit of work for a pointer assignment. *SOLUTION* Here's the tree after adding all the items: 5 \ 15 / \ 10 35 / \ 25 70 \ \ 30 75 To delete 35, here is the work done: * *Locating 35*: Number of comparisons is 5. Number of pointer assignments is 6: to move two pointers (prev and ptr) down the tree, with 2 initial assignments, then 2 assignments to move to 15, then 2 assignments to move to 35. And one assignment to hold on to 35. * *Locating y*: The largest value in the left subtree of 35 is 30. Locating this requires 4 more pointer assignments. (Move prev and ptr from 35 to 25, then to 30.) * *Overwriting 35 with 30*: We don't count this work since it happens only once. * *Deleting y (30)*: Since this is a leaf node, one pointer assignment to set 25's right child to null. So in all 5 comparisons and 6+1+4+1 = 5*2 + 12*1 = 22 units of work total. ------------------------------------------------------------------------ 2. * You are given the following *BSTNode* class: public class BSTNode<T extends Comparable<T>> { T data;
Background image of page 1

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

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

This document was uploaded on 11/01/2011 for the course 198 112 at Rutgers.

Page1 / 5

set6_sol - CS112 Spring 2011 Problem Set 6 Binary Search...

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

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