(function(){function f(a,b){this.key=a,this.value=b,this.color=e,this.left=null,this.right=null,this.parent=null}function g(a,b,c){this.tree=a,this.start=b,this.end=c;var d=this;this.walk=function e(a,f){if(a===null)return;b!==undefined&&a.keyc?e(a.left,f):(e(a.left,f),f(a.value,a.key,d.tree),e(a.right,f))}}function h(){this.root=null,this.balancer=new k(this)}function k(a){this.tree=a}function l(a){return a===null?d:a.color}function m(a,b){while(a!==null){if(b===a.key)return a;ba.key&&(a=a.right)}return a}var a={},b=this,c=b.redblack;typeof module!="undefined"&&module.exports?module.exports=a:b.redblack=a,a.VERSION="0.1.2",a.noConflict=function(){return b.redblack=c,a},a.tree=function(){return new h};var d=a.BLACK="black",e=a.RED="red";f.prototype.grandparent=function(){return this.parent===null?null:this.parent.parent},f.prototype.sibling=function(){return this.parent===null?null:this===this.parent.left?this.parent.right:this.parent.left},f.prototype.uncle=function(){return this.parent===null?null:this.parent.sibling()},g.prototype.forEach=function(a){this.walk(this.tree.root,a)},g.prototype.map=function(a){var b=[];return this.forEach(function(c,d,e){b.push(a(c,d,e))}),b},h.prototype.get=function(a){var b=m(this.root,a);return b===null?null:b.value},h.prototype.insert=function(a,b){var c=new f(a,b);if(this.root===null)this.root=c;else{var d=this.root;for(;;)if(ad.key)){d.value=b;return}if(d.right===null){d.right=c;break}d=d.right}c.parent=d}this.balancer.inserted(c)},h.prototype.delete=function(a){var b=m(this.root,a);if(b===null)return;if(b.left!==null&&b.right!==null){var c=b.left;while(c.right!==null)c=c.right;b.key=c.key,b.value=c.value,b=c}var f=b.right===null?b.left:b.right;l(b)===d&&(b.color=l(f),this.balancer.deleted(b)),this.balancer.replaceNode(b,f),l(this.root)===e&&(this.root.color=d)},h.prototype.range=function(a,b){return new g(this,a,b)};for(var i in g.prototype)if(g.prototype.hasOwnProperty(i)){var j=g.prototype[i];h.prototype[i]=function(){var a=new g(this);return j.apply(a,arguments)}}k.prototype.inserted=function(a){this.insertCase1(a)},k.prototype.deleted=function(a){this.deleteCase1(a)},k.prototype.replaceNode=function(a,b){a.parent===null?this.tree.root=b:a===a.parent.left?a.parent.left=b:a.parent.right=b,b!==null&&(b.parent=a.parent)},k.prototype.rotateLeft=function(a){var b=a.right;this.replaceNode(a,b),a.right=b.left,b.left!==null&&(b.left.parent=a),b.left=a,a.parent=b},k.prototype.rotateRight=function(a){var b=a.left;this.replaceNode(a,b),a.left=b.right,b.right!==null&&(b.right.parent=a),b.right=a,a.parent=b},k.prototype.insertCase1=function(a){a.parent===null?a.color=d:this.insertCase2(a)},k.prototype.insertCase2=function(a){if(l(a.parent)===d)return;this.insertCase3(a)},k.prototype.insertCase3=function(a){var b=a.uncle(),c=a.grandparent();b!==null&&l(b)===e?(a.parent.color=d,b.color=d,c.color=e,this.insertCase1(c)):this.insertCase4(a)},k.prototype.insertCase4=function(a){var b=a.grandparent();a===a.parent.right&&a.parent===b.left?(this.rotateLeft(a.parent),a=a.left):a===a.parent.left&&a.parent===b.right&&(this.rotateRight(a.parent),a=a.right),this.insertCase5(a)},k.prototype.insertCase5=function(a){var b=a.grandparent();a.parent.color=d,b.color=e,a===a.parent.left&&a.parent===b.left?this.rotateRight(b):a===a.parent.right&&a.parent===b.right&&this.rotateLeft(b)},k.prototype.deleteCase1=function(a){a.parent!==null&&this.deleteCase2(a)},k.prototype.deleteCase2=function(a){var b=a.sibling();l(b)===e&&(a.parent.color=e,b.color=d,a===a.parent.left?this.rotateLeft(a.parent):this.rotateRight(a.parent)),this.deleteCase3(a)},k.prototype.deleteCase3=function(a){var b=a.sibling();l(a.parent)===d&&l(b)===d&&l(b.left)===d&&l(b.right)===d?(b.color=e,this.deleteCase1(a.parent)):this.deleteCase4(a)},k.prototype.deleteCase4=function(a){var b=a.sibling();l(a.parent)===e&&l(b)===d&&l(b.left)===d&&l(b.right)===d?(b.color=e,a.parent.color=d):this.deleteCase5(a)},k.prototype.deleteCase5=function(a){var b=a.sibling();a===a.parent.left&&l(b)===d&&l(b.left)===e&&l(b.right)===d?(b.color=e,b.left.color=d,this.rotateRight(b)):a===a.parent.right&&l(b)===d&&l(b.right)===e&&l(b.left)===d&&(b.color=e,b.right.color=d,this.rotateLeft(b)),this.deleteCase6(a)},k.prototype.deleteCase6=function(a){var b=a.sibling();b.color=l(a.parent),a.parent.color=d,a===a.parent.left?(b.right.color=d,this.rotateLeft(a.parent)):(b.left.color=d,this.rotateRight(a.parent))}})();