Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda