title: Binary Search Tree & "Cannot read properties of null"
body: <p>I have a generic binary search tree that uses a comparator function to evaluate which value is bigger. This is my remove method:</p>
  <pre><code>remove(data) {
          const removeHelper = (node, data) =&gt; {
              if (!node) return null;
              if (this.#comparator(data, node.data) === 0) {
                  if (!node.left &amp;&amp; !node.right) return null;
                  if (!node.left) return node.right;
                  if (!node.right) return node.left;
                  let tmp = node.right;
                  while (!tmp.left) {
                      tmp = tmp.left;
                  }
                  node.data = tmp.data;
                  node.right = removeHelper(node.right, tmp.data);
              } else if (this.#comparator(data, node.data) === -1) {
                  node.left = removeHelper(node.left, data);
                  return node;
              } else {
                  node.right = removeHelper(node.right, data);
                  return node;
              }
          };
          this.root = removeHelper(this.root, data);
          return data;
      }
  </code></pre>
  <p>I keep getting the error: <code>TypeError: Cannot read properties of null (reading 'left')</code> reffering to this line: (while (!tmp.left) )</p>
  <p>How is my remove method not supposed to read property types of null if thats what the entire method functions off of</p>