Docsity

DSW Tree Balancing Algorithm: Implementation and Trace - Prof. Timothy Rolfe, Study notes of Algorithms and Programming

Toggle sidebar
bg1
DSW Tree Balancing Algorithm
by Timothy Rolfe
This is an algorithm to balance a binary search tree without requiring any additional space beyond the left- and
right-links that are inherent in a binary tree. It was devised by Colin Day, and later improved by Quentin F. Stout
and Bette L. Warren (hence the acronym “DSW”). These notes are based on the discussion of the algorithm in
Adam Drozdek’s Data Structures and Algorithms in C++ (PWS Publishing, 1996), pp. 173 ff.
Step one: generate a degenerate BST with all left links NULL. Drozdek refers to this as creating the
Backbone, and it is based on performing rightward rotations on every node with a non-null left link.
Step two: given n nodes in the tree, make approximately log2(n) rightward-only traversals of the tree,
performing leftward rotations. This generates a complete binary tree — that is, except possibly at
the bottom level, all tree levels are full, and if the bottom level is not full, all nodes are grouped on
the left.
The two rotations spoken of above are (as one might expect) inverses of each other. As such, it is more useful to
show the grandparent (Gr) whose link must be altered, and then instead of parent and child, leftward node (Lt) and
rightward node(Rt). For completeness, the leftward child of Lt is shown, even though it is not moved, as well as
the rightward child of Rt. The child that is moved is the subtree occurring between Lt and Rt: in one
circumstance it is the rightward child of Lt, in the other it is the leftward child of Rt.
Gr
Rt
Lt
P Q
RP
Q R
Gr
Lt
Rt
Right rotate
Left rotate
In the spirit of Drozdek’s Delete logic, the rotation functions, implementing the above -sketched algorithms, have
been written so that they return a reference to the modified subtree as they do their updates.
In the implementation of the DSW algorithm, I have taken from Warren and Stout the technique of placing the
entire BST to be balanced as the right subtree of a pseudo-root. This means that none of the rotations are in fact
done at the root node, greatly simplifing the programming.
NOTE: All Binary Search Tree balancing algorithms are based on rotations exactly as seen in the DSW
algorithm. Some perform compound rotations; that is, they use transformations that may be viewed as two or
more rotations within a valid Binary Search Tree that still retain the BST property for the generated tree.
A more extensive discussion of the DSW algorithm is available at http://penguin.ewu.edu/~trolfe/DSWpaper/ .
Printed on 2020-12-05 at 21:36 Page 1
pf3
pf4
pf5

Partial preview of the text

Download DSW Tree Balancing Algorithm: Implementation and Trace - Prof. Timothy Rolfe and more Study notes Algorithms and Programming in PDF only on Docsity!

DSW Tree Balancing Algorithm

by Timothy Rolfe

This is an algorithm to balance a binary search tree without requiring any additional space beyond the left- and

right-links that are inherent in a binary tree. It was devised by Colin Day, and later improved by Quentin F. Stout

and Bette L. Warren (hence the acronym “DSW”). These notes are based on the discussion of the algorithm in

Adam Drozdek’s Data Structures and Algorithms in C++ (PWS Publishing, 1996), pp. 173 ff.

Step one: generate a degenerate BST with all left links NULL. Drozdek refers to this as creating the

Backbone, and it is based on performing rightward rotations on every node with a non-null left link.

Step two: given n nodes in the tree, make approximately log 2 ( n ) rightward-only traversals of the tree,

performing leftward rotations. This generates a complete binary tree — that is, except possibly at

the bottom level, all tree levels are full, and if the bottom level is not full, all nodes are grouped on

the left.

The two rotations spoken of above are (as one might expect) inverses of each other. As such, it is more useful to

show the grandparent (Gr) whose link must be altered, and then instead of parent and child, leftward node (Lt) and

rightward node(Rt). For completeness, the leftward child of Lt is shown, even though it is not moved, as well as

the rightward child of Rt. The child that is moved is the subtree occurring between Lt and Rt: in one

circumstance it is the rightward child of Lt, in the other it is the leftward child of Rt.

Gr

Rt

Lt

P Q

R

P

Q R

Gr

Lt

Rt

Right rotate

Left rotate

In the spirit of Drozdek’s Delete logic, the rotation functions, implementing the above-sketched algorithms, have

been written so that they return a reference to the modified subtree as they do their updates.

In the implementation of the DSW algorithm, I have taken from Warren and Stout the technique of placing the

entire BST to be balanced as the right subtree of a pseudo-root. This means that none of the rotations are in fact

done at the root node, greatly simplifing the programming.

NOTE: All Binary Search Tree balancing algorithms are based on rotations exactly as seen in the DSW

algorithm. Some perform compound rotations; that is, they use transformations that may be viewed as two or

more rotations within a valid Binary Search Tree that still retain the BST property for the generated tree.

A more extensive discussion of the DSW algorithm is available at http://penguin.ewu.edu/~trolfe/DSWpaper/.

Printed on 2020-12-05 at 21:36 Page 1

DSW Tree Balancing Algorithm Page 2

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 Tree Balancing Algorithm Page 4

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 }

DSW Tree Balancing Algorithm Page 5

} // end class DSW

Tree-Balancing: Trace of the DSW Algorithm on a 17-node Tree Page 7

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

Two Rotations

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

Seven Rotation

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

Three Rotations

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

One Rotation

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