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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

An immediate consequence of the height-balance property is that a subtree of an<br />

AVL tree is itself an AVL tree. The height-balance property has also the important<br />

consequence of keep<strong>in</strong>g the height small, as shown <strong>in</strong> the follow<strong>in</strong>g proposition.<br />

Proposition 10.2: The height of an AVL tree stor<strong>in</strong>g n entries is O(logn).<br />

Justification: Instead of try<strong>in</strong>g to f<strong>in</strong>d an upper bound on the height of an<br />

AVL tree directly, it turns out to be easier to work on the "<strong>in</strong>verse problem" of<br />

f<strong>in</strong>d<strong>in</strong>g a lower bound on the m<strong>in</strong>imum number of <strong>in</strong>ternal nodes n(h) of an AVL<br />

tree with height h. We will show that n(h) grows at least exponentially. From this, it<br />

will be an easy step to derive that the height of an AVL tree stor<strong>in</strong>g n entries is<br />

O(logn).<br />

To start with, notice that n(1) = 1 <strong>and</strong> n(2) = 2, because an AVL tree of height 1<br />

must have at least one <strong>in</strong>ternal node <strong>and</strong> an AVL tree of height 2 must have at least<br />

two <strong>in</strong>ternal nodes. Now, for h ≥ 3, an AVL tree with height h <strong>and</strong> the m<strong>in</strong>imum<br />

number of nodes is such that both its subtrees are AVL trees with the m<strong>in</strong>imum<br />

number of nodes: one with height h − 1 <strong>and</strong> the other with height h − 2. Tak<strong>in</strong>g the<br />

root <strong>in</strong>to account, we obta<strong>in</strong> the follow<strong>in</strong>g formula that relates n(h) to n(h − 1) <strong>and</strong><br />

n(h − 2), for h ≥ 3:<br />

n(h) = 1 + n(h−1) + n(h−2).<br />

(10.1)<br />

At this po<strong>in</strong>t, the reader familiar with the properties of Fibonacci progressions<br />

(Section 2.2.3 <strong>and</strong> Exercise C-4.12) will already see that n(h) is a function<br />

exponential <strong>in</strong> h. For the rest of the readers, we will proceed with our reason<strong>in</strong>g.<br />

Formula 10.1 implies that n(h) is a strictly <strong>in</strong>creas<strong>in</strong>g function of h. Thus, we know<br />

that n(h − 1) > n(h − 2). Replac<strong>in</strong>g n(h − 1) with n(h − 2) <strong>in</strong> Formula 10.1 <strong>and</strong><br />

dropp<strong>in</strong>g the 1, we get, for h ≥ 3,<br />

n(h) > 2·n(h − 2).<br />

(10.2)<br />

Formula 10.2 <strong>in</strong>dicates that n(h) at least doubles each time h <strong>in</strong>creases by 2, which<br />

<strong>in</strong>tuitively means that n(h) grows exponentially. To show this fact <strong>in</strong> a formal way,<br />

we apply Formula 10.2 repeatedly, yield<strong>in</strong>g the follow<strong>in</strong>g series of <strong>in</strong>equalities:<br />

n(h) > 2·n(h − 2)<br />

<br />

> 4·n(h − 4)<br />

> 8·n(h − 6)<br />

600

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

Saved successfully!

Ooh no, something went wrong!