我的代码会报错,求大神指点!!
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Binary Tree</title>
</head>
<body>
<script type="text/javascript">
function BinaryTree () {
var Node=function (key) {
this.key=key;
this.left=null;
this.right=null;
};
var root=null;
var insertNode=function (node,noder) {
if (noder.key<node.key) {
if (node.left===null) {
node.left=noder;
} else {
insertNode(node,left,noder);//这里会出现错误,但将所有的noder换成node就不会报错了!为什么呢?
}
} else {
if (node.right===null) {
node.right=noder;
} else {
insertNode(node,right,noder);
}
}
}
this.insert=function (key) {
var noder=new Node(key);
if (root===null) {
root=noder;
} else {
insertNode(root,noder);
}
};
}
var nodes=[8,3,10,1,6,14,4,7,13];
var binaryTree=new BinaryTree();
nodes.forEach( function(key) {
binaryTree.insert(key);
});
</script>
</body>
</html>