/* automatically generated by JSCoverage - do not edit */
if (typeof _$jscoverage === 'undefined') _$jscoverage = {};
if (! _$jscoverage['collections/RedBlackTree.js']) {
  _$jscoverage['collections/RedBlackTree.js'] = [];
  _$jscoverage['collections/RedBlackTree.js'][1] = 0;
  _$jscoverage['collections/RedBlackTree.js'][6] = 0;
  _$jscoverage['collections/RedBlackTree.js'][8] = 0;
  _$jscoverage['collections/RedBlackTree.js'][9] = 0;
  _$jscoverage['collections/RedBlackTree.js'][12] = 0;
  _$jscoverage['collections/RedBlackTree.js'][13] = 0;
  _$jscoverage['collections/RedBlackTree.js'][21] = 0;
  _$jscoverage['collections/RedBlackTree.js'][22] = 0;
  _$jscoverage['collections/RedBlackTree.js'][23] = 0;
  _$jscoverage['collections/RedBlackTree.js'][26] = 0;
  _$jscoverage['collections/RedBlackTree.js'][27] = 0;
  _$jscoverage['collections/RedBlackTree.js'][28] = 0;
  _$jscoverage['collections/RedBlackTree.js'][29] = 0;
  _$jscoverage['collections/RedBlackTree.js'][30] = 0;
  _$jscoverage['collections/RedBlackTree.js'][31] = 0;
  _$jscoverage['collections/RedBlackTree.js'][33] = 0;
  _$jscoverage['collections/RedBlackTree.js'][35] = 0;
  _$jscoverage['collections/RedBlackTree.js'][36] = 0;
  _$jscoverage['collections/RedBlackTree.js'][38] = 0;
  _$jscoverage['collections/RedBlackTree.js'][39] = 0;
  _$jscoverage['collections/RedBlackTree.js'][40] = 0;
  _$jscoverage['collections/RedBlackTree.js'][43] = 0;
  _$jscoverage['collections/RedBlackTree.js'][45] = 0;
  _$jscoverage['collections/RedBlackTree.js'][46] = 0;
  _$jscoverage['collections/RedBlackTree.js'][48] = 0;
  _$jscoverage['collections/RedBlackTree.js'][55] = 0;
  _$jscoverage['collections/RedBlackTree.js'][58] = 0;
  _$jscoverage['collections/RedBlackTree.js'][59] = 0;
  _$jscoverage['collections/RedBlackTree.js'][60] = 0;
  _$jscoverage['collections/RedBlackTree.js'][61] = 0;
  _$jscoverage['collections/RedBlackTree.js'][62] = 0;
  _$jscoverage['collections/RedBlackTree.js'][63] = 0;
  _$jscoverage['collections/RedBlackTree.js'][64] = 0;
  _$jscoverage['collections/RedBlackTree.js'][65] = 0;
  _$jscoverage['collections/RedBlackTree.js'][68] = 0;
  _$jscoverage['collections/RedBlackTree.js'][69] = 0;
  _$jscoverage['collections/RedBlackTree.js'][70] = 0;
  _$jscoverage['collections/RedBlackTree.js'][71] = 0;
  _$jscoverage['collections/RedBlackTree.js'][75] = 0;
  _$jscoverage['collections/RedBlackTree.js'][76] = 0;
  _$jscoverage['collections/RedBlackTree.js'][77] = 0;
  _$jscoverage['collections/RedBlackTree.js'][79] = 0;
  _$jscoverage['collections/RedBlackTree.js'][80] = 0;
  _$jscoverage['collections/RedBlackTree.js'][81] = 0;
  _$jscoverage['collections/RedBlackTree.js'][82] = 0;
  _$jscoverage['collections/RedBlackTree.js'][84] = 0;
  _$jscoverage['collections/RedBlackTree.js'][85] = 0;
  _$jscoverage['collections/RedBlackTree.js'][86] = 0;
  _$jscoverage['collections/RedBlackTree.js'][87] = 0;
  _$jscoverage['collections/RedBlackTree.js'][88] = 0;
  _$jscoverage['collections/RedBlackTree.js'][90] = 0;
  _$jscoverage['collections/RedBlackTree.js'][93] = 0;
  _$jscoverage['collections/RedBlackTree.js'][94] = 0;
  _$jscoverage['collections/RedBlackTree.js'][95] = 0;
  _$jscoverage['collections/RedBlackTree.js'][96] = 0;
  _$jscoverage['collections/RedBlackTree.js'][98] = 0;
  _$jscoverage['collections/RedBlackTree.js'][99] = 0;
  _$jscoverage['collections/RedBlackTree.js'][100] = 0;
  _$jscoverage['collections/RedBlackTree.js'][103] = 0;
  _$jscoverage['collections/RedBlackTree.js'][104] = 0;
  _$jscoverage['collections/RedBlackTree.js'][105] = 0;
  _$jscoverage['collections/RedBlackTree.js'][107] = 0;
  _$jscoverage['collections/RedBlackTree.js'][110] = 0;
  _$jscoverage['collections/RedBlackTree.js'][111] = 0;
  _$jscoverage['collections/RedBlackTree.js'][112] = 0;
  _$jscoverage['collections/RedBlackTree.js'][113] = 0;
  _$jscoverage['collections/RedBlackTree.js'][114] = 0;
  _$jscoverage['collections/RedBlackTree.js'][115] = 0;
  _$jscoverage['collections/RedBlackTree.js'][117] = 0;
  _$jscoverage['collections/RedBlackTree.js'][118] = 0;
  _$jscoverage['collections/RedBlackTree.js'][119] = 0;
  _$jscoverage['collections/RedBlackTree.js'][120] = 0;
  _$jscoverage['collections/RedBlackTree.js'][121] = 0;
  _$jscoverage['collections/RedBlackTree.js'][123] = 0;
  _$jscoverage['collections/RedBlackTree.js'][124] = 0;
  _$jscoverage['collections/RedBlackTree.js'][125] = 0;
  _$jscoverage['collections/RedBlackTree.js'][126] = 0;
  _$jscoverage['collections/RedBlackTree.js'][127] = 0;
  _$jscoverage['collections/RedBlackTree.js'][128] = 0;
  _$jscoverage['collections/RedBlackTree.js'][130] = 0;
  _$jscoverage['collections/RedBlackTree.js'][132] = 0;
  _$jscoverage['collections/RedBlackTree.js'][135] = 0;
  _$jscoverage['collections/RedBlackTree.js'][137] = 0;
  _$jscoverage['collections/RedBlackTree.js'][153] = 0;
  _$jscoverage['collections/RedBlackTree.js'][157] = 0;
  _$jscoverage['collections/RedBlackTree.js'][158] = 0;
  _$jscoverage['collections/RedBlackTree.js'][162] = 0;
  _$jscoverage['collections/RedBlackTree.js'][163] = 0;
  _$jscoverage['collections/RedBlackTree.js'][164] = 0;
  _$jscoverage['collections/RedBlackTree.js'][165] = 0;
  _$jscoverage['collections/RedBlackTree.js'][166] = 0;
  _$jscoverage['collections/RedBlackTree.js'][171] = 0;
  _$jscoverage['collections/RedBlackTree.js'][172] = 0;
  _$jscoverage['collections/RedBlackTree.js'][173] = 0;
  _$jscoverage['collections/RedBlackTree.js'][174] = 0;
  _$jscoverage['collections/RedBlackTree.js'][175] = 0;
  _$jscoverage['collections/RedBlackTree.js'][177] = 0;
  _$jscoverage['collections/RedBlackTree.js'][178] = 0;
  _$jscoverage['collections/RedBlackTree.js'][179] = 0;
  _$jscoverage['collections/RedBlackTree.js'][180] = 0;
  _$jscoverage['collections/RedBlackTree.js'][181] = 0;
}
_$jscoverage['collections/RedBlackTree.js'][1]++;
var define = require("../define").define, Tree = require("./Tree"), base = require("../base"), multiply = base.string.multiply;
_$jscoverage['collections/RedBlackTree.js'][6]++;
var RED = "red", BLACK = "black";
_$jscoverage['collections/RedBlackTree.js'][8]++;
var isRed = (function (node) {
  _$jscoverage['collections/RedBlackTree.js'][9]++;
  return node != null && node.red;
});
_$jscoverage['collections/RedBlackTree.js'][12]++;
var makeNode = (function (data, parent) {
  _$jscoverage['collections/RedBlackTree.js'][13]++;
  return ({data: data, red: true, left: null, right: null});
});
_$jscoverage['collections/RedBlackTree.js'][21]++;
var insert = (function (root, data, compare) {
  _$jscoverage['collections/RedBlackTree.js'][22]++;
  if (root == null) {
    _$jscoverage['collections/RedBlackTree.js'][23]++;
    return makeNode(data, null);
  }
  else {
    _$jscoverage['collections/RedBlackTree.js'][26]++;
    var cmp = compare(data, root.data);
    _$jscoverage['collections/RedBlackTree.js'][27]++;
    if (cmp) {
      _$jscoverage['collections/RedBlackTree.js'][28]++;
      var dir = cmp == -1? "left": "right";
      _$jscoverage['collections/RedBlackTree.js'][29]++;
      var otherDir = dir == "left"? "right": "left";
      _$jscoverage['collections/RedBlackTree.js'][30]++;
      root[dir] = insert(root[dir], data, compare);
      _$jscoverage['collections/RedBlackTree.js'][31]++;
      var node = root[dir];
      _$jscoverage['collections/RedBlackTree.js'][33]++;
      if (isRed(node)) {
        _$jscoverage['collections/RedBlackTree.js'][35]++;
        var sibling = root[otherDir];
        _$jscoverage['collections/RedBlackTree.js'][36]++;
        if (isRed(sibling)) {
          _$jscoverage['collections/RedBlackTree.js'][38]++;
          root.red = true;
          _$jscoverage['collections/RedBlackTree.js'][39]++;
          node.red = false;
          _$jscoverage['collections/RedBlackTree.js'][40]++;
          sibling.red = false;
        }
        else {
          _$jscoverage['collections/RedBlackTree.js'][43]++;
          if (isRed(node[dir])) {
            _$jscoverage['collections/RedBlackTree.js'][45]++;
            root = rotateSingle(root, otherDir);
          }
          else {
            _$jscoverage['collections/RedBlackTree.js'][46]++;
            if (isRed(node[otherDir])) {
              _$jscoverage['collections/RedBlackTree.js'][48]++;
              root = rotateDouble(root, otherDir);
            }
          }
        }
      }
    }
  }
  _$jscoverage['collections/RedBlackTree.js'][55]++;
  return root;
});
_$jscoverage['collections/RedBlackTree.js'][58]++;
var rotateSingle = (function (root, dir) {
  _$jscoverage['collections/RedBlackTree.js'][59]++;
  var otherDir = dir == "left"? "right": "left";
  _$jscoverage['collections/RedBlackTree.js'][60]++;
  var save = root[otherDir];
  _$jscoverage['collections/RedBlackTree.js'][61]++;
  root[otherDir] = save[dir];
  _$jscoverage['collections/RedBlackTree.js'][62]++;
  save[dir] = root;
  _$jscoverage['collections/RedBlackTree.js'][63]++;
  root.red = true;
  _$jscoverage['collections/RedBlackTree.js'][64]++;
  save.red = false;
  _$jscoverage['collections/RedBlackTree.js'][65]++;
  return save;
});
_$jscoverage['collections/RedBlackTree.js'][68]++;
var rotateDouble = (function (root, dir) {
  _$jscoverage['collections/RedBlackTree.js'][69]++;
  var otherDir = dir == "left"? "right": "left";
  _$jscoverage['collections/RedBlackTree.js'][70]++;
  root[otherDir] = rotateSingle(root[otherDir], otherDir);
  _$jscoverage['collections/RedBlackTree.js'][71]++;
  return rotateSingle(root, dir);
});
_$jscoverage['collections/RedBlackTree.js'][75]++;
var remove = (function (root, data, done, compare) {
  _$jscoverage['collections/RedBlackTree.js'][76]++;
  if (root == null) {
    _$jscoverage['collections/RedBlackTree.js'][77]++;
    done.done = true;
  }
  else {
    _$jscoverage['collections/RedBlackTree.js'][79]++;
    var dir;
    _$jscoverage['collections/RedBlackTree.js'][80]++;
    if (compare(data, root.data) == 0) {
      _$jscoverage['collections/RedBlackTree.js'][81]++;
      if (root.left == null || root.right == null) {
        _$jscoverage['collections/RedBlackTree.js'][82]++;
        var save = root[root.left == null? "right": "left"];
        _$jscoverage['collections/RedBlackTree.js'][84]++;
        if (isRed(root)) {
          _$jscoverage['collections/RedBlackTree.js'][85]++;
          done.done = true;
        }
        else {
          _$jscoverage['collections/RedBlackTree.js'][86]++;
          if (isRed(save)) {
            _$jscoverage['collections/RedBlackTree.js'][87]++;
            save.red = false;
            _$jscoverage['collections/RedBlackTree.js'][88]++;
            done.done = true;
          }
        }
        _$jscoverage['collections/RedBlackTree.js'][90]++;
        return save;
      }
      else {
        _$jscoverage['collections/RedBlackTree.js'][93]++;
        var heir = root.right, p;
        _$jscoverage['collections/RedBlackTree.js'][94]++;
        while (heir.left != null) {
          _$jscoverage['collections/RedBlackTree.js'][95]++;
          p = heir;
          _$jscoverage['collections/RedBlackTree.js'][96]++;
          heir = heir.left;
}
        _$jscoverage['collections/RedBlackTree.js'][98]++;
        p && (p.left = null);
        _$jscoverage['collections/RedBlackTree.js'][99]++;
        root.data = heir.data;
        _$jscoverage['collections/RedBlackTree.js'][100]++;
        data = heir.data;
      }
    }
    _$jscoverage['collections/RedBlackTree.js'][103]++;
    dir = compare(data, root.data) == -1? "left": "right";
    _$jscoverage['collections/RedBlackTree.js'][104]++;
    root[dir] = remove(root[dir], data, done, compare);
    _$jscoverage['collections/RedBlackTree.js'][105]++;
    ! done.done && (root = removeBalance(root, dir, done));
  }
  _$jscoverage['collections/RedBlackTree.js'][107]++;
  return root;
});
_$jscoverage['collections/RedBlackTree.js'][110]++;
var removeBalance = (function (root, dir, done) {
  _$jscoverage['collections/RedBlackTree.js'][111]++;
  var notDir = dir == "left"? "right": "left";
  _$jscoverage['collections/RedBlackTree.js'][112]++;
  var p = root, s = p[notDir];
  _$jscoverage['collections/RedBlackTree.js'][113]++;
  if (isRed(s)) {
    _$jscoverage['collections/RedBlackTree.js'][114]++;
    root = rotateSingle(root, dir);
    _$jscoverage['collections/RedBlackTree.js'][115]++;
    s = p[notDir];
  }
  _$jscoverage['collections/RedBlackTree.js'][117]++;
  if (s != null) {
    _$jscoverage['collections/RedBlackTree.js'][118]++;
    if (! isRed(s.left) && ! isRed(s.right)) {
      _$jscoverage['collections/RedBlackTree.js'][119]++;
      isRed(p) && (done.done = true);
      _$jscoverage['collections/RedBlackTree.js'][120]++;
      p.red = 0;
      _$jscoverage['collections/RedBlackTree.js'][121]++;
      s.red = 1;
    }
    else {
      _$jscoverage['collections/RedBlackTree.js'][123]++;
      var save = p.red, newRoot = (root === p);
      _$jscoverage['collections/RedBlackTree.js'][124]++;
      p = (isRed(s[notDir])? rotateSingle: rotateDouble)(p, dir);
      _$jscoverage['collections/RedBlackTree.js'][125]++;
      p.red = save;
      _$jscoverage['collections/RedBlackTree.js'][126]++;
      p.left.red = p.right.red = 0;
      _$jscoverage['collections/RedBlackTree.js'][127]++;
      if (newRoot) {
        _$jscoverage['collections/RedBlackTree.js'][128]++;
        root = p;
      }
      else {
        _$jscoverage['collections/RedBlackTree.js'][130]++;
        root[dir] = p;
      }
      _$jscoverage['collections/RedBlackTree.js'][132]++;
      done.done = true;
    }
  }
  _$jscoverage['collections/RedBlackTree.js'][135]++;
  return root;
});
_$jscoverage['collections/RedBlackTree.js'][137]++;
var RedBlackTree;
_$jscoverage['collections/RedBlackTree.js'][153]++;
module.exports = exports = define(Tree, {instance: {insert: (function (data) {
  _$jscoverage['collections/RedBlackTree.js'][157]++;
  this.__root = insert(this.__root, data, this.compare);
  _$jscoverage['collections/RedBlackTree.js'][158]++;
  this.__root.red = false;
}), remove: (function (data) {
  _$jscoverage['collections/RedBlackTree.js'][162]++;
  var done = {done: false};
  _$jscoverage['collections/RedBlackTree.js'][163]++;
  var root = remove(this.__root, data, done, this.compare);
  _$jscoverage['collections/RedBlackTree.js'][164]++;
  if (root != null) {
    _$jscoverage['collections/RedBlackTree.js'][165]++;
    root.red = 0;
  }
  _$jscoverage['collections/RedBlackTree.js'][166]++;
  this.__root = root;
}), __printNode: (function (node, level) {
  _$jscoverage['collections/RedBlackTree.js'][171]++;
  var str = [];
  _$jscoverage['collections/RedBlackTree.js'][172]++;
  if (node == null || node == undefined) {
    _$jscoverage['collections/RedBlackTree.js'][173]++;
    str.push(multiply("\t", level));
    _$jscoverage['collections/RedBlackTree.js'][174]++;
    str.push("~");
    _$jscoverage['collections/RedBlackTree.js'][175]++;
    console.log(str.join(""));
  }
  else {
    _$jscoverage['collections/RedBlackTree.js'][177]++;
    this.__printNode(node.right, level + 1);
    _$jscoverage['collections/RedBlackTree.js'][178]++;
    str.push(multiply("\t", level));
    _$jscoverage['collections/RedBlackTree.js'][179]++;
    str.push((node.red? "RED": "BLACK") + ":" + node.data + "\n");
    _$jscoverage['collections/RedBlackTree.js'][180]++;
    console.log(str.join(""));
    _$jscoverage['collections/RedBlackTree.js'][181]++;
    this.__printNode(node.left, level + 1);
  }
})}});
_$jscoverage['collections/RedBlackTree.js'].source = ["var define = require(\"../define\").define,","        Tree = require(\"./Tree\"),","        base = require(\"../base\"),","        multiply = base.string.multiply;","","var RED = \"red\", BLACK = \"black\";","","var isRed = function(node) {","    return node != null &amp;&amp; node.red;","};","","var makeNode = function(data, parent) {","    return {","        data : data,","        red : true,","        left : null,","        right : null","    }","};","","var insert = function(root, data, compare) {","    if (root == null) {","        return makeNode(data, null);","","    } else {","        var cmp = compare(data, root.data);","        if (cmp) {","            var dir = cmp == -1 ? \"left\" : \"right\";","            var otherDir = dir == \"left\" ? \"right\" : \"left\";","            root[dir] = insert(root[dir], data, compare);","            var node = root[dir];","","            if (isRed(node)) {","","                var sibling = root[otherDir];","                if (isRed(sibling)) {","                    /* Case 1 */","                    root.red = true;","                    node.red = false;","                    sibling.red = false;","                } else {","","                    if (isRed(node[dir])) {","","                        root = rotateSingle(root, otherDir);","                    } else if (isRed(node[otherDir])) {","","                        root = rotateDouble(root, otherDir);","                    }","                }","","            }","        }","    }","    return root;","};","","var rotateSingle = function(root, dir) {","    var otherDir = dir == \"left\" ? \"right\" : \"left\";","    var save = root[otherDir];","    root[otherDir] = save[dir];","    save[dir] = root;","    root.red = true;","    save.red = false;","    return save;","};","","var rotateDouble = function(root, dir) {","    var otherDir = dir == \"left\" ? \"right\" : \"left\";","    root[otherDir] = rotateSingle(root[otherDir], otherDir);","    return rotateSingle(root, dir);","};","","","var remove = function (root, data, done, compare) {","    if (root == null) {","        done.done = true;","    } else {","        var dir;","        if (compare(data, root.data) == 0) {","            if (root.left == null || root.right == null) {","                var save = root[root.left == null ? \"right\" : \"left\"];","                /* Case 0 */","                if (isRed(root)) {","                    done.done = true;","                } else if (isRed(save)) {","                    save.red = false;","                    done.done = true;","                }","                return save;","            }","            else {","                var heir = root.right, p;","                while (heir.left != null) {","                    p = heir;","                    heir = heir.left;","                }","                p &amp;&amp; (p.left = null);","                root.data = heir.data;","                data = heir.data;","            }","        }","        dir = compare(data, root.data) == -1 ? \"left\" : \"right\";","        root[dir] = remove(root[dir], data, done, compare);","        !done.done &amp;&amp; (root = removeBalance(root, dir, done));","    }","    return root;","};","","var removeBalance = function(root, dir, done) {","    var notDir = dir == \"left\" ? \"right\" : \"left\";","    var p = root, s = p[notDir];","    if (isRed(s)) {","        root = rotateSingle(root, dir);","        s = p[notDir];","    }","    if (s != null) {","        if (!isRed(s.left) &amp;&amp; !isRed(s.right)) {","            isRed(p) &amp;&amp; (done.done = true);","            p.red = 0;","            s.red = 1;","        } else {","            var save = p.red, newRoot = ( root === p );","            p = (isRed(s[notDir]) ? rotateSingle : rotateDouble)(p, dir);","            p.red = save;","            p.left.red = p.right.red = 0;","            if (newRoot) {","                root = p;","            } else {","                root[dir] = p;","            }","            done.done = true;","        }","    }","    return root;","};","var RedBlackTree;","/**"," * @class &lt;p&gt;A RedBlack tree is a form of a self balancing binary tree.&lt;/p&gt;"," *"," * &lt;b&gt;Performance&lt;/b&gt;"," * &lt;table&gt;"," *     &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;Best&lt;/td&gt;&lt;td&gt;Worst&lt;/td&gt;&lt;/tr&gt;"," *     &lt;tr&gt;&lt;td&gt;Space&lt;/td&gt;&lt;td&gt;O(n)&lt;/td&gt;&lt;td&gt;O(n)&lt;/td&gt;&lt;/tr&gt;"," *     &lt;tr&gt;&lt;td&gt;Search&lt;/td&gt;&lt;td&gt;O(log n)&lt;/td&gt;&lt;td&gt;O(log n)&lt;/td&gt;&lt;/tr&gt;"," *     &lt;tr&gt;&lt;td&gt;Insert&lt;/td&gt;&lt;td&gt;O(log n)&lt;/td&gt;&lt;td&gt;O(log n)&lt;/td&gt;&lt;/tr&gt;"," *     &lt;tr&gt;&lt;td&gt;Delete&lt;/td&gt;&lt;td&gt;O(log n)&lt;/td&gt;&lt;td&gt;O(log n)&lt;/td&gt;&lt;/tr&gt;"," * &lt;table&gt;"," *     @name RedBlackTree"," * @augments comb.collections.Tree"," * @memberOf comb.collections"," */","module.exports = exports  = define(Tree, {","    instance : {","        /**@lends comb.collections.RedBlackTree.prototype*/","        insert : function(data) {","            this.__root = insert(this.__root, data, this.compare);","            this.__root.red = false;","        },","","        remove : function(data) {","            var done = {done : false};","            var root = remove(this.__root, data, done, this.compare);","            if (root != null)","                root.red = 0;","            this.__root = root;","        },","","","        __printNode : function(node, level) {","            var str = [];","            if (node == null || node == undefined) {","                str.push(multiply('\\t', level));","                str.push(\"~\");","                console.log(str.join(\"\"));","            } else {","                this.__printNode(node.right, level + 1);","                str.push(multiply('\\t', level));","                str.push((node.red ? \"RED\" : \"BLACK\") + \":\" + node.data + \"\\n\");","                console.log(str.join(\"\"));","                this.__printNode(node.left, level + 1);","            }","        }","","    }","});",""];
