[英]Error in Recursive Function Used For Inserting a Node in a Binary Tree in JavaScript
将节点插入树中后,我变得未定义,不知道为什么会发生这种情况。 这是发生的事情 - 在插入节点后,该函数应该返回 true,但它返回 undefined。 插入节点后,代码不会停止并返回到 if 检查条件,将 'current' 设置为 '7',这种情况一直发生,直到 'current' 为 '10'(根值),然后它最终返回insert() 函数返回未定义。 我唯一的问题是为什么它返回未定义,以及为什么在将节点插入所需位置后返回到 root。 有人可以告诉我吗? 我错过了一些非常小的东西吗?
顺便说一下,我插入的值是 8。 tree.insert(8);
class Node{
constructor(val){
this.val = val;
this.left = null;
this.right = null;
}
}
class BinarySearchTree{
constructor(){
this.root = null;
}
insert(val){
if(!this.root){
this.root = newNode;
return this;
}
let newNode = new Node(val);
if(val === this.root.val)return false;
function recursiveInsert(current,newNode){
if(newNode.val === current.val)return false;
if(newNode.val > current.val){
if(!current.right){
current.right = newNode;
return true;
}
recursiveInsert(current.right,newNode);
}
if(newNode.val<current.val){
if(!current.left){
current.left = newNode;
return true;
}
recursiveInsert(current.left, newNode);
}
}
return recursiveInsert(this.root,newNode);
}
}
let tree = new BinarySearchTree();
tree.root = new Node(10);
tree.root.left = new Node(7);
tree.root.right = new Node(15);
tree.root.left.right = new Node(9);
有这些问题:
在if(!this.root){
块中,您在newNode
收到值之前引用它,因此将newNode
的初始化移动到此if
语句之前。
在同一个块中,您return this
,但您写下要返回一个布尔值,因此将其更改为return true;
递归调用应该用于返回该调用返回的任何内容,因此在它们前面加上return
,就像您在进行初始调用的最后一行中所做的那样。
我还建议使用insert
方法构建初始树。 并添加一个迭代器,以便您可以按中序快速输出树。
请注意,有一个else if
条件始终为真,因此只需使用else
:
class Node { constructor(val) { this.val = val; this.left = null; this.right = null; } // Add this method for inorder traversal of the values * inorder() { if (this.left) yield * this.left.inorder(); yield this.val; if (this.right) yield * this.right.inorder(); } } class BinarySearchTree { constructor() { this.root = null; } insert(val) { let newNode = new Node(val); // First create the node if (!this.root) { this.root = newNode; return true; // you wanted a boolean } // This is not necessary: it already happens in the function below // if(val === this.root.val)return false; function recursiveInsert(current,newNode) { if (newNode.val === current.val) return false; if (newNode.val > current.val) { if (!current.right) { current.right = newNode; return true; } // Return the result return recursiveInsert(current.right, newNode); } else { // it is the only possibility left if (!current.left) { current.left = newNode; return true; } // Return the result return recursiveInsert(current.left, newNode); } } return recursiveInsert(this.root, newNode); } } let tree = new BinarySearchTree(); // Build the tree only with the insert-method tree.insert(10); tree.insert(7); tree.insert(15); tree.insert(9); tree.insert(8); // Add the value you wanted to test with tree.insert(15); // Try some duplicate console.log(...tree.root.inorder());
您可以通过将递归插入函数作为Node
类的方法来使代码更好看。 此外,丰富BinarySearchTree
构造函数,以便它可以从多个值开始,就像本机Array
构造函数一样。
class Node { constructor(val) { this.val = val; this.left = null; this.right = null; } * inorder() { if (this.left) yield * this.left.inorder(); yield this.val; if (this.right) yield * this.right.inorder(); } insert(val) { if (val === this.val) return false; if (val > this.val) { if (this.right) return this.right.insert(val); this.right = new Node(val); } else { if (this.left) return this.left.insert(val); this.left = new Node(val); } return true; } } class BinarySearchTree { constructor(...values) { this.root = null; for (let val of values) this.insert(val); } * inorder() { if (this.root) yield * this.root.inorder(); } insert(val) { if (this.root) return this.root.insert(val); this.root = new Node(val); return true; } } let tree = new BinarySearchTree(10, 7, 15, 9); tree.insert(8); tree.insert(15); console.log(...tree.inorder());
你应该返回 recursiveCall 否则你会得到 undefined 因为递归调用需要时间来执行并且函数不会等待它返回一些东西。
// ...code
return recursiveInsert(current.right,newNode);
// and
return recursiveInsert(current.left,newNode);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.