UNPKG

binary-search-tree-adt

Version:
8 lines 1.77 kB
/*! * binary-search-tree-adt * v0.0.1 - 2014-08-27 * https://github.com/pasangsherpa/binary-search-tree-adt * (c) Pasang Sherpa <pgyalzen@gmail.com> (https://github.com/pasangsherpa) * MIT License */ !function(){"use strict";var a=function(){function a(a){this.element=a||null,this.right=this.left=null}function b(b){var d=new a(b);return o=c(o,d),p++,d.element}function c(a,b){return null===a?b:(b.element<a.element?a.left=c(a.left,b):a.right=c(a.right,b),a)}function d(){if(m())throw new Error("removeMin(): Tree is empty.")}function e(){if(m())throw new Error("removeMin(): Tree is empty.")}function f(){if(m())throw new Error("removeMin(): Tree is empty.");for(var a=o,b=o;null!==a.left;)b=a,a=a.left;b.left=null}function g(){if(m())throw new Error("removeMin(): Tree is empty.");for(var a=o,b=o;null!==a.right;)b=a,a=a.right;b.right=null}function h(){if(m())throw new Error("removeMin(): Tree is empty.");return o}function i(a){if(m())throw new Error("find(): Tree is empty.");var b=j(o,a);if(null===b)throw new Error("find(): No such element.");return b.element}function j(a,b){if(null===a)return null;if(a.element===b)return a;var c=j(a.left,b);return null===c&&(c=j(a.right,b)),c}function k(){if(m())throw new Error("removeMin(): Tree is empty.");for(var a=o;null!==a.left;)a=a.left;return a.element}function l(){if(m())throw new Error("removeMin(): Tree is empty.");for(var a=o;null!==a.right;)a=a.right;return a.element}function m(){return 0===p}function n(){return p}var o=null,p=0;return{add:b,remove:d,removeAllOccurrence:e,removeMin:f,removeMax:g,getRoot:h,find:i,findMin:k,findMax:l,isEmpty:m,size:n}};"function"==typeof define&&define.amd?define(function(){return a}):"undefined"!=typeof module&&module.exports?module.exports=a:window.BinarySearchTree=a}();