Reference no: EM133116101
COMP.4040 Analysis of Algorithms - University of Massachusetts Lowell
Question 1: Suppose that we have numbers between 1 and 1000 in a binary search tree, and we want to search for the number 363. Which of the following sequences could not be the sequence of nodes examined? Justify.
(a)2, 252, 401, 398, 330, 344, 397, 363.
(b)924, 220, 911, 244, 898, 258, 362, 363.
(c)925, 202, 911, 240, 912, 245, 363.
(d)2, 399, 387, 219, 266, 382, 381, 278, 363.
(e)935, 278, 347, 621, 299, 392, 358, 363.
Question 2: The lowest common ancestor of two nodes v1 and v2 (denoted by LCA(v1, v2)) of a rooted tree that has both v1 and v2 as descendants.
(a) Write pseudocode that computes LCA (v1, v2) in a given BST T .
(b) What is the time-complexity of your algorithm? Give the time complexity as a function of n = |T | and of h, the height of T .
Question 3: Given a binary tree T give a simple algorithm to check whether T is a BST. Try to use functions already de ned in class. Argue correctness with one or two sentences. What is the time complexity?
Question 4: Show the BST obtained by successively inserting the keys 45, 38, 31, 41, 12, 39, 40, 42 into an initially empty BST. Then, show the resulting BST after deleting 38.
Question 5: Show the red-black trees that result after successively inserting the keys 41, 38, 31, 12, 19, 8 into an initially empty red-black tree.
Question 6: You are given a red-black tree, T, and some value v (not contained in T ). You must split T into two red-black trees, such that one contains all values less than v, and the other contains all values greater than v.
Try to get logarithmic time, but if you can get anything sub-linear, that's also good.