Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am trying to do a run a callback function on an array input into a binary search tree.. I keep getting told my callback function "is not a function"

Here is my code to create the binary search tree as well as a prototype method to add to the tree. There is also the preorder method along with some calls to execute the functions to test them…

function BinarySearchTree(value) {
    this.value = value;
    this.right = null;
    this.left = null;
  }
  
  BinarySearchTree.prototype.add = function(value) {
    if (value < this.value && this.left) {
      this.left.add(value);
    } else if (value < this.value) {
      this.left = new BinarySearchTree(value);
    }
  
    if (value > this.value && this.right) {
      this.right.add(value);
    } else if (value > this.value) {
      this.right = new BinarySearchTree(value);
    }
  
  };

 BinarySearchTree.prototype.depthFirstPre = function(callback) {
    callback(this.value);
    this.depthFirstPre(this.left);
    this.depthFirstPre(this.right);
  };

binarySearchTree = new BinarySearchTree(5);
var array = [];
var func = function(value){ array.push(value); };
binarySearchTree.add(2);
binarySearchTree.add(3);
binarySearchTree.add(7);
binarySearchTree.add(6);
binarySearchTree.depthFirstPre(func);
console.log(array) -> *should output [ 5, 2, 3, 7, 6 ]*

I keep getting "callback is not a function" when I try and execute the depthFirstPre function and I am confused as to why.

Thanks for any help, it will be greatly appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could add a check and take this.left or this.right for the call.

function BinarySearchTree(value) {
    this.value = value;
    this.right = null;
    this.left = null;
}

BinarySearchTree.prototype.add = function(value) {
    if (value < this.value) {
        if (this.left) this.left.add(value);
        else this.left = new BinarySearchTree(value);
    }

    if (value > this.value){
        if (this.right) this.right.add(value);
        else this.right = new BinarySearchTree(value);
    }
};

BinarySearchTree.prototype.depthFirstPre = function(callback) {
    callback(this.value);
    this.left && this.left.depthFirstPre(callback);
    this.right && this.right.depthFirstPre(callback);
};

var binarySearchTree = new BinarySearchTree(5),
    array = [],
    func = function(value) { array.push(value); };

binarySearchTree.add(2);
binarySearchTree.add(3);
binarySearchTree.add(7);
binarySearchTree.add(6);
binarySearchTree.depthFirstPre(func);

console.log(array); // [ 5, 2, 3, 7, 6 ]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading