0 Up votes0 Down votes

11 views3 pages2sol

Sep 10, 2017

© © All Rights Reserved

PDF, TXT or read online from Scribd

2sol

© All Rights Reserved

11 views

2sol

© All Rights Reserved

- American Computer Science League (ACSL) Rules and Handbook
- r059210503 Advanced Data Structure
- ISROTechnicalPaper2009
- (www.entrance-exam.net)-ISRO Sample Paper 6
- Data Medium Exchange
- 1-D75477GC10_TOC
- Stc
- Locators
- Report Study on Cooperative Data Dissemination_153903015.pdf
- NTrees_1
- Improving Multicast Tree
- LSD
- The Pain With Using Singleton(WebDynpro)
- cisco-pp2
- 2nd unit - 2m
- 20 Binary Search Tree
- phani
- Bst
- Hashing
- Lecture16-BSTrees.ppt

You are on page 1of 3

For each question, please write up detailed answers carefully. Make sure that you use notation and terminology

correctly, and that you explain and justify what you are doing. Marks may be deducted for incorrect/ambiguous

use of notation and terminology, and for making incorrect, unjustified or vague claims in your solutions.

1. Let D be an ordered Dictionary with n items implemented with an AVL tree. Show how to implement the

following method for D in time O(log n):

countAllinRange(k1 , k2 ): return the number of items in D with key k such that k1 6 k 6 k2 .

note: you may assume that the AVL tree supports a data field at each node x which stores the number of

nodes in the subtree rooted at x.

Solution : We will show how to implement a method AllinRangefromNode(v, k1 , k2 ) where v is a node in D.

This is enough of course since AllinRange(k1 , k2 ) = AllinRangefromNode(root, k1 , k2 ). In addition we will implement

countAllBiggerThan(v,k) and countAllsmallerThan(v,k) that return the number of items in the subtree of D

rooted at node v, with keys bigger-than or smaller-than k respectively.

We denote by numnods(v) the data field supported by the AVL that stores the number of nodes in the subtree

rooted at v.

AllinRangefromNode(v, k1 , k2 ) {

if v = N IL return 0

if (v.key < k1 ) return AllinRangefromNode(rightchild(v), k1 , k2 )

if (v.key > k1 ) return AllinRangefromNode(lef tchild(v), k1 , k2 )

else return 1+ countAllBiggerThan(lef tchild(v), k1 ) + countAllSmallerThan(rightchild(v), k2 )

}

Why does this work? If v.key < k1 then we can safely ignore the elements in the left subtree of v and continue

with the same query to the right child of v. A similar argument justifies the third line. If, however,k1 6 v.key 6 k2

then v should be counted, and for the two subtrees of v we only need to count the elements that are bigger than k1

in the left subtree, and smaller than k2 in the right subtree.

How long does it take? We can bound the running time of this method by the height of the tree, that is

O(logn) plus the running time of the other two methods. This is since every time the conditions in line 2 or line

3 are satisfied in the recursion tree, we move to a deeper node. Hence there can be as many recursive calls to

AllinRangefromNode as the height of the tree.

Now we define countAllBiggerThan(v, k) and countAllSmallerThan(v, k).

countAllBiggerThan(v, k) {

if v = N IL return 0

if (v.key < k) return countAllBiggerThan(rightchild(v), k)

else return 1 + countAllBiggerThan(lef tchild(v), k) + numnodes(rightchild(v))

}

countAllSamllerThan(v, k) {

if v = N IL return 0

if (v.key > k) return countAllSmallerThan(lef ttchild(v), k)

else return countAllBiggerThan(rightchild(v), k) + numnodes(lef tchild(v))

}

CSC 263 H5S Homework Assignment # 2 solutuions Spring 2010

Why do they work? We analyze the first of the two methods (the other is proved similarly). If the condition

in line 2 holds then we can avoid looking at the left subtree as all elelemtns there are clearly too small. Otherwise,

all elements of the right subtree must be counted, the root must be counted and in addition all sufficiently large

elements in the right subtree should, hence the recursion.

How long do they take? As before its clear that the recursion can be no longer than the height of the tree,

as regardless of whether the condition in line 2 holds, we move to a child node of the original node.

2. Given two AVL trees T1 and T2 , where the largest key in T1 is less than the smallest key in T2 , Join(T1 , T2 )

returns an AVL tree containing the union of the elements in T1 and T2 .

Give an algorithm (in pseudocode) for Join that runs in time O(log n), where n is the size of the resulting AVL tree.

Justify the correctness and efficiency of your algorithm.

Solution:

Begin by computing the heights h1 of T1 and h2 of T2 . This takes time O(h1 + h2 ): You simply traverse a path

from the root, going to left child if the balance factor is -1, to the right child if it is positive, and to any of the

children if the balance factor is 0, until you reach a leaf. Assume that h1 > h2 ; the other case is symmetric.

Next, DELETE the smallest element x from T2 , leaving T20 of height h. This takes O(h2 ) time.

Find a node v on the rightmost path from the root of T1 , whose height is either h or h + 1, as follows:

v root(T1 )

h0 h1

while h0 > h + 1 do

if balance factor (v) = -1

then h0 h0 2

else h0 h0 1

v rightchild(v)

Let u denote the parent of v.

Form a new tree whose root contains the key x, whose left subtree is the subtree rooted at v and whose right subtree

is T20 .

Note that this is a valid binary search tree, since all the keys in the subtree rooted at v are in T1 and, hence, smaller

than x, and, by construction, x is smaller than or equal to all elements in T20 . The balance factor of the root of this

new tree is h h0 , which is either -1 or 0, so this new tree is a valid AVL tree. The height of this new tree is h0 + 1,

which is 1 bigger than vs height. Let the root of this new tree be the right child of u, in place of v. Again, since all

keys in this new tree are at least as big as u, this results in a valid binary search tree. This part of the construction

takes constant time.

Now, as in the INSERT algorithm, we go up the tree, starting at u, fixing balance factors and perhaps doing

a rotation. This takes O(h1 ) time. Note that the correctness follows from the condition at u before this process is

begun is a condition that can arise during the INSERT algorithm.

Since h1 , h2 O(log n), the total time taken by this algorithm is in O(log n).

3. Suppose we are using a priority queue Q to schedule jobs on a CPU. Job requests together with their priorities

are inserted into the queue. Whenever the CPU is free, the next job to execute is found by extracting the highest

priority job in Q. Using a heap implementation for Q the scheduler can both extract the next job from Q and add

a new job to Q in time O(log n) where n is the number of pending jobs in the queue.

CSC 263 H5S Homework Assignment # 2 solutuions Spring 2010

However, in practice, we want our scheduler to be able to do more than just insert jobs and extract the highest

priority job. In particular, we want our scheduler to be able to remove a job x from the job queue (Delete(x)) as

well as change the priority of a job x to some new value k (Change-Priority(x, k)). Show how to add the operations

Delete(x) and Change-Priority(x, k) to the heap data structure so that (a) they both run in time O(log n) and (b)

the resulting data structure after executing either these operations is still a heap. For both operations, you may

assume that parameter x gives you the index corresponding to job x in the array representation of the heap.

Solution:

For the delete operation, we start with taking the last item (lets say it has key k) and placing it in position

x. So now the original key of x is gone, and instead we have y there, and in addition the heap is of the

right size. But of course, its not necessarily a heap: y may agree or disagree with its parents/children. If y

is smaller than the key of the parent, we perform up-heap bubble. Otherwise, we perform down-heap bubble

(which, in the case that y is smaller than the keys of the children, will stop right away). Since there is either

up-heap or down-heap bubble, this will have ruinning timeof O(log n). The initial swap is of course O(1).

The change-priority is similar but even easier: All you have to do is change the key of poistion x to k, and

continue as the second phase of the delete(x) method above. In fact, you can even implement delete(x) by

performing the swap, and then using change-priority as a black box if you wish! Again, clearly this takes

O(log n).

4. [10 marks]

Consider two Binomial Heap H1 and H2 . You are required to create the union H of H1 and H2 and also to find the

minimum key in H. A standard way would be to perform

H Union(H1 , H2 )

m LookupMin(H).

m1 LookupMin(H1 )

m2 LookupMin(H2 )

m min(m1 , m2 )

H Union(H1 , H2 ).

Give an example of heaps H1 and H2 in which the first sequence of operations is faster, and one in which the

second is faster. Justify your answer.

Solution: Notice that the union operation will have the same inputs in both cases (the lookupMin operations

do not change the Binomial heaps). So the question boilds down to comparing the cost of the lookupMin operations.

For a Binomail heap J, denote by `(J) length of the linked list of Binomial trees in H. Recall that the cost of

lookupMin(J) is simply `(J). Hence, if we denote by u the running time of persorming the union of H1 and H2 we

get that the first method would run in time

u + `(H)

and the second in time

`(H1 ) + `(H2 ) + 1 + u.

An example in which the first method is faster is one where `(H) is bigger than `(H1 ) + `(H2 ) + 1 and vice versa.

If H1 and H2 contain list of binomial trees of all heights upto some k, then H will contains only one binomial tree

(of height k + 1, and so `(H) = 1 and `(H1 ) + `(H2 ) + 1 = 2k + 1. On the other hand, it is always the case that

`(H) 6 `(H1 ) + `(H2 ) (do you see why? equality will happen only when the binomial trees of H1 are all different

than those of H2 ) and so an example of the opposite kind does not exist.

- American Computer Science League (ACSL) Rules and HandbookUploaded bybellataco
- r059210503 Advanced Data StructureUploaded bySRINIVASA RAO GANTA
- ISROTechnicalPaper2009Uploaded byypragati
- (www.entrance-exam.net)-ISRO Sample Paper 6Uploaded byMradul Saxena
- Data Medium ExchangeUploaded byRavKum
- StcUploaded byHoàng Công
- LocatorsUploaded bysonal
- Improving Multicast TreeUploaded byJebakumar Gomer Rajadurai
- 1-D75477GC10_TOCUploaded byRapid Chase
- Report Study on Cooperative Data Dissemination_153903015.pdfUploaded byWahab Kukaswadia
- LSDUploaded bymeisamtey
- 2nd unit - 2mUploaded byPushpa Ramakrishnan Iyer
- NTrees_1Uploaded byIonArapu
- The Pain With Using Singleton(WebDynpro)Uploaded byheart404
- 20 Binary Search TreeUploaded byvhiep195
- phaniUploaded byVamshi Krishna Singirikonda
- cisco-pp2Uploaded bybenaminsan
- BstUploaded bysuvaidzar_551293571
- HashingUploaded bygoyaltarun
- Lecture16-BSTrees.pptUploaded bycatarac19
- Mapping Contoured Terrain_A Comparison of SLAM Algorithms for RAdioControlled HelicoptersUploaded byGeoFurriel
- Assignment 3 KdtreeUploaded bycartamenes
- DECEMBER 2010 UGC NET COMPUTER SCIENCE SOLVEDUploaded byvani_ppc
- SQL DATA STORAGE AND QUERY PROCESSING one mark dumpsUploaded byshyam15287
- Analysis Lab 6.pdfUploaded bysampe9233
- b+ tree insertion & deletionUploaded bySubhash Singh
- Priority Queue Using HeapUploaded byLycelleRamos
- Lesson 7Uploaded byAnonymous iUW0JL6O5
- The 18th Central European Olympiad in Informatics CEOI 2011Uploaded byPaweł Kapiszka
- Apress Beginning SQL Server for Developers 4th (2015)Uploaded byQuastor

- comptia-cybersecurity-analyst-(cs0-001)Uploaded byAditya Mishra
- Cloud Affairs - Time and WorkUploaded byAditya Mishra
- Cloud affairs - Probability.docxUploaded byAditya Mishra
- probabilityP.pdfUploaded byKarn Kumar
- Remainder.docxUploaded byAditya Mishra
- Gen EnglishUploaded byAditya Mishra
- GS2011 QP ComputerUploaded by'Vinay Kumar Machamraj
- Advt Eng Fraud Mgmt Chief Off Security 01122016Uploaded byAditya Mishra
- Linux Important command.txtUploaded byAditya Mishra
- cisb351Uploaded byAditya Mishra
- Exercise Set2 SolutionsUploaded byAditya Mishra
- logic_problemSet.pdfUploaded byAditya Mishra
- Bharat Bill Payments SystemUploaded byAditya Mishra
- Gate New Syllabus Computer Science and Information Technology for 2016Uploaded byersayantan
- Aptitude Shortcuts and Mind Tricks for Boats and Streams Problems Type-I-www.ibpsguide.com (1)Uploaded byVikas Banpurkar
- 4 Regular ExpressionsUploaded byAditya Mishra
- Solution 0Uploaded byAditya Mishra
- Other Geometry pUploaded byAditya Mishra
- 14-1UUploaded byAditya Mishra
- 15-1Uploaded byAditya Mishra
- cs151_sq07_q4_solUploaded byAditya Mishra
- s03nfUploaded byAditya Mishra
- Midterm1 SolutionUploaded byAditya Mishra
- e2 AnswersUploaded byAditya Mishra
- FinalUploaded byAditya Mishra
- Midterm SolutionsUploaded bydragon0962
- Practice MidtermUploaded byAditya Mishra
- CISA PraticeUploaded byAditya Mishra

- Red Black TreesUploaded bytruongvinhlan19895148
- - binary search treeUploaded byapi-237335979
- Estrutura- exercicioUploaded byJuri Lefanov
- Data Structures Lab CseUploaded bysridharegsp
- Binary Tree OverviewUploaded bykusum
- CS 4820-Lecture 6Uploaded bySean Li
- DS3Uploaded bysatya20000
- Lecture 7 - AVL Trees IIUploaded byමනෝජ් තුඩුගල
- Binary Tree Term 1Uploaded byGunjan Bhavsar
- Tree TraversalsUploaded byRajendranbehappy
- DBMS-B+ and B treesUploaded byMageshwari Dharaneeswaran
- AVLUploaded byepanicu2000
- Binary Search Trees.pptUploaded byআরব বেদুঈন
- AVL treeUploaded byajachy
- Lecture 19Uploaded bykabham_09
- cs301Uploaded byRizwanIlyas
- Lecture Notes for TreesUploaded bynani
- 3-solutions-clrs-18Uploaded byalac_john
- Prim's AlgorithmUploaded byCabigting Jeremiah
- AI_Lab-3Uploaded byDil Prasad Kunwar
- 10_HeapUploaded byCristian
- Ch7.TreesUploaded byUsama Lucky
- Cse-III-data Structures With c [10cs35]-Question PaperUploaded byMonim Moni
- lec114Uploaded byMike Ash
- lab5ASDUploaded byTasleem Arif
- Lecture12 UpdatedUploaded byJose guiteerrz
- אלגוריתמים- הרצאה 11 | Red Black TreesUploaded byRon
- ps4solUploaded bydjoseph_1
- Lecture 130 249 TreeUploaded byadityatheaaa
- Binary Search Tree Java2Uploaded bySushmita Ghangas

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.