const TreeNode = require('../helpers/TreeNode')

const insert = (root, val) => {
  if (!root) {
    return new TreeNode(val)
  }

  let ins = false

  var traverse = function (node) {
    if (ins || !node) {
      return
    }

    if (!node.left && val < node.val) {
      node.left = new TreeNode(val)
      ins = true
    } else if (!node.right && val > node.val) {
      node.right = new TreeNode(val)
      ins = true
    }

    if (val < node.val) {
      traverse(node.left)
    }
    if (val > node.val) {
      traverse(node.right)
    }
  }

  traverse(root)

  return root
}

module.exports = insert
