2017-04-06 38 views
0

所以我嘗試使用僞古典繼承創建二叉搜索樹。它接受一個數組,對它進行排序,使用中間值作爲起點,並將數組中剩餘的值插入到BST中。我想我正在盡我所能利用函數式編程(糾正我,如果我錯了請)通過使用可重用的方法,也因爲BST插入方法需要遞歸。嘗試使用僞古典繼承創建BST時遞歸函數的問題

我已經指出了代碼出錯的地方。我相信它需要3作爲初始值,我也相信1(數組中的下一個值)成功插入,但我相信數字2是錯誤發生的地方,當它說「TypeError:this.left.insert是不是功能「。任何人都可以指出我做錯了什麼?爲什麼插入方法不會爲this.left調用它自己?

var NoDuplicatesBST = function(array) { 
     var tempArr = arguments[0].sort(function(a, b) { 
     return a-b; 
     }); 
     var middle = Math.floor(((tempArr.length - 1)/2)); 
     var sliced = tempArr.splice(middle, 1); 

     this.createBST(sliced[0]); 

     // now insert the rest of tempArr into the BST 
     for (var i = 0; i < tempArr.length; i++) { 
     this.insert(tempArr[i]); 
     } 
    }; 

    NoDuplicatesBST.prototype.createBST = function(number) { 
     this.value = number; 
     this.left = null; 
     this.right = null; 
    }; 

    NoDuplicatesBST.prototype.insert = function(number) { 
     if (number < this.value) { 
     if (this.left === null) { 
      this.left = new this.createBST(number); 
     } else { 
// ------------CODE BELOW DOES NOT WORK!, LINED 77 ALSO PROBABLY. TypeError: this.left.insert is not a function---------------------- 
      this.left.insert(number); 
     } 
     } else if (number > this.value) { 
     if (this.right === null) { 
      this.right = new this.createBST(number); 
     } else { 
      this.right.insert(number); 
     } 
     } else { 
     // Do nothing 
     } 
    }; 

    var testBST = new NoDuplicatesBST([2,3,4,5,1]); 

    console.log("The testBST:", testBST); 
+0

你使用'this.createBST(number)'作爲構造函數,這不是一個構造函數,爲你的節點創建一個單獨的類 – Oskar

回答

0

這不是功能性的方式寫的,看看,並嘗試去通過這個教程學習更多關於函數式編程的JS:http://reactivex.io/learnrx/

而到了原來的問題,爲什麼你看到"TypeError: this.left.insert is not a function"。檢查我的評論在您的代碼:

var NoDuplicatesBST = function(arr) { 
 
    var middle, left = [], center, right = []; 
 
    if (!Array.isArray(arr) || arr.length == 0) { 
 
    return this; 
 
    } 
 
    if (arr.length == 1) { 
 
    center = arr[0]; 
 
    } else { 
 
    middle = Math.floor((arr.length/2)); 
 
    center = arr[middle]; 
 
    left = arr.slice(0, middle); 
 
    right = arr.slice(middle + 1, arr.length); 
 
    console.log('left:', left); 
 
    console.log('middle:', center); 
 
    console.log('right:', right); 
 
    } 
 

 
    this.createBST(center); 
 

 
    // now insert left and right parts to BST 
 
    if (left.length > 0) { 
 
    this.insert(left); 
 
    } 
 
    if (right.length > 0) { 
 
    this.insert(right); 
 
    } 
 
}; 
 

 
NoDuplicatesBST.prototype.createBST = function(number) { 
 
    this.value = number; 
 
    this.left = null; 
 
    this.right = null; 
 
}; 
 

 
NoDuplicatesBST.prototype.insert = function(arr) { 
 
    if (arr.length > 0) { 
 
    //array is sorted and we took the middle element, so we can compare just the first element 
 
    if (arr[0] < this.value) { 
 
     /** Here you use createBST as a constructor, it creates a new element, 
 
     with left and right values, but you break the prototypal inheritance chain, 
 
     that's why you don't have access to the insert function */ 
 
     // this.left = new this.createBST(number); 
 
     // it's better to pass the part of the array to build the tree further 
 
     this.left = new NoDuplicatesBST(arr); 
 
    } else { 
 
     this.right = new NoDuplicatesBST(arr); //the same as above 
 
    } 
 
    } 
 
}; 
 

 
var arr = [2, 3, 4, 5, 1]; 
 
var tempArr = arr.reduce(function (noDuplicatesArr, current) { //remove duplicates 
 
    if (noDuplicatesArr.indexOf(current) === -1) { 
 
    noDuplicatesArr.push(current); 
 
    } 
 

 
    return noDuplicatesArr; 
 
}, []).sort(function(a, b) { 
 
    return a - b; 
 
}); 
 
var testBST = new NoDuplicatesBST(tempArr); 
 
console.log("The testBST:", testBST);

對於原型鏈繼承檢查:https://developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

BTW。我改變了你的代碼來接受數組,而不是數字,現在構建一個BST

+0

如果你console.log(this)在this.left = new this.createBST(number)之後,它看起來像這樣: {value:3, left:{value:1,left:null,right:null}, right:null} 這不是一個BST嗎? –

+0

@JeffYoo請再次閱讀我的答案,我沒有說這是或不是BST,你用'new this.createBST'創建你的節點,這意味着你不能訪問'insert'方法。你打破了那裏的原型繼承鏈。 'new this.createBST'返回的JS不是NoDuplicatesBST類的實例。 – Oskar

+0

有沒有辦法可以解決這個問題呢? –