方法insert报错
你们这个代码有没有自己测试过?我怎么测试报错,说insert方法不存在
你们这个代码有没有自己测试过?我怎么测试报错,说insert方法不存在
2017-09-10
var root = null;
function BinaryTree() {
var Node = function(key) {
this.key = key;
this.left = null;
this.right = null;
}
}
var insertNode = function(node, newNode) {
if (newNode.key < node.key) {
if (node.left === null) {
node.left = newNode
} else {
insertNode(node, newNode)
}
} else {
if (node.right === null) {
node.right = newNode
} else {
insertNode(node, newNode)
}
}
}
this.insert = function(key) {
var newNode = new Node(key);
if (root === null) {
root = newNode
} else {
insertNode(node, newNode)
}
}
var nodes = [8, 3, 10, 1, 6, 14, 4, 7, 13];
var binaryTree = new BinaryTree();
nodes.forEach(function(key) {
binaryTree.insert(key);
})
//报错....
举报