



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




From Stout and Warren — translated from Pascal to C to Java by Tim Rolfe. Stout and Warren explicitly encode the rightward rotations, while in the implementation that follows the rotations are isolated in their own callable functions. // Tree to Vine algorithm: a "pseudo-root" is passed --- // comparable with a dummy header for a linked list. void tree_to_vine ( BSTnode root ) { BSTnode vineTail, remainder, tempPtr; vineTail = root; remainder = vineTail.Right; while ( remainder != NULL ) {//If no leftward subtree, move rightward if ( remainder.Left == NULL ) { vineTail = remainder; remainder = remainder.Right; } // else eliminate the leftward subtree by rotations else /* Rightward rotation */ { tempPtr = remainder.Left; remainder.Left = tempPtr.Right; tempPtr.Right = remainder; remainder = tempPtr; vineTail.Right = tempPtr; } } } rotate remainder vineTail^ vineTail T T1 T2 T2 T T Figure 2. A Tree_to_Vine Rotation
DSW Algorithm Implementation Methods public class DSW extends BST { public void rebalance() { BSTnode pseudo_root = new BSTnode (-1, null, root); vinify (pseudo_root); // Post-condition: degenerate tree balance(pseudo_root); // Post-condition: balanced tree root = pseudo_root.right; // Update BST root. correctTree (root, null); // Update heights and parent links. } // Turn an arbitrary binary search tree into a vine (degenerate tree) // Note: pseudo-root guaranteed non-null private void vinify (BSTnode pseudo_root) { BSTnode Gr = pseudo_root,// Grandparent p = Gr.right; // parent while (p != null) { if (p.left != null) // Eliminate all leftward links { Gr.right = rotateRight(Gr.right); p = Gr.right; } else { Gr = p; // else move rightward and do it again p = Gr.right; } } } // Tim Rolfe: utility routine for balance(pseudo_root) below private void rot_Set(BSTnode pseudo_root, int n) { BSTnode p; if (n == 0) return; // Zero rotations to be done p = pseudo_root; // Root is at pseudo_root.right for ( ; n > 0; n--) { p.right = rotateLeft(p.right); // Note: after the rotate, just p = p.right; // move one position --- the rotate // itself moves the other position if (p == null && n != 1) { System.err.println ("About to jump through null --- ABORT!!!!"); System.exit(1); } } } // Given a degenerate binary search tree (vine), generate the // most balanced possible tree. private void balance (BSTnode pseudo_root) { BSTnode p = pseudo_root.right; int n, // Number of nodes in the degenerate rightward tree m; // Nodes in the completely full part of the final tree // Count the nodes in the vine. for (n = 0; p != null; p = p.right) n++; for (m = 1; m <= n+1; ) // Overshoot, then back off. m += m; m = m/2 - 1; // pow(2.0, floor(log(n+1)/log(2))) - 1 rot_Set (pseudo_root, n-m); // Perform a set of n-m left rotations // Takes care of the non-full level while ( m > 1 ) { m /= 2; rot_Set (pseudo_root, m);// Perform a set of m left rotations } } // After the dust has settled, get valid height, and size values // back into the tree. private void correctTree( BSTnode node, BSTnode parent ) { if ( node != null ) { int ltVal, rtVal; // First, correct below this node. correctTree (node.left, node); correctTree (node.right, node); // NOW we can properly correct this node. ltVal = node.left != null? node.left.height : 0; rtVal = node.right != null? node.right.height : 0; node.height = 1 + Math.max( ltVal, rtVal ); ltVal = node.left != null? node.left.size : 0; rtVal = node.right != null? node.right.size : 0; node.size = 1 + ltVal + rtVal; } } // Rotation functions BSTnode rotateLeft (BSTnode lt) // Rotate leftward --- left node moves down, right node moves up. { BSTnode rt = lt.right, q = rt.left; rt.left = lt; lt.right= q; return rt; // Return the link to the new subtree root } BSTnode rotateRight(BSTnode rt) // Rotate rightward --- right node moves down, left node moves up. { BSTnode lt = rt.left, q = lt.right; lt.right= rt; rt.left = q; return lt; // Return the link to the new subtree root }
} // end class DSW
15: R--Tim 16: L--Steven 14: R--Steve 13: R--Roger 12: R--Rob 11: R--Paul 10: R--Mike 9: R--Mark 8: R--John 7: R--Jim 6: R--Greg 5: R--David 4: R--Dave 3: R--Brian 2: R--Bill 1: R--Barney 0: #--Alan 16: R--Tim 15: R--Steven 14: R--Steve 13: R--Roger 12: R--Rob 11: R--Paul 10: R--Mike 9: R--Mark 8: R--John 7: R--Jim 6: R--Greg 5: R--David 4: R--Dave 3: R--Brian 2: R--Bill 1: R--Barney 0: #--Alan Begin Tree Balancing
14: R--Tim 13: R--Steven 12: R--Steve 11: R--Roger 10: R--Rob 9: R--Paul 8: R--Mike 7: R--Mark 6: R--John 5: R--Jim 4: R--Greg 3: R--David 2: R--Dave 1: R--Brian 2: L--Bill 0: #--Barney 1: L--Alan
7: R--Tim 6: R--Steven 7: L--Steve 5: R--Roger 6: L--Rob 4: R--Paul 5: L--Mike 3: R--Mark 4: L--John 2: R--Jim 3: L--Greg 1: R--David 2: L--Dave 0: #--Brian 2: R--Bill 1: L--Barney 2: L--Alan
4: R--Tim 3: R--Steven 4: L--Steve 2: R--Roger 4: R--Rob 3: L--Paul 4: L--Mike 1: R--Mark 3: R--John 2: L--Jim 3: L--Greg 0: #--David 2: R--Dave 1: L--Brian 3: R--Bill 2: L--Barney 3: L--Alan
3: R--Tim 2: R--Steven 3: L--Steve 1: R--Roger 3: R--Rob 2: L--Paul 3: L--Mike 0: #--Mark 3: R--John 2: R--Jim 3: L--Greg 1: L--David 3: R--Dave 2: L--Brian 4: R--Bill 3: L--Barney 4: L--Alan