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.

1. 2h+1 ≤ n≤2 h+1 −1<br />

2. h+1≤n E ≤2 h<br />

3. h≤n I ≤2 h −1<br />

4. log(n + 1) − 1 ≤ h ≤ (n − 1)/2.<br />

Relat<strong>in</strong>g Internal Nodes to External Nodes <strong>in</strong> a Proper<br />

B<strong>in</strong>ary Tree<br />

In addition to the b<strong>in</strong>ary tree properties above, we also have the follow<strong>in</strong>g<br />

relationship between the number of <strong>in</strong>ternal nodes <strong>and</strong> external nodes <strong>in</strong> a proper<br />

b<strong>in</strong>ary tree.<br />

Proposition 7.11: In a nonempty proper b<strong>in</strong>ary tree T, with n E external<br />

nodes <strong>and</strong> n I <strong>in</strong>ternal nodes, we have n e = n I + 1.<br />

Justification: We justify this proposition by remov<strong>in</strong>g nodes from T <strong>and</strong><br />

divid<strong>in</strong>g them up <strong>in</strong>to two "piles", an <strong>in</strong>ternal-node pile <strong>and</strong> an external-node pile,<br />

until T becomes empty. The piles are <strong>in</strong>itially empty. At the end, the externalnode<br />

pile will have one more node than the <strong>in</strong>ternal-node pile. We consider two<br />

cases:<br />

Case 1: If T has only one node v, we remove v <strong>and</strong> place it on the external-node<br />

pile. Thus, the external-node pile has one node <strong>and</strong> the <strong>in</strong>ternal-node pile is<br />

empty.<br />

Case 2: Otherwise (T has more than one node), we remove from T an (arbitrary)<br />

external node w <strong>and</strong> its parent v, which is an <strong>in</strong>ternal node. We place w on the<br />

external-node pile <strong>and</strong> v on the <strong>in</strong>ternal-node pile. If v has a parent u, then we<br />

reconnect u with the former sibl<strong>in</strong>g z of w, as shown <strong>in</strong> Figure 7.13. This<br />

operation, removes one <strong>in</strong>ternal node <strong>and</strong> one external node, <strong>and</strong> leaves the tree<br />

be<strong>in</strong>g a proper b<strong>in</strong>ary tree.<br />

Repeat<strong>in</strong>g this operation, we eventually are left with a f<strong>in</strong>al tree consist<strong>in</strong>g of a<br />

s<strong>in</strong>gle node. Note that the same number of external <strong>and</strong> <strong>in</strong>ternal nodes have been<br />

removed <strong>and</strong> placed on their respective piles by the sequence of operations<br />

lead<strong>in</strong>g to this f<strong>in</strong>al tree. Now, we remove the node of the f<strong>in</strong>al tree <strong>and</strong> we place<br />

it on the external-node pile. Thus, the the external-node pile has one more node<br />

than the <strong>in</strong>ternal-node pile.<br />

400

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

Saved successfully!

Ooh no, something went wrong!