18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Lecture 8 - binary trees<br />

too. */<br />

if (y z)<br />

return y<br />

then<br />

Lines 1-3 determine which node y is physically removed.<br />

Lines 4-6 identify x as the non-nil decendant, if any.<br />

Lines 7-8 give x a new parent.<br />

Lines 9-10 modify the root node, if necessary<br />

Lines 11-13 reattach the subtree, if necessary.<br />

Lines 14-16 if the removed node is deleted, copy.<br />

then<br />

else<br />

Conclusion: deletion takes time proportional to the height of the tree. Listen To Part 8-13<br />

Balanced Search Trees<br />

/* If y has other fields, copy them,<br />

All six of our dictionary operations, when implemented with binary search trees, take O(h), where h is the height of the tree.<br />

<strong>The</strong> best height we could hope to get is , if the tree was perfectly balanced, since<br />

But if we get unlucky with our order of insertion or deletion, we could get linear height!<br />

insert(a)<br />

file:///E|/LEC/LECTUR17/NODE8.HTM (9 of 13) [19/1/2003 1:34:46]

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

Saved successfully!

Ooh no, something went wrong!