Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
Binary Search Tree & "Cannot read properties of null"

I have a generic binary search tree that uses a comparator function to evaluate which value is bigger. This is my remove method:

remove(data) {
        const removeHelper = (node, data) => {
            if (!node) return null;
            if (this.#comparator(data, node.data) === 0) {
                if (!node.left && !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;
    }

I keep getting the error: TypeError: Cannot read properties of null (reading 'left') reffering to this line: (while (!tmp.left) )

How is my remove method not supposed to read property types of null if thats what the entire method functions off of

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!