在JavaScript中查找节点的高度
在我的JavaScript代码中,我试图在在JavaScript中查找节点的高度,javascript,binary-search-tree,Javascript,Binary Search Tree,在我的JavaScript代码中,我试图在二叉搜索树中找到给定节点的高度。 这是我的密码 class BinarySearchTree2 { constructor() { this.root = null; } findHeight(node = this.root,nodeData,level = 1) { let root = node; if(root === null) return n
二叉搜索树中找到给定节点的高度。
这是我的密码
class BinarySearchTree2 {
constructor() {
this.root = null;
}
findHeight(node = this.root,nodeData,level = 1) {
let root = node;
if(root === null)
return null;
if(root.data === nodeData)
return level;
let foundLevel = 0;
if(nodeData < root.data) {
foundLevel = findHeight(root.left,nodeData,level + 1);
}
// If you have found it on the left subtree, that's it, return
if(foundLevel !== 0)
return foundLevel;
foundLevel = findHeight(root.left,nodeData,level + 1);
return foundLevel;
}
}
它抛出错误。没有定义说findHeight
我做错了什么?这行应该是
findHeight(node=this.root,nodeData,level=1){
像这样
findHeight = (node = this.root,nodeData,level = 1) => {
或者您需要将函数绑定到构造函数中的CLA。这一行应该是
findHeight(node=this.root,nodeData,level=1){
像这样
findHeight = (node = this.root,nodeData,level = 1) => {
或者您需要将函数绑定到构造函数中的CLA。如果您想在其内部调用方法,您仍然需要使用
此
,因为您尝试调用的方法实际上附加到对象本身。因此
class BinarySearchTree2 {
constructor() {
this.root = null;
}
findHeight(node = this.root,nodeData,level = 1) {
let root = node;
if(root === null)
return null;
if(root.data === nodeData)
return level;
let foundLevel = 0;
if(nodeData < root.data) {
// change here
foundLevel = this.findHeight(root.left,nodeData,level + 1);
}
// If you have found it on the left subtree, that's it, return
if(foundLevel !== 0)
return foundLevel;
// change here
foundLevel = this.findHeight(root.left,nodeData,level + 1);
return foundLevel;
}
}
类二进制搜索树2{
构造函数(){
this.root=null;
}
findHeight(节点=this.root,节点数据,级别=1){
设根=节点;
如果(根===null)
返回null;
if(root.data==节点数据)
回报水平;
设foundLevel=0;
if(节点数据<根数据){
//在这里换车
foundLevel=this.findHeight(root.left,nodeData,level+1);
}
//如果您在左子树上找到了它,则返回
如果(foundLevel!==0)
回报水平;
//在这里换车
foundLevel=this.findHeight(root.left,nodeData,level+1);
回报水平;
}
}
将按预期工作如果您要在其内部调用方法,您仍然需要使用
此
,因为您尝试调用的方法实际上附加到对象本身。因此
class BinarySearchTree2 {
constructor() {
this.root = null;
}
findHeight(node = this.root,nodeData,level = 1) {
let root = node;
if(root === null)
return null;
if(root.data === nodeData)
return level;
let foundLevel = 0;
if(nodeData < root.data) {
// change here
foundLevel = this.findHeight(root.left,nodeData,level + 1);
}
// If you have found it on the left subtree, that's it, return
if(foundLevel !== 0)
return foundLevel;
// change here
foundLevel = this.findHeight(root.left,nodeData,level + 1);
return foundLevel;
}
}
类二进制搜索树2{
构造函数(){
this.root=null;
}
findHeight(节点=this.root,节点数据,级别=1){
设根=节点;
如果(根===null)
返回null;
if(root.data==节点数据)
回报水平;
设foundLevel=0;
if(节点数据<根数据){
//在这里换车
foundLevel=this.findHeight(root.left,nodeData,level+1);
}
//如果您在左子树上找到了它,则返回
如果(foundLevel!==0)
回报水平;
//在这里换车
foundLevel=this.findHeight(root.left,nodeData,level+1);
回报水平;
}
}
将按预期工作您可能需要调用
this.findHeight()
instead您可以添加完整的类代码吗?我们需要insert
方法。您可能需要调用this.findHeight()
instead您可以添加完整的类代码吗?我们需要insert
方法。