Getting a reference error "ReferenceError: insertLevelOrder is not defined" on function defined inside a class
Getting a reference error "ReferenceError: insertLevelOrder is not defined" on function defined inside a class
我试图使用 javaScript 实现一个完整的二叉树,但我得到了 ReferenceError 的错误:insertLevelOrder is not defined 这里是我的代码:
// given array in level order fashion
class Node {
constructor(data, left = null, right = null) {
this.data = data;
this.left = left;
this.right = right;
}
};
class Tree {
constructor() {
this.root = null;
}
// Function to insert nodes in level order
insertLevelOrder(arr, root, i)
{
// Base case for recursion
if (i < arr.length) {
var temp = new Node(arr[i]);
root = temp;
// insert left child
root.left = insertLevelOrder(arr, root.left, 2 * i + 1);
// insert right child
root.right = insertLevelOrder(arr, root.right, 2 * i + 2);
}
return root;
}
// Function to print tree nodes in InOrder fashion
inOrder(root)
{
if (root != null) {
inOrder(root.left);
console.log(root.data + " ");
inOrder(root.right);
}
}
}
var tree = new Tree();
var arr = new Array(1, 2, 3, 4, 5, 6, 6, 6, 6 );
tree.root = tree.insertLevelOrder(arr, tree.root, 0);
我在最后添加了一些代码来测试算法我不确定哪里错了
由于您试图在自身内部递归调用 insertLevelOrder
,因此您应该将其作为当前实例的方法调用(通过 this.insertLevelOrder
):
root.left = this.insertLevelOrder(arr, root.left, 2 * i + 1);
root.right = this.insertLevelOrder(arr, root.right, 2 * i + 2);
在 class 中你必须像 this.insertLevelOrder(...
一样使用 this
我已经删除了您在代码中的注释,并在您必须添加的地方添加了注释 this.
class Node {
constructor(data, left = null, right = null) {
this.data = data;
this.left = left;
this.right = right;
}
};
class Tree {
constructor() {
this.root = null;
}
insertLevelOrder(arr, root, i)
{
if (i < arr.length) {
var temp = new Node(arr[i]);
root = temp;
// you need to add this.
root.left = this.insertLevelOrder(arr, root.left, 2 * i + 1);
// you need to add this.
root.right = this.insertLevelOrder(arr, root.right, 2 * i + 2);
}
return root;
}
inOrder(root)
{
if (root != null) {
this.inOrder(root.left); // you need to add this.
console.log(root.data + " ");
this.inOrder(root.right); // you need to add this.
}
}
}
var tree = new Tree();
var arr = new Array(1, 2, 3, 4, 5, 6, 6, 6, 6 );
tree.root = tree.insertLevelOrder(arr, tree.root, 0);
我试图使用 javaScript 实现一个完整的二叉树,但我得到了 ReferenceError 的错误:insertLevelOrder is not defined 这里是我的代码:
// given array in level order fashion
class Node {
constructor(data, left = null, right = null) {
this.data = data;
this.left = left;
this.right = right;
}
};
class Tree {
constructor() {
this.root = null;
}
// Function to insert nodes in level order
insertLevelOrder(arr, root, i)
{
// Base case for recursion
if (i < arr.length) {
var temp = new Node(arr[i]);
root = temp;
// insert left child
root.left = insertLevelOrder(arr, root.left, 2 * i + 1);
// insert right child
root.right = insertLevelOrder(arr, root.right, 2 * i + 2);
}
return root;
}
// Function to print tree nodes in InOrder fashion
inOrder(root)
{
if (root != null) {
inOrder(root.left);
console.log(root.data + " ");
inOrder(root.right);
}
}
}
var tree = new Tree();
var arr = new Array(1, 2, 3, 4, 5, 6, 6, 6, 6 );
tree.root = tree.insertLevelOrder(arr, tree.root, 0);
我在最后添加了一些代码来测试算法我不确定哪里错了
由于您试图在自身内部递归调用 insertLevelOrder
,因此您应该将其作为当前实例的方法调用(通过 this.insertLevelOrder
):
root.left = this.insertLevelOrder(arr, root.left, 2 * i + 1);
root.right = this.insertLevelOrder(arr, root.right, 2 * i + 2);
在 class 中你必须像 this.insertLevelOrder(...
this
我已经删除了您在代码中的注释,并在您必须添加的地方添加了注释 this.
class Node {
constructor(data, left = null, right = null) {
this.data = data;
this.left = left;
this.right = right;
}
};
class Tree {
constructor() {
this.root = null;
}
insertLevelOrder(arr, root, i)
{
if (i < arr.length) {
var temp = new Node(arr[i]);
root = temp;
// you need to add this.
root.left = this.insertLevelOrder(arr, root.left, 2 * i + 1);
// you need to add this.
root.right = this.insertLevelOrder(arr, root.right, 2 * i + 2);
}
return root;
}
inOrder(root)
{
if (root != null) {
this.inOrder(root.left); // you need to add this.
console.log(root.data + " ");
this.inOrder(root.right); // you need to add this.
}
}
}
var tree = new Tree();
var arr = new Array(1, 2, 3, 4, 5, 6, 6, 6, 6 );
tree.root = tree.insertLevelOrder(arr, tree.root, 0);