我正在尝试对二叉搜索树中的数组输入执行 运行 回调函数。我一直被告知我的回调函数 "is not a function"
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"
这是我创建二叉搜索树的代码以及添加到树中的原型方法。还有 preorder 方法以及一些调用来执行函数来测试它们...
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 ]*
当我尝试执行 depthFirstPre 函数时,我总是收到“回调不是函数”的提示,我很困惑为什么。
感谢您的帮助,我们将不胜感激!
您可以添加一张支票并使用 this.left
或 this.right
进行通话。
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);
if (this.left) this.left.depthFirstPre(callback);
if (this.right) this.right.depthFirstPre(callback);
};
BinarySearchTree.prototype.depthFirst = function() {
return [
this.value,
...(this.left ? this.left.depthFirst() : []),
...(this.right ? this.right.depthFirst() : [])
];
};
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 ]
console.log(...binarySearchTree.depthFirst());
这是我创建二叉搜索树的代码以及添加到树中的原型方法。还有 preorder 方法以及一些调用来执行函数来测试它们...
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 ]*
当我尝试执行 depthFirstPre 函数时,我总是收到“回调不是函数”的提示,我很困惑为什么。
感谢您的帮助,我们将不胜感激!
您可以添加一张支票并使用 this.left
或 this.right
进行通话。
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);
if (this.left) this.left.depthFirstPre(callback);
if (this.right) this.right.depthFirstPre(callback);
};
BinarySearchTree.prototype.depthFirst = function() {
return [
this.value,
...(this.left ? this.left.depthFirst() : []),
...(this.right ? this.right.depthFirst() : [])
];
};
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 ]
console.log(...binarySearchTree.depthFirst());