12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

458 Chap. 13 Advanced Tree <strong>Structures</strong>GSPDAPSCBGABCD(a)(b)Figure 13.9 Splay tree zigzig rotation. (a) The original tree with S, P, <strong>and</strong> Gin zigzig formation. (b) The tree after the rotation takes place. The positions ofsubtrees A, B, C, <strong>and</strong> D are altered as appropriate <strong>to</strong> maintain the BST property.1. S is the left child of P, which is in turn the left child of G.2. S is the right child of P, which is in turn the right child of G.Thus, a zigzig rotation takes place in those situations where a zigzag rotation is notappropriate. The zigzig rotation is illustrated by Figure 13.9. While Figure 13.9appears somewhat different from Figure 13.6, in fact the zigzig rotation is identical<strong>to</strong> the AVL tree double rotation.Note that zigzag rotations tend <strong>to</strong> make the tree more balanced, because theybring subtrees B <strong>and</strong> C up one level while moving subtree D down one level. Theresult is often a reduction of the tree’s height by one. Zigzig promotions do nottypically reduce the height of the tree; they merely bring the newly accessed record<strong>to</strong>ward the root.Splaying node S involves a series of double rotations until S reaches either theroot or the child of the root. Then, if necessary, a single rotation makes S theroot. This process tends <strong>to</strong> re-balance the tree. In any case, it will make frequentlyaccessed nodes stay near the <strong>to</strong>p of the tree, resulting in reduced access cost. Proofthat the splay tree does in fact meet the guarantee of O(m log n) is beyond the scopeof this book. Such a proof can be found in the references in Section 13.4.Example 13.4 Consider a search for value 89 in the splay tree of Figure13.10(a). The splay tree’s search operation is identical <strong>to</strong> searching ina BST. However, once the value has been found, it is splayed <strong>to</strong> the root.Three rotations are required in this example. The first is a zigzig rotation,

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

Saved successfully!

Ooh no, something went wrong!