JSFiddle - React, Tailwind, and code Playground

JavaScript

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) break;

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

BinarySearchTree.prototype.dfs = function(node) {
  if (node) {
    console.log(node.val);
    this.dfs(node.left);
    this.dfs(node.right);
  }
}

BinarySearchTree.prototype.depthFirstTraversal = function() {
  let current = this.root;
  this.dfs(current);
}


BinarySearchTree.prototype.sameFunction = function(node = null, isRoot = true) {
  let current = isRoot ? this.root : node;
  if (current) {
    console.log(current.val);
    this.sameFunction(current.left, false);
    this.sameFunction(current.right, false);
  }
}

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)
// tree.depthFirstTraversal();
tree.sameFunction();