二叉搜索树语法
Binary Search Tree Syntax
这部分二叉搜索树在JS中的实现,参考"this._root"有什么意义? (为什么他们不能说 "this.root")? link 可在 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。
关于this._root
- 我认为这只是作者的决定,没有任何特殊意义。
这部分二叉搜索树在JS中的实现,参考"this._root"有什么意义? (为什么他们不能说 "this.root")? link 可在 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。
关于this._root
- 我认为这只是作者的决定,没有任何特殊意义。