|
Research Paper: click here
|
Property:
|
Property:
|
Property:
|
|
|
/* ======================================================= Splay operation ======================================================= */ public void splay( BSTEntry x ) { while ( x != root ) { if ( x.parent == root ) { // height(x) = 1, can only move up 1 level... zig1(x); // Perform a zig operation } else { zig2( x ); // Perform a zig-zig or a zig-zag operation } } } |
Pseudo code:
Determine the configuration: 1. y 2. y / \ / \ T1 x x T4 / \ / \ T2 W W T3 setup the pointers x, y, T1, T2, T3 and T4 Depending on the configuration, construct the resulting tree Make x the root node (you only use zig in this case !) |
public void zig1( BSTEntry x ) { BSTEntry y = x.parent; // Find the parent node /* ******************************************************************* Determine the parent child relationships between (x,y)) ******************************************************************* */ boolean xIsLeftChild = (x == y.left); BSTEntry T1, T2, T3, T4, W; /* ======================================================= Determine the configuration and reconfigure ======================================================= */ if (xIsLeftChild) { /* Configuration zig 2 */ System.out.println("Use zig(2)"); // Prepare to link // y x // / \ / \ W = x.left; // x T4 W y T3 = x.right; // / \ / \ T4 = y.right; // W T3 T3 T4 // Link them up x.left = W; if ( W != null ) W.parent = x; x.right = y; y.parent = x; y.left = T3; if ( T3 != null ) T3.parent = y; y.right = T4; if ( T4 != null ) T4.parent = y; } else { /* Configuration zig(1) */ System.out.println("Use zig(1)"); // Prepare to link // y x T1 = y.left; // / \ / \ T2 = x.left; // T1 x y W // / \ / \ W = x.right; // T2 W T1 T2 // Link them up x.left = y; y.parent = x; x.right = W; if ( W != null ) W.parent = x; y.left = T1; if ( T1 != null ) T1.parent = y; y.right = T2; if ( T2 != null ) T2.parent = y; } root = x; // We only use zig1() when y is root !!! x.parent = null; } |
public void zig2( BSTEntry x ) { BSTEntry y, z, zParent; BSTEntry T1, T2, T3, T4; y = x.parent; z = y.parent; zParent = z.parent; // Save the parent of z (link later) /* ******************************************************************* Determine the parent child relationships between (y,z) and (x,y)) z --> y --> x ******************************************************************* */ boolean yIsLeftChild = (y == z.left); boolean xIsLeftChild = (x == y.left); /* =========================================================== Determine the configuration and restructure accordingly =========================================================== */ if (xIsLeftChild && yIsLeftChild) { /* Configuration zig-zig 2 */ System.out.println("Use zig-zig(2)"); // z x // / \ / \ // y T4 T1 y T1 = x.left; // / \ / \ T2 = x.right; // x T3 T2 z T3 = y.right; // / \ / \ T4 = z.right; // T1 T2 T3 T4 x.left = T1; if ( T1 != null ) T1.parent = x; x.right = y; y.parent = x; y.left = T2; if ( T2 != null ) T2.parent = y; y.right = z; z.parent = y; z.left = T3; if ( T3 != null ) T3.parent = z; z.right = T4; if ( T4 != null ) T4.parent = z; } else if (!xIsLeftChild && yIsLeftChild) { /* Configuration zig-zag(2) */ System.out.println("Use zig-zag(2)"); // z x // / \ / \ // y T4 y z T1 = y.left; // / \ / \ / \ T2 = x.left; // T1 x T1 T2 T3 T4 T3 = x.right; // / \ T4 = z.right; // T2 T3 x.left = y; y.parent = x; x.right = z; z.parent = x; y.left = T1; if ( T1 != null ) T1.parent = y; y.right = T2; if ( T2 != null ) T2.parent = y; z.left = T3; if ( T3 != null ) T3.parent = z; z.right = T4; if ( T3 != null ) T4.parent = z; } else if (xIsLeftChild && !yIsLeftChild) { /* Configuration zig-zag(1) */ System.out.println("Use zig-zag(1)"); // z x // / \ / \ // T1 y z y T1 = z.left; // / \ / \ / \ T2 = x.left; // x T4 T1 T2 T3 T4 T3 = x.right; // / \ T4 = y.right; // T2 T3 x.left = z; z.parent = x; x.right = y; y.parent = x; y.left = T3; if ( T3 != null ) T3.parent = y; y.right = T4; if ( T4 != null ) T4.parent = y; z.left = T1; if ( T1 != null ) T1.parent = z; z.right = T2; if ( T2 != null ) T2.parent = z; } else { /* Configuration zig-zig(1) */ System.out.println("Use zig-zig(1)"); // z x // / \ / \ // T1 y y T4 T1 = z.left; // / \ / \ T2 = y.left; // T2 x z T3 T3 = x.left; // / \ / \ T4 = x.right; // T3 T4 T1 T2 x.left = y; y.parent = x; x.right = T4; if ( T4 != null ) T4.parent = x; y.left = z; z.parent = y; y.right = T3; if ( T3 != null ) T3.parent = y; z.left = T1; if ( T1 != null ) T1.parent = z; z.right = T2; if ( T2 != null ) T2.parent = z; } /* ============================================================= Link the resulting subtree to z's Parent (zParent) (Remember that prior to the restructuring, the tree is: z z \ / y y and so on... z was the root \ \ x x ============================================================== */ if ( root == z ) { /* If z is the root node, handle the replacement differently.... */ root = x; // x is now the new root x.parent = null; } else { // Link x to zParent (at left or at right, depending where z was) if ( zParent.left == z ) { /* Link x to the left branch of z's parent */ x.parent = zParent; zParent.left = x; } else { /* Link x to the right branch of z's parent */ x.parent = zParent; zParent.right = x; } } } |
How to run the program:
|
|
|
|
One of the reason why I chose to study the splay tree is to introduce students to an interesting (novel) way to study performance evaluation:
|
|