Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

I believe I am using ‘this’ incorrectly? I am unsure of how to proceed. Here are the methods I am using.

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;
  }

And here is the input and tests I am running…

  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())

The last 3 console logs output 2, 1, and NaN. So my left and right height methods are working correctly! Can anyone tell me why my ‘balanceFactor’ method isn’t working?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks and happy holidays!

>Solution :

leftHeight and rightHeight are functions so you need to call them like this

  BinarySearchTree.prototype.balanceFactor = function () {
    console.log(this.leftHeight())
    return this.leftHeight() - this.rightHeight();
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading