23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C-8.13<br />

We can represent a path from the root to a given node of a b<strong>in</strong>ary tree by means<br />

of a b<strong>in</strong>ary str<strong>in</strong>g, where 0 means "go to the left child" <strong>and</strong> 1 means "go to the<br />

right child." For example, the path from the root to the node stor<strong>in</strong>g (8,W) <strong>in</strong> the<br />

heap of Figure 8.12a is represented by "101." Design an O(logn)-time algorithm<br />

for f<strong>in</strong>d<strong>in</strong>g the last node of a complete b<strong>in</strong>ary tree with n nodes, based on the<br />

above representation. Show how this algorithm can be used <strong>in</strong> the<br />

implementation of a complete b<strong>in</strong>ary tree by means of a l<strong>in</strong>ked structure that<br />

does not keep a reference to the last node.<br />

C-8.14<br />

Given a heap T <strong>and</strong> a key k, give an algorithm to compute all the entries <strong>in</strong> T<br />

with key less than or equal to k. For example, given the heap of Figure 8.12a<br />

<strong>and</strong> query k= 7, the algorithm should report the entries with keys 2, 4, 5, 6, <strong>and</strong><br />

7 (but not necessarily <strong>in</strong> this order). Your algorithm should run <strong>in</strong> time<br />

proportional to the number of entries returned.<br />

C-8.15<br />

Provide a justification of the time bounds <strong>in</strong> Table 8.4.<br />

C-8.16<br />

Tamar<strong>in</strong>do Airl<strong>in</strong>es wants to give a first-class upgrade coupon to their top log n<br />

frequent flyers, based on the number of miles accumulated, where n is the total<br />

number of the airl<strong>in</strong>es' frequent flyers. The algorithm they currently use, which<br />

runs <strong>in</strong> O(nlogn) time, sorts the flyers by the number of miles flown <strong>and</strong> then<br />

scans the sorted list to pick the top logn flyers. Describe an algorithm that<br />

identifies the top logn flyers <strong>in</strong> O(n) time.<br />

C-8.17<br />

Develop an algorithm that computes the kth smallest element of a set of n<br />

dist<strong>in</strong>ct <strong>in</strong>tegers <strong>in</strong> O(n + klogn) time.<br />

509

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!