- /******************************
- * class: Binary tree search
- *
- *
- *
- *******************************/
- var tbst = function (isLower, isGreater, equals) {
- this.hi = null;
- this.elem = null;
- this.hd=null;
- // Comparators
- this.isLower = isLower;
- this.isGreater = isGreater;
- this.equals = equals;
- /**
- * Retrieves the eldest son
- * a left subtree
- * Returns an element
- */
- this.mayor = function (tbst) {
- if (! tbst.hd.isEmpty()) {
- return tbst.mayor(this.hd);
- }else {
- return tbst.elem;
- }
- }
- }
- tbst.prototype.isLeaf = function () {
- return (this.hi==null) && (this.hd==null);
- }
- tbst.prototype.isEmpty = function () {
- return (this.elem==null);
- }
- tbst.prototype.clone = function () {
- if (! this.isEmpty()) {
- var bst = new tbst(this.isLower,this.isGreater,this.equals);
- bst.elem = this.elem;
- bst.hi = this.hi.clone();
- bst.hd = this.hd.clone();
- return bst;
- }else {
- return null;
- }
- }
- // deleting an element
- tbst.prototype.eliminar = function (e){
- if (! this.isEmpty() ) {
- if ( this.isLower(e , this.elem) ) {
- this.hi.eliminar(e);
- }else if ( this.isGreater(e , this.elem )) {
- this.hd.eliminar(e);
- }else { // Hay que eliminarlo
- // hi = null
- if ( this.hd && this.hi.isEmpty()) {
- this.elem= this.hd.elem;
- delete this.hd;
- this.hd= new tbst(this.isLower,this.isGreater,this.equals);
- }else if ( this.hi && this.hd.isEmpty() ) {
- // hd = null
- this.elem = this.hi.elem;
- delete this.hi;
- this.hi = new tbst(this.isLower,this.isGreater,this.equals);
- }else {
- // hi!= null && hd!= null
- var tbstMayor = this.mayor(this.hi);
- this.elem = tbstMayor;
- this.hi.eliminar(tbstMayor);
- }
- }
- }
- }
- // inserting an element
- tbst.prototype.insertar = function (e){
- if ( this.isEmpty() ) {
- this.elem = e;
- this.hi = new tbst(this.isLower,this.isGreater,this.equals);
- this.hd = new tbst(this.isLower,this.isGreater,this.equals);
- }else {
- if (this.isLower (e , this.elem) ) {
- this.hi.insertar(e);
- }else if (this.isGreater (e , this.elem )){
- this.hd.insertar(e);
- }else
- throw new Error ("equal");
- }
- }
- // pre-order traversal
- tbst.prototype.preorder = function (arr) {
- if (! this.isEmpty()) {
- arr.push(this.elem);
- this.hi.inorder(arr);
- this.hd.inorder(arr);
- }
- }
- // in-order traversal
- tbst.prototype.inorder = function (arr) {
- if (! this.isEmpty()) {
- this.hi.inorder(arr);
- arr.push(this.elem);
- this.hd.inorder(arr);
- }
- }
- // post-order traversal
- tbst.prototype.postorder = function (arr) {
- if (! this.isEmpty()) {
- this.hi.inorder(arr);
- this.hd.inorder(arr);
- arr.push(this.elem);
- }
- }
- //Travel width
- tbst.prototype.bfs = function (lista){
- // Cola
- var arr = new Array();
- if (! this.isEmpty ()) {
- arr.push(this);
- var i= 0;
- while (arr[i]) {
- lista.push(arr[i].elem);
- if (! arr[i].hi.isEmpty()) {
- arr.push(arr[i].hi);
- }
- if (! arr[i].hd.isEmpty()) {
- arr.push(arr[i].hd);
- }
- if (i!= arr.length)
- delete arr[i];
- i++;
- }
- }
- }