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