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

188
Vistas
Finding a value in a Binary Search Tree

I am making the code of a function to find a value passed by parameter in a BinarySearchTree but it is not working for me and I don't know why.if the price is inside the tree I have to return true, if the price passed by parameter is less than or equal to 0 I have to return "error" and if it is not found I must return false this is my code.

BinarySearchTree.prototype.searchPrice = function (price) {

    if (price <= 0) return "Error"  
    
    if(this.value === price) return true;

    if(this.value < price) return this.right.searchPrice(price);

    if(this.value > price) return this.left.searchPrice(price);

    if(this.value === null) return false;
};
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

The issue might be that you have null check for this.value at the end which causes the if(this.value < price) always to be true when this.value is null

Try to move the line if(this.value === null) return false; at the top and see if it works

UPDATE: Also you need to add null checks for the this.left and this.right

BinarySearchTree.prototype.searchPrice = function (price) {

    if (price <= 0) return "Error"  

// move this line at the top like this
    if(this.value === null) return false;
    
    if(this.value === price) return true;

    if(this.value < price) {
        if(!this.right) {
            return false;
        }
        return this.right.searchPrice(price);
    }

    if(this.value > price){
        if(!this.left) {
            return false;
        }
        return this.left.searchPrice(price);
    }

};
about 4 years ago · Santiago Gelvez Denunciar
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