简体   繁体   中英

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. 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.

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

The result i got is here: (i run this code on 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: 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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