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.

Thus, without loss of generality, we assume that we are always <strong>in</strong>terested <strong>in</strong> the<br />

m<strong>in</strong>imum key, which will always be at the root of the heap.<br />

For the sake of efficiency, as will become clear later, we want the heap T to have as<br />

small a height as possible. We enforce this requirement by <strong>in</strong>sist<strong>in</strong>g that the heap T<br />

satisfy an additional structural property: it must be complete. Before we def<strong>in</strong>e this<br />

structural property, we need some def<strong>in</strong>itions. We recall from Section 7.3.3 that<br />

level i of a b<strong>in</strong>ary tree T is the set of nodes of Tthat have depth i. Given nodes v <strong>and</strong><br />

w on the same level of T, we say that v is to the left of w if v is encountered before<br />

w <strong>in</strong> an <strong>in</strong>order traversal of T. That is, there is a node u of T such that v is <strong>in</strong> the left<br />

subtree of u <strong>and</strong> w is <strong>in</strong> the right subtree of u. For example, <strong>in</strong> the b<strong>in</strong>ary tree of<br />

Figure 8.3, the node stor<strong>in</strong>g entry (15,K) is to the left of the node stor<strong>in</strong>g entry (7,<br />

Q). In a st<strong>and</strong>ard draw<strong>in</strong>g of a b<strong>in</strong>ary tree, the "to the left of" relation is visualized<br />

by the relative horizontal placement of the nodes.<br />

Complete B<strong>in</strong>ary Tree Property: A heap T with height h is a complete b<strong>in</strong>ary tree if<br />

levels 0,1,2,… ,h − 1 of T have the maximum number of nodes possible (namely,<br />

level i has 2 i nodes, for 0 ≤ i ≤ h − 1) <strong>and</strong> <strong>in</strong> level h − 1, all the <strong>in</strong>ternal nodes are to<br />

the left of the external nodes <strong>and</strong> there is at most one node with one child, which<br />

must be a left child.<br />

By <strong>in</strong>sist<strong>in</strong>g that a heap T be complete, we identify another important node <strong>in</strong> a<br />

heap T, other than the root, namely, the last node of T, which we def<strong>in</strong>e to be the<br />

right-most, deepest external node of T (see Figure 8.3).<br />

The Height of a Heap<br />

Let h denote the height of T. Another way of def<strong>in</strong><strong>in</strong>g the last node of T is that it<br />

is the node on level h such that all the other nodes of level h are to the left of it.<br />

Insist<strong>in</strong>g that T be complete also has an important consequence, as shown <strong>in</strong><br />

Proposition 8.5.<br />

470

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

Saved successfully!

Ooh no, something went wrong!