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.

The Complete B<strong>in</strong>ary Tree ADT<br />

As an abstract data type, a complete b<strong>in</strong>ary T supports all the methods of b<strong>in</strong>ary<br />

tree ADT (Section 7.3.1), plus the follow<strong>in</strong>g two methods:<br />

add(o): Add to T <strong>and</strong> return a new external node v stor<strong>in</strong>g element o<br />

such that the result<strong>in</strong>g tree is a complete b<strong>in</strong>ary tree with last node v.<br />

remove(): Remove the last node of T <strong>and</strong> return its element.<br />

Us<strong>in</strong>g only these update operations guarantees that we will always have a<br />

complete b<strong>in</strong>ary tree. As shown <strong>in</strong> Figure 8.4, there are two cases for the effect of<br />

an add or remove. Specifically, for an add, we have the follow<strong>in</strong>g (remove is<br />

similar).<br />

• If the bottom level of T is not full, then add <strong>in</strong>serts a new node on the<br />

bottom level of T, immediately after the right-most node of this level (that is,<br />

the last node); hence, T's height rema<strong>in</strong>s the same.<br />

• If the bottom level is full, then add <strong>in</strong>serts a new node as the left child of<br />

the left-most node of the bottom level of T; hence, T's height <strong>in</strong>creases by one.<br />

Figure 8.4: Examples of operations add <strong>and</strong> remove<br />

on a complete b<strong>in</strong>ary tree, where w denotes the node<br />

<strong>in</strong>serted by add or deleted by remove. The trees<br />

shown <strong>in</strong> (b) <strong>and</strong> (d) are the results of perform<strong>in</strong>g add<br />

operations on the trees <strong>in</strong> (a) <strong>and</strong> (c), respectively.<br />

Likewise, the trees shown <strong>in</strong> (a) <strong>and</strong> (c) are the results<br />

of perform<strong>in</strong>g remove operations on the trees <strong>in</strong> (b)<br />

<strong>and</strong> (d), respectively.<br />

472

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

Saved successfully!

Ooh no, something went wrong!