sol.ex05[1] - Data structures, Spring 2008 Solution 5:...

Info iconThis preview shows pages 1–3. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Data structures, Spring 2008 Solution 5: Heap, AVL Tree Instructions • Exercises in this course should be submited to the course box in Ross -2. • Cooperation between students is permitted according to the course guidelines. We remind that: 1. Exercises are for you, benefit from them! 2. Each student must submit his own exercises. 3. Is is not permitted to share any written material. 4. Cheating is a serious university offense which may lead to severe consequences. It will be treated accordingly by course staff. • Write your name, ID, and CS login at the top of the first page (3pt). • Give correctness and complexity proofs for every algorithm you write. • Prove everything. If you suppose anything - write so explicitly. • For every problem find the most efficient algorithm. A non-efficient algorithm will be considered as an incomplete answer. • In this exercise there are 100 regular points! and 3 bonus. Binary Search Tree 1. (a) (5 pt) Suppose that we have numbers between 1 and 1000 in a binary search tree (BST) and want to search for the number 363. Which of the following sequences could not be the sequence of nodes examined? i. 2, 252, 401, 398, 330, 344, 397, 363. ii. 924, 220, 911, 244, 898, 258, 362, 363. iii. 925, 202, 911, 240, 912, 245, 363. iv. 2, 399, 387, 219, 266, 382, 381, 278, 363. v. 935, 278, 347, 621, 299, 392, 358, 363. solution: ( iii ) , ( v ) are sequences that can not be the series of nodes examined. Suppose in our BST if y is a node in the left subtree of x and z is a node in the right subtree of x, then key [ y ] < key [ x ] ≤ key [ z ]. In ( iii ), 912 appears in the subtree rooted 5: Heap, AVL Tree-1 at the left son of 911, which is not legal. Similarly 299 is smaller than 347 but is in the right subtree. (b) (9 pt) It is possible to convert any binary search tree with n nodes to any other BST with n nodes using rotations. The number of rotations required for this in the worst case is: i. Θ(log n ) ii. Θ( n ) iii. Θ( n log n ) iv. Θ( n 2 ) Prove your answer. The correct answer is Θ( n ). The algorithm to convert a tree to a left-linked list is the flowing. Consider the path from the root of the tree to the minimal node. While there is a node on this path with a right child perform a left rotation on that node....
View Full Document

This note was uploaded on 03/24/2010 for the course CS cs 5329 taught by Professor Wang during the Spring '10 term at Southwest Texas Junior College.

Page1 / 6

sol.ex05[1] - Data structures, Spring 2008 Solution 5:...

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

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