尝试为 BST 制作 'balanceFactor' 方法。无法弄清楚如何计算平衡因子...传入错误的参数?
Trying to work and make a 'balanceFactor' method for a BST. Can't figure out how to calculate the balance factor... passing in the wrong argument?
我认为我使用 'this' 不正确?我不确定如何进行。这是我正在使用的方法。
function BinarySearchTree(value) {
this.value = value;
this.right = null;
this.left = null;
}
BinarySearchTree.prototype.add = function(value) {
node = this;
if (value < this.value) {
if (this.left) this.left.add(value);
else this.left = new BinarySearchTree(value);
}
if (value > this.value) {
if (this.right) this.right.add(value);
else this.right = new BinarySearchTree(value);
}
}
BinarySearchTree.prototype.leftHeight = function(root = this.value) {
if (this.value == null) {
return 0;
}
let queue = [];
queue.push(this);
let leftHeight = 0;
while (1 == 1) {
let nodeCount = queue.length;
if (nodeCount == 0) {
return leftHeight - 1;
}
leftHeight++;
while (nodeCount > 0) {
let newNode = queue.shift();
if (newNode.left != null) {
queue.push(newNode.left);
}
nodeCount--;
}
}
}
BinarySearchTree.prototype.rightHeight = function(root = this.value) {
if (this.value == null) {
return 0;
}
let queue = [];
queue.push(this);
let rightHeight = 0;
while (1 == 1) {
let nodeCount = queue.length;
if (nodeCount == 0) {
return rightHeight - 1;
}
rightHeight++;
while (nodeCount > 0) {
let newNode = queue.shift();
if (newNode.right != null) {
queue.push(newNode.right);
}
nodeCount--;
}
}
}
BinarySearchTree.prototype.balanceFactor = function(this) {
console.log(this.leftHeight)
return this.leftHeight - this.rightHeight;
}
这是我的输入和测试 运行...
let binarySearchTree = new BinarySearchTree;
binarySearchTree = new BinarySearchTree(5);
binarySearchTree.left = new BinarySearchTree(2)
binarySearchTree.right = new BinarySearchTree(3);
binarySearchTree.right.right = new BinarySearchTree(1);
console.log(binarySearchTree.rightHeight());
console.log(binarySearchTree.leftHeight());
console.log(binarySearchTree.balanceFactor())
最后 3 个控制台记录输出 2、1 和 NaN。所以我的左右高度方法工作正常!谁能告诉我为什么我的 'balanceFactor' 方法不起作用?
谢谢,节日快乐!
leftHeight
和 rightHeight
是函数所以你需要这样调用它们
BinarySearchTree.prototype.balanceFactor = function () {
console.log(this.leftHeight())
return this.leftHeight() - this.rightHeight();
}
我认为我使用 'this' 不正确?我不确定如何进行。这是我正在使用的方法。
function BinarySearchTree(value) {
this.value = value;
this.right = null;
this.left = null;
}
BinarySearchTree.prototype.add = function(value) {
node = this;
if (value < this.value) {
if (this.left) this.left.add(value);
else this.left = new BinarySearchTree(value);
}
if (value > this.value) {
if (this.right) this.right.add(value);
else this.right = new BinarySearchTree(value);
}
}
BinarySearchTree.prototype.leftHeight = function(root = this.value) {
if (this.value == null) {
return 0;
}
let queue = [];
queue.push(this);
let leftHeight = 0;
while (1 == 1) {
let nodeCount = queue.length;
if (nodeCount == 0) {
return leftHeight - 1;
}
leftHeight++;
while (nodeCount > 0) {
let newNode = queue.shift();
if (newNode.left != null) {
queue.push(newNode.left);
}
nodeCount--;
}
}
}
BinarySearchTree.prototype.rightHeight = function(root = this.value) {
if (this.value == null) {
return 0;
}
let queue = [];
queue.push(this);
let rightHeight = 0;
while (1 == 1) {
let nodeCount = queue.length;
if (nodeCount == 0) {
return rightHeight - 1;
}
rightHeight++;
while (nodeCount > 0) {
let newNode = queue.shift();
if (newNode.right != null) {
queue.push(newNode.right);
}
nodeCount--;
}
}
}
BinarySearchTree.prototype.balanceFactor = function(this) {
console.log(this.leftHeight)
return this.leftHeight - this.rightHeight;
}
这是我的输入和测试 运行...
let binarySearchTree = new BinarySearchTree;
binarySearchTree = new BinarySearchTree(5);
binarySearchTree.left = new BinarySearchTree(2)
binarySearchTree.right = new BinarySearchTree(3);
binarySearchTree.right.right = new BinarySearchTree(1);
console.log(binarySearchTree.rightHeight());
console.log(binarySearchTree.leftHeight());
console.log(binarySearchTree.balanceFactor())
最后 3 个控制台记录输出 2、1 和 NaN。所以我的左右高度方法工作正常!谁能告诉我为什么我的 'balanceFactor' 方法不起作用?
谢谢,节日快乐!
leftHeight
和 rightHeight
是函数所以你需要这样调用它们
BinarySearchTree.prototype.balanceFactor = function () {
console.log(this.leftHeight())
return this.leftHeight() - this.rightHeight();
}