4

I've written this code for a Binary Search Tree. For the most part I understand how it works when it comes to inserting nodes. What I don't quite get is the while(true) part.

I'm used to comparing some sort of value when using while loops.

For this code, how does it work?

Is it because I'm setting current and the while loop goes back and checks if that matches the value passed? is that how the while loop works with true?

class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

class BinarySearchTree {
  constructor() {
    this.root = null;
  }
}

BinarySearchTree.prototype.insert = function(value) {
  const newNode = new Node(value);

  if (this.root === null) {
    this.root = newNode;
  }

  let current = this.root;

  while(true) {
    if (value === current.val) return;

    if (value < current.val) {
      if (current.left === null) {
        current.left = newNode;
      }

      current = current.left;
    } else {
      if (current.right === null) {
        current.right = newNode;
      }

      current = current.right;
    }
  }
}


let tree = new BinarySearchTree();
tree.insert(10)
tree.insert(5)
tree.insert(13)
tree.insert(11)
tree.insert(2)
tree.insert(16)
tree.insert(7)

console.log(tree);
totalnoob
  • 2,521
  • 8
  • 35
  • 69

3 Answers3

3

The insert function will iterate, if there are nodes, until it finds a place to put the new node -- either current.right or current.left. After that it will set the current node to either left or right. This means in the next iteration it will check if current.value === value and if true the while(true) infinite loop will be exited which is done by the "return" you could also use "break" instead.

Read this (Does return stop a loop?) to understand how the "return;" works.

Edit: To clear it up, "return" and "break" have different functions but in your case both have the same effect.

sascha10000
  • 1,245
  • 1
  • 10
  • 22
  • so a return or break turns that true into false? – totalnoob Oct 25 '18 at 15:52
  • Totally not. return exists a function immediately and break exits the loop immediately. You don't affect the while(true) but inside you say "please exit the function". With break you would just leave the loop. You are basically controlling the flow of your program with those keywords. – sascha10000 Oct 25 '18 at 15:56
1

For this code, how does it work? there's nothing in the code other than it returning when the same number is inserted. how does it break out of the loop?

It returns.

A return will exit the current function.

That overrides a loop inside the function.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

The "while true" loop in javascript runs without any conditions until the break/return statement executes inside the loop

while loop only runs if the condition we write return a true, but here we pass the true directly without the condition

REFERENCE: https://www.freecodecamp.org/news/python-while-loop-tutorial/

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 05 '23 at 07:35