chap14-solutions - Selected Solutions for Chapter 14:...

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

View Full Document Right Arrow Icon
Selected Solutions for Chapter 14: Augmenting Data Structures Solution to Exercise 14.1-7 Let AŒ1 :: n± be the array of n distinct numbers. One way to count the inversions is to add up, for each element, the number of larger elements that precede it in the array: # of inversions D n X j D 1 j In ².j/ j ; where In ².j/ D f i W i < j and AŒi± > AŒj± g . Note that j In ².j/ j is related to AŒj± ’s rank in the subarray AŒ1 : : j± because the elements in In ².j/ are the reason that AŒj± is not positioned according to its rank. Let r.j/ be the rank of AŒj± in AŒ1 : : j± . Then j D r.j/ C j In ².j/ j , so we can compute j In ².j/ j D j N r.j/ by inserting AŒ1±; :: : ; AŒn± into an order-statistic tree and using OS-RANK to find the rank of each AŒj± in the tree immediately after it is inserted into the tree. (This OS-RANK value is r.j/ .) Insertion and OS-RANK each take O. lg n/ time, and so the total time for n ele- ments is O.n lg n/ . Solution to Exercise 14.2-2 Yes, we can maintain black-heights as attributes in the nodes of a red-black tree without affecting the asymptotic performance of the red-black tree operations. We appeal to Theorem 14.1, because the black-height of a node can be computed from the information at the node and its two children. Actually, the black-height can be computed from just one child’s information: the black-height of a node is the black-height of a red child, or the black height of a black child plus one. The second child does not need to be checked because of property 5 of red-black trees. Within the RB-INSERT-FIXUP and RB-DELETE-FIXUP procedures are color changes, each of which potentially cause O. lg n/ black-height changes. Let us
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
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/22/2010 for the course CS 310 taught by Professor Fussell during the Spring '08 term at University of Texas at Austin.

Page1 / 4

chap14-solutions - Selected Solutions for Chapter 14:...

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