Hw3 Updated

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CSE 332 Data Abstractions, Winter 2014 Homework 3 (Updated)

Due: Wednesday, January 29 at the BEGINNING of lecture. Your work should be readable as well as correct. You should refer to the written homework guidelines on the course website for a reminder about what is acceptable pseudocode. Please write your section at the top of your homework.

Update: Homework 3 consists of the first three problems (the ones on AVL trees). Problem 4 is being transferred to assignment 4.

Problem 1: AVL Insertion


Show the result of inserting 47, 8, 5, 9, 4, 6, 34, 2, 1 and 3 in that order into an initially empty AVL tree. Show the tree after each insertion, clearly labeling which tree is which.

Problem 2: Verifying AVL Trees


Give pseudocode for a linear-time algorithm that verifies that an AVL tree is correctly maintained. Assume every node has fields: key, data, height, left, and right and that keys can be compared with <, = =, and >. The algorithm should verify all of the following: The tree is a binary search tree. The height information stored in each node is correct. Every node is balanced. Your code should throw an exception if one of these trees is violated; if the tree is a valid AVL tree, no exception should be thrown.

Problem 3: AVL Rotation Scenarios


The diagram below shows the general case for performing a case 4 (right-right) rotation; an insertion has taken place in subtree Z, and an imbalance has been detected at node a. Each triangle is assumed to be a subtree that is AVL balanced. Each triangle is labeled with its height so you can clearly see that after the rotation is applied, the resulting entire tree will be AVL balanced. Assume subtree Z has height h before the insertion, and height h+1 after the insertion, as shown below. What you need to do for this problem is argue that subtrees X & Y must have height h; that is, show that it is not possible for either to have height h-1 or h+1. State what the problems are with the two specific height scenarios of h-1 and h+1 for both X and Y. While you are describing why X cannot be height h-1 or h+1, dont assume anything about the height of Y and vice versa. A sentence or two for each of the four scenarios is fine. A proof is not required.

Problem 4 is NOT due this week, it will be added as problem 1 for HW 4. Problem 4: B-Tree Insertion
Show the result of inserting 38, 11, 17, 3, 42, 51, 7, 14 & 15 in that order into an initially empty B tree with M = 3 and L = 2. (Recall the text, lecture, and this problem call a B tree what many call a B+ tree.) Show the tree after each insertion, clearly labeling which tree is which. In an actual implementation, there is flexibility in how insertion overflow is handled. However, in this problem, follow these three guidelines: Always use splitting (not adoption). Split leaf nodes by keeping the smallest 2 elements in the original node and putting the 1 largest element in the new node. Split internal nodes by keeping the 2 children with the smaller values attached to the original node and attach the 2 children with the larger values to the new node.

You might also like