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.

O(n)<br />

replace<br />

O(1)<br />

root, parent, children, left, right, sibl<strong>in</strong>g<br />

O(1)<br />

hasLeft, hasRight, isInternal, isExternal,<br />

isRoot<br />

O(1)<br />

<strong>in</strong>sertLeft, <strong>in</strong>sertRight, attach, remove<br />

O(1)<br />

7.3.5 An Array-List Representation of a<br />

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

An alternative representation of a b<strong>in</strong>ary tree T is based on a way of number<strong>in</strong>g the<br />

nodes of T. For every node v of T, let p(v) be the <strong>in</strong>teger def<strong>in</strong>ed as follows.<br />

• If v is the root of T, then p(v) = 1.<br />

• If v is the left child of node u, then p(v) = 2p(u).<br />

• If v is the right child of node u, then p(v) = 2p(u) + 1.<br />

The number<strong>in</strong>g function p is known as a level number<strong>in</strong>g of the nodes <strong>in</strong> a b<strong>in</strong>ary<br />

tree T, for it numbers the nodes on each level of T <strong>in</strong> <strong>in</strong>creas<strong>in</strong>g order from left to<br />

right, although it may skip some numbers. (See Figure 7.15.)<br />

Figure 7.15: B<strong>in</strong>ary tree level number<strong>in</strong>g: (a) general<br />

scheme; (b) an example.<br />

414

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

Saved successfully!

Ooh no, something went wrong!