我正在創建一個程序來表示JavaScript中的二叉搜索樹。我想要的是一種創建兩個ptrs(左側,右側)爲空的公共樹節點的方法。這是我寫的代碼:
如何在javascript中創建自定義對象的常用常量實例?
var BST = function(data) {
if (data === null || data === undefined){
this.data = null;
this.left = null;
this.right = null;
}
else{
this.data = data;
this.left = new BST(null);
this.right = new BST(null);
}
};
BST.prototype.insert = function(data) {
if (this.data === null){
this.data = data;
this.left = new BST(null);
this.right = new BST(null);
}
else if (data < this.data)
this.left.insert(data);
else if (data > this.data)
this.right.insert(data);
};
BST.prototype.inOrder = function(func) {
if (this.data !== null) {
this.left.inOrder(func);
func(this.data);
this.right.inOrder(func);
}
};
在這裏,我想與分配一空節點的所有空指針(如if(data === null || data === undefined)
狀態定義)。但是對於每個空節點,我不得不創建一個代表相同數據的新節點。 有沒有辦法分配給空節點的公共實例?
我用一個空節點,而不是原因只是用
else{
this.data = data;
this.left = null;
this.right = null;
}
是在調用inOrder
方法,在到達一個節點與left or right = null
,它提供了TypeError
,因爲它試圖從運行null.inOrder(func);
,this.left
翻譯到null
。
解決方法是修改inOrder
函數,這將導致圍繞每個語句的許多條件,即不是非常優雅的實現。
我也可以在對象的原型之外定義inOrder
,並使它以樹爲參數,即inOder(tree,func)
,但我不想這樣做。
此外,作爲代碼的第二個改進,請考慮insert
方法;在null
情況:
if (this.data === null){
this.data = data;
this.left = new BST(null);
this.right = new BST(null);
}
,因爲我無論如何都要覆蓋每個條目,我想完全通過沿線的做一些重新分配這個節點到一個新的樹:
if (this.data === null)
this = new BST(data);
我意識到這對前者來說效率較低,但它仍然更加簡潔。那麼有什麼辦法可以這樣做嗎?
您不能指定'this',而是需要操作父節點。 – Bergi 2012-04-17 18:23:16