Build 545032stable
Module 878861
1,895 characters · in no known chunkDependencies
Source Raw
function(e,t,n){var i=n(744784);function r(e){this.data=e,this.left=null,this.right=null,this.red=!0}function s(e){this._root=null,this._comparator=e,this.size=0}function a(e){return null!==e&&e.red}function o(e,t){var n=e.get_child(!t);return e.set_child(!t,n.get_child(t)),n.set_child(t,e),e.red=!0,n.red=!1,n}function l(e,t){return e.set_child(!t,o(e.get_child(!t),!t)),o(e,t)}r.prototype.get_child=function(e){return e?this.right:this.left},r.prototype.set_child=function(e,t){e?this.right=t:this.left=t},s.prototype=new i,s.prototype.insert=function(e){var t=!1;if(null===this._root)this._root=new r(e),t=!0,this.size++;else{var n=new r(void 0),i=0,s=0,u=null,c=n,d=null,_=this._root;for(c.right=this._root;;){if(null===_?(_=new r(e),d.set_child(i,_),t=!0,this.size++):a(_.left)&&a(_.right)&&(_.red=!0,_.left.red=!1,_.right.red=!1),a(_)&&a(d)){var f=c.right===u;_===d.get_child(s)?c.set_child(f,o(u,!s)):c.set_child(f,l(u,!s))}var h=this._comparator(_.data,e);if(0===h)break;s=i,i=h<0,null!==u&&(c=u),u=d,d=_,_=_.get_child(i)}this._root=n.right}return this._root.red=!1,t},s.prototype.remove=function(e){if(null===this._root)return!1;var t=new r(void 0),n=t;n.right=this._root;for(var i=null,s=null,u=null,c=1;null!==n.get_child(c);){var d=c;s=i,i=n,n=n.get_child(c);var _=this._comparator(e,n.data);if(c=_>0,0===_&&(u=n),!a(n)&&!a(n.get_child(c))){if(a(n.get_child(!c))){var f=o(n,c);i.set_child(d,f),i=f}else if(!a(n.get_child(!c))){var h=i.get_child(!d);if(null!==h)if(a(h.get_child(!d))||a(h.get_child(d))){var p=s.right===i;a(h.get_child(d))?s.set_child(p,l(i,d)):a(h.get_child(!d))&&s.set_child(p,o(i,d));var E=s.get_child(p);E.red=!0,n.red=!0,E.left.red=!1,E.right.red=!1}else i.red=!1,h.red=!0,n.red=!0}}}return null!==u&&(u.data=n.data,i.set_child(i.right===n,n.get_child(null===n.left)),this.size--),this._root=t.right,null!==this._root&&(this._root.red=!1),null!==u},e.exports=s}