SplayTree.js
/**
   * Splay the tree base on x as root
   * @param {object} x - The node to splay
   */
Splay the tree base on x as root
@param {object} x - The node to splay
Splay the tree base on x as root @param { object } x - The node to splay
splay(x) {
    let p, gp, ggp, l, r;
    while (x.parent) {
      p = x.parent;
      gp = p.parent;
      if (gp && gp.parent) {
        ggp = gp.parent;
        if (ggp.left === gp) ggp.left = x;
        else ggp.right = x;
        x.parent = ggp;
      } else {
        x.parent = null;
        this._root = x;
      }
      l = x.left;
      r = x.right;
      if (x === p.left) { 
        if (gp) {
          if (gp.left === p) {
            if (p.right) {
              gp.left = p.right;
              gp.left.parent = gp;
            } else gp.left = null;
            p.right = gp;
            gp.parent = p;
          } else {
            if (l) {
              gp.right = l;
              l.parent = gp;
            } else gp.right = null;
            x.left = gp;
            gp.parent = x;
          }
        }
        if (r) {
          p.left = r;
          r.parent = p;
        } else p.left = null;
        x.right = p;
        p.parent = x;
      } else { 
        if (gp) {
          if (gp.right === p) {
            if (p.left) {
              gp.right = p.left;
              gp.right.parent = gp;
            } else gp.right = null;
            p.left = gp;
            gp.parent = p;
          } else {
            if (r) {
              gp.left = r;
              r.parent = gp;
            } else gp.left = null;
            x.right = gp;
            gp.parent = x;
          }
        }
        if (l) {
          p.right = l;
          l.parent = p;
        } else p.right = null;
        x.left = p;
        p.parent = x;
      }
    }
  }
splay ( x ) { let p , gp , ggp , l , r ; while ( x . parent ) { p = x . parent ; gp = p . parent ; if ( gp && gp . parent ) { ggp = gp . parent ; if ( ggp . left === gp ) ggp . left = x ; else ggp . right = x ; x . parent = ggp ; } else { x . parent = null ; this . _root = x ; } l = x . left ; r = x . right ; if ( x === p . left ) { if ( gp ) { if ( gp . left === p ) { if ( p . right ) { gp . left = p . right ; gp . left . parent = gp ; } else gp . left = null ; p . right = gp ; gp . parent = p ; } else { if ( l ) { gp . right = l ; l . parent = gp ; } else gp . right = null ; x . left = gp ; gp . parent = x ; } } if ( r ) { p . left = r ; r . parent = p ; } else p . left = null ; x . right = p ; p . parent = x ; } else { if ( gp ) { if ( gp . right === p ) { if ( p . left ) { gp . right = p . left ; gp . right . parent = gp ; } else gp . right = null ; p . left = gp ; gp . parent = p ; } else { if ( r ) { gp . left = r ; r . parent = gp ; } else gp . left = null ; x . right = gp ; gp . parent = x ; } } if ( l ) { p . right = l ; l . parent = p ; } else p . right = null ; x . left = p ; p . parent = x ; } } }
Splay the tree base on x as root
 @param {object} x - The node to splay
Splay the tree base on x as root @param { object } x - The node to splay
// left /* zig-zig */ /* zig-zag */ // right /* zig-zig */ /* zig-zag */