[英]insert() function doesn't add nodes to the binary search tree
我正在嘗試制作二叉搜索樹。 如果我從一個數組開始,我的 function 從該數組中創建一個二叉搜索樹(這里一切正常)。 像這樣的數組let a = [2,4,5,3,9,7,3,8,5];
這棵樹看起來像圖片。 我的問題是 insert() function。如果我從一個空數組開始並向它添加一個節點,它就可以工作。 但是,當我添加第二個節點時,第二個節點不會添加到我的樹中,並且我的樹顯示為只有 1 個節點(根節點)。 這里的片段:
const Node = (data, left = null, right = null) => {
return {data, left, right};
};
const Tree = array => {
const remDupsAndSort = array => {
const mergeSort = array => {
if(array.length <= 1) return array;
let leftArr = array.slice(0, array.length / 2);
let rightArr = array.slice(array.length / 2);
return merge(mergeSort(rightArr), mergeSort(leftArr))
};
const merge = (leftArr, rightArr) => {
let sorted = [];
while(leftArr.length && rightArr.length){
if(leftArr[0] < rightArr[0]){
sorted.push(leftArr.shift());
}else{
sorted.push(rightArr.shift());
}
};
return [...sorted, ...leftArr, ...rightArr]
};
return mergeSort([... new Set(array)])
};
array = remDupsAndSort(array);
const buildTree = (array, start, end) => {
if(start > end) return null;
let mid = Math.floor((start + end) / 2);
let node = Node(array[mid]);
node.left = buildTree(array, start, mid - 1);
node.right = buildTree(array, mid + 1, end);
return node;
};
const insert = value => {
if(!root) return root = Node(value);
current = root;
while(current){
if(value < current){
current = current.left
}else{
current = current.right
}
}
current = Node(value)
// if(!current){
// current = Node(value)
// // }else{
// if(value < current){
// current.left = insert(value, current.left)
// }else{
// current.right = insert(value, current.right)
// }
// }
return root
};
const prettyPrint = (node = root, prefix = '', isLeft = true) => {
if(node){
if (node.right !== null) {
prettyPrint(node.right, `${prefix}${isLeft ? '│ ' : ' '}`, false);
}
console.log(`${prefix}${isLeft ? '└── ' : '┌── '}${node.data}`);
if (node.left !== null) {
prettyPrint(node.left, `${prefix}${isLeft ? ' ' : '│ '}`, true);
}
}else{
console.log(node)
}
}
let root = buildTree(array, 0, array.length - 1);
return {root, prettyPrint, insert}
};
let a = [2,4,5,3,9,7,3,8,5];
let b = [];
let c = [1,2,4,5,6,7]
let f = Tree(a)
let d = Tree(b)
let e = Tree(c)
d.insert(4)
// d.insert(8) ---> doesn't work
// d.prettyPrint()
// f.insert(1) ---> doesn't work
f.prettyPrint()
// e.prettyPrint()
// console.log(d.root)
如果我運行d.prettyPrint()
我會得到└── 4
正如預期的那樣。 但是如果我在 8 沒有被添加到樹中之后運行d.insert(8)
並且代碼再次返回└── 4
。 更令人困惑的是,如果我console.log(d.root)
它返回 null 即使我的 prettyPrint function 返回└── 4
作為根。
顯然,我希望將節點添加到樹中。 在我的一次嘗試中,我嘗試編寫如下代碼:
const insert = (value, current = root) => {
if(!current){
current = Node(value)
}else{
if(value < current){
current.left = insert(value, current.left)
}else{
current.right = insert(value, current.right)
}
}
return current
};
即使我分配了current = root
代碼為d.insert(4)
返回了 null
您的insert
function 中存在這些問題:
current
被隱式定義為全局變量——這不會在嚴格模式下解析。 它應該聲明為局部變量,使用let
value < current
應該改成value < current.data
current
節點(在循環之后)不會改變樹。 它只是將 object 分配給該變量。 這樣的分配永遠不會改變樹,就像current = current.right
也不會改變樹一樣。 您需要將新節點分配給current
的父節點的left
或right
屬性。 所以這個賦值應該早一步發生。更正:
const insert = value => {
if(!root) return root = Node(value);
let current = root; // Define with `let`
while(current){
if(value < current.data){ // Compare with the data, not the node
// Mutate the parent node when inserting
if (!current.left) return current.left = Node(value);
current = current.left
}else{
if (!current.right) return current.right = Node(value);
current = current.right
}
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.