简体   繁体   English

JavaScript-二进制搜索树-inOrderTraverse()函数将“未定义”日志记录到控制台窗口吗?

[英]JavaScript - Binary Search Tree - inOrderTraverse() function log “undefined” to console window?

I'm implementing an AVL Tree using JavaScript, and I'm currently working with the inOrderTraverse() function. 我正在使用JavaScript实现AVL树,并且目前正在使用inOrderTraverse()函数。 The problem is that: when i want to log the values of the nodes to the console window, it shows "undefined" instead. 问题是:当我要将节点的值记录到控制台窗口时,它显示为“未定义”。

I've referenced from https://gist.github.com/TheIronDeveloper/6604713 and i think there's not much difference between my code and the code i've referenced from. 我已经从https://gist.github.com/TheIronDeveloper/6604713引用了,我认为我的代码和我从中引用的代码之间并没有太大区别。

Here is my JS code for implementing AVL Tree: http://jsfiddle.net/nrU4T/ or https://dl.dropboxusercontent.com/u/178536659/avl%20tree%20javascript/avlTree.js 这是我用于实现AVL树的JS代码: http : //jsfiddle.net/nrU4T/https://dl.dropboxusercontent.com/u/178536659/avl%20tree%20javascript/avlTree.js

The result i got is here: (i run this code on Chrome) 我得到的结果是在这里:(我在Chrome上运行此代码)

Node 12 added.     avlTree.js:205
Nodes count = 1    avlTree.js:206
undefined          avlTree.js:64
                   avlTree.js:68
Node 18 added.     avlTree.js:264
Nodes count = 2    avlTree.js:265
undefined          avlTree.js:64
undefined          avlTree.js:64
                   avlTree.js:68
                   avlTree.js:68
Node 20 added.     avlTree.js:264
Nodes count = 3    avlTree.js:265
undefined          avlTree.js:64
undefined          avlTree.js:64
undefined          avlTree.js:64

Please help me to fix this error ... Any comments would be very appreciated. 请帮助我解决此错误...任何评论将不胜感激。 Thank you guys a lot. 非常感谢你们。

Its your bug: 它是你的错误:

in function inOrderTraverse() you have to print this.data and not this.value . inOrderTraverse()函数中,您必须打印this.data而不是this.value

inOrderTraverse: function() {
    if(this.left !== null) {
        this.left.inOrderTraverse();
    }
    console.log(this.data); // <-- here - not this.value
    if(this.right !== null) {
        this.right.inOrderTraverse();
    }
    console.log("\n");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM