二进制搜索树语法

时间:2015-04-12 00:08:09

标签: javascript binary-tree binary-search-tree

对于JS中二叉搜索树的这部分实现,引用" this._root&#34 ;?的重要性是什么? (为什么他们不能说" 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

};

2 个答案:

答案 0 :(得分:0)

他们可能试图表明不应该在对象外部访问此变量(private在其他语言中)。

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

答案 1 :(得分:0)

我想这里有更完整的例子:https://github.com/nzakas/computer-science-in-javascript/blob/master/data-structures/binary-search-tree/binary-search-tree.js

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