Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

310
Visualizações
Binary Tree Search return highest number

I'm trying to find the car with the highest number of prizes. I visit each node of the tree and I get the array size and compare it with each node. However I can't seem to return the node with the highest value. Could anybody look at my code and point what's wrong with it? Also I'm not sure if I'm returning it correctly from the recursive method mostPrizes.

My tree

public class Car{

private String name;
private double key;
private ArrayList<Prize> Prizes;    
public Car left;
public Car right;

public Car(String name)
{   
this.name = name;
}

public double getKey() {
    return key;
}

public void setKey(double key) {
    this.key = key;
}

public ArrayList<Prize> getPrizes() {
    return Prizes;
}


public void setPrizes(ArrayList<Prize> Prizes) {
    this.Prizes = Prizes;
}}


   public class main{

   public Car root;
   public void findWiner()
   { 
     Car winner;
     Car a = root;
     Car b = root;
     winner = mostPrizes(a,b)






   public Car mostPrizes (Car car, Car best)
{
        Car temp = best;
        //visit left node
        if (car.left != null){
         mostPrizes(car.left,best);             
        }           
        //check how many prizes
        if(car.getPrizes().size() < best.getPrizes().size()){
           best = car;                                     
        }
        //visit right nodes
        if (car.right != null ){
            mostPrizes(car.right,best);             
        }
        return best;
  }}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The mostPrzes method of yours is not right, because you are not assigning best of left and right subtrees to the current best, change it to the code below

    public Car mostPrizes (Car car)
    {
        if(car== null){
            return null;
        }
        Car leftMax= mostPrizes(car.left);
        Car rightMax= mostPrizes(car.right);

        if(leftMax!=null){
            if(car.getPrizes().size()>leftMax.getPrizes().size()){
                return node;
            }
            else{
                return leftMax;
            }
        }
        if(rightMax!=null){
            if(car.getPrizes().size()>rightMax.getPrizes().size()){
                return node;
            }
            else{
               return rightMax;
            }
        }
        return car;
    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda