繁体   English   中英

二进制搜索树语法

[英]Binary Search Tree Syntax

对于JS中二进制搜索树实现的这一部分,引用“ this._root”的意义是什么? (为什么他们不能说“ this.root”)? 有关此链接,请访问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

};

他们可能试图指示不应在对象外部(其他语言的private变量)访问此变量。

Python有一个严格的约定,即使用以下划线字符开头的名称来表示此字段或方法是私有的。 作者可能正在尝试将相同的约定应用于javascript。

我想更完整的示例位于此处: https//github.com/nzakas/computer-science-in-javascript/blob/master/data-structures/binary-search-tree/binary-search-tree.js

关于this._root我认为这只是作者的决定,没有任何特殊含义。

暂无
暂无

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

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