[英]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.