11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

438 Chap. 13 Advanced Tree <strong>Structures</strong>PSSCAPA B B C(a)(b)Figure 13.7 Splay tree single rot<strong>at</strong>ion. This rot<strong>at</strong>ion takes place only whenthe node being splayed is a child of the root. Here, node S is promoted to theroot, rot<strong>at</strong>ing with node P. Because the value of S is less than the value of P,P must become S’s right child. The positions of subtrees A, B, <strong>and</strong> C are alteredas appropri<strong>at</strong>e to maintain the BST property, but the contents of these subtreesremains unchanged. (a) The original tree with P as the parent. (b) The tree aftera rot<strong>at</strong>ion takes place. Performing a single rot<strong>at</strong>ion a second time will return thetree to its original shape. Equivalently, if (b) is the initial configur<strong>at</strong>ion of the tree(i.e., S is <strong>at</strong> the root <strong>and</strong> P is its right child), then (a) shows the result of a singlerot<strong>at</strong>ion to splay P to the root.way th<strong>at</strong> retains the BST property. While Figure 13.7 is slightly different fromFigure 13.5, in fact the splay tree single rot<strong>at</strong>ion is identical to the AVL tree singlerot<strong>at</strong>ion.Unlike the AVL tree, the splay tree requires two types of double rot<strong>at</strong>ion. Doublerot<strong>at</strong>ions involve S, its parent (call it P), <strong>and</strong> S’s gr<strong>and</strong>parent (call it G). Theeffect of a double rot<strong>at</strong>ion is to move S up two levels in the tree.The first double rot<strong>at</strong>ion is called a zigzag rot<strong>at</strong>ion. It takes place when eitherof the following two conditions are met:1. S is the left child of P, <strong>and</strong> P is the right child of G.2. S is the right child of P, <strong>and</strong> P is the left child of G.In other words, a zigzag rot<strong>at</strong>ion is used when G, P, <strong>and</strong> S form a zigzag. Thezigzag rot<strong>at</strong>ion is illustr<strong>at</strong>ed by Figure 13.8.The other double rot<strong>at</strong>ion is known as a zigzig rot<strong>at</strong>ion. A zigzig rot<strong>at</strong>ion takesplace when either of the following two conditions are met: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 rot<strong>at</strong>ion takes place in those situ<strong>at</strong>ions where a zigzag rot<strong>at</strong>ion is notappropri<strong>at</strong>e. The zigzig rot<strong>at</strong>ion is illustr<strong>at</strong>ed by Figure 13.9. While Figure 13.9appears somewh<strong>at</strong> different from Figure 13.6, in fact the zigzig rot<strong>at</strong>ion is identicalto the AVL tree double rot<strong>at</strong>ion.

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

Saved successfully!

Ooh no, something went wrong!