当它循环到8下的left3时,就无法获取3下面的left 。难道只有我是这样吗
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></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,newNode){
if(newNode.key<node.key){
if(node.left == null){
node.left = newNode.key;
}
else{
insertNode(node.left,newNode);
}
}
else{
if(node.right == null){
node.right = newNode.key;
}
else{
insertNode(node.right,newNode);
}
}
}
this.insert = function(key){
var newNode = new Node(key);
if(root == null){root = newNode}
else{insertNode(root,newNode)}
};
var inOrderTraverseNode = function(node,callback){
if(node!==null){
inOrderTraverseNode(node.left,callback);
callback(node.key);
inOrderTraverseNode(node.right,callback);
}
}
this.inOrderTraverse = function(callback){
inOrderTraverseNode(root,callback);
}
}
var nodes = [8,3,10,1,6,14,4,7,13];
var binaryTree = new BinaryTree();
nodes.forEach(function(key){
binaryTree.insert(key);
});
var callback = function(key){
console.log(key);
}
binaryTree.inOrderTraverse(callback);
</script>
</body>
</html>