Exercise1_Solution_Part2 - Quiz 1 Solutions Problem 4[2D...

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

View Full Document Right Arrow Icon
Background image of page 1

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

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

Unformatted text preview: Quiz 1 Solutions Problem 4 [2D points] Consider the following a. Label. the following binary tree with numbers from. the set {5, 2‘2. 9, 14, 13. 11 B} so that it is a legal binary search tree. Answer: b. Label each node in the figure above with r or 'o denoting the colors RED and BLACK, respectively, so that the tree is a legal red-black tree. Answer: {See above. Note: There‘s only one legal labeling.) :2. Make the left child of the root be the root by performing a single rotation. Draw the binary search tree that results, and label your tree with the keys from part Is it possible to label the nodes with colors so that the tree is a red-black tree? Justify.r your answer. Answer: Problem 5 [21'] points] Consider a set .5' of it 2 2 distinct numbers given in unsorted order. Each of the following four problem parts asks you to give an algorithm to determine two distinct numbers I and y in the set .5' that satisfy a stated condition. In as i'etv.r words as possible, describe your algorithm and justify its running time. To keep your answers brief, use algorithm from lectures and the book as subroutines. a. In Din} time, determine my E S such that II m e[ 2 I'm - 2i for all to, e E 3. Answer: 1-, y are the two farthest apart. Find min, max, each in Din) time. h. In flinlg n] time, determine 1,1; E .S‘ such that s 91- y and II - el 5 he - 2| for all m, z E 5 such that in 5c 2. Answer: 1.3; are the two closest together. First sort {with heapsort or merge sort) — Obi}; n}: then. scan the sortec'. numbers for the two adjacent numbers with the minimum difference -- Din}. Total: 0(nlg n} c. In Din] expected time, determine I, y E .S' such that r+y=3. where Z is given, or determine that no two such numbers exist. {Hint y = 3—2.} Answer: First put all the numbers into a hash table of size n. Expects-ti hashing time: n-Ofl] = Din}, assuming simple uniform hashing and collisions resolved by chaining, for example. Then for each .1: E 5, look for Z — :t in the hash table. It takes at most r. lookups to either find one or tr;r them all. Expected lockup time: {n} - Gil} = Din] Expected total time.- Gin] ti. In D[n] tin-rer determine any E S such that 1 .n— *5 (muz— min 2) I yl _ n ----1 :es‘ 1E5 ' r'.e., determine any two numbers that are at least as close together as the average distance between ooneeoutive numbers in the sorted order. [Hintr Use divide and conquer.) Answer: The idea: Repeatedly split the set in half by partitioning around the median and ret:ursi'|.'el‘_i.r look in. the half with the smaller average dis- ' tanoe. That half must have a pair oi" numbers at least as close together as the original average distance. since {see hint for problem 3d] for an}.r set of numbers. at least one number must be 5 the average. When the recursion gets down to just 2 numbers, these are the anewer. Note one subtle point: The median must be included in both sides of the partition. so that it. wfli be in a subset with each of its neighbors: otherwise if the answer includes that median. it might not be found. Algorithm: Input is in array :1. Define aoemge-distoneebe] = fi (mania z — mint.“ e]. where n = iengtMA]. Takes Eli's} to compute {to find min and max). Answer is PIXEL-1.], where Finntaii if lengthiA] = 2 then return A Find median .1: of A in linear time Partition At around a: into A1 and A: in linear time and include 3 in both A1 and air; Calculate auemge-tiisteneeMfl and onemge-distoneeMfl in linear time 1:: average-distance needn't reenrnpute rnart[:‘l1]I = Ininifiigl = I if enemye-disteneeEAfl If. onemge-e'istaneehiifi then Finnish) else PINDMfl Running time: T{n} = Tfnfl) + Elfin) = Ei-[n] {roaster theorem case 3) ...
View Full Document

This note was uploaded on 10/21/2011 for the course COT 6401 taught by Professor Staff during the Spring '09 term at FAU.

Page1 / 3

Exercise1_Solution_Part2 - Quiz 1 Solutions Problem 4[2D...

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