Answers for "add nodes to binary tree js"

0

add nodes to binary tree js

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

  insert(data) {
    if (data < this.data && this.left) {
      this.left.insert(data);
    } else if (data < this.data) {
      this.left = new Node(data);
    }

    if (data > this.data && this.right) {
      this.right.insert(data);
    } else if (data > this.data) {
      this.right = new Node(data);
    }
  }
}
Posted by: Guest on October-07-2021

Code answers related to "add nodes to binary tree js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language