简体   繁体   中英

Binary Search Tree Syntax

For this portion of an implementation of a binary search tree in JS, what is the significance of referring to "this._root"? (why can't they say "this.root")? The link for this is available at http://www.nczonline.net/blog/2009/06/16/computer-science-in-javascript-binary-search-tree-part-2/

BinarySearchTree.prototype = {

    //more code here

    remove: function(value){

        var found       = false,
            parent      = null,
            current     = this._root,
            childCount,
            replacement,
            replacementParent;

        //make sure there's a node to search
        while(!found && current){

            //if the value is less than the current node's, go left
            if (value < current.value){
                parent = current;
                current = current.left;

            //if the value is greater than the current node's, go right
            } else if (value > current.value){
                parent = current;
                current = current.right;

            //values are equal, found it!
            } else {
                found = true;
            }
        }

        //only proceed if the node was found
        if (found){
            //continue
        }

    },

    //more code here

};

They may be trying to indicate that this variable should not be accessed outside of the object ( private in other languages).

Python has a strong convention of using names starting with underscore character to indicate that this field or method is private. The author may be trying to apply the same convention to javascript.

I suppose that more complete example is located here: https://github.com/nzakas/computer-science-in-javascript/blob/master/data-structures/binary-search-tree/binary-search-tree.js

Regarding this._root - I think it's author's decision only, without any special meaning.

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