Skip to main content
added 5 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

So I'm searching my binary search tree of VehicleVehicle objects and I'm just wondering whether there would be much point/is it, if it's best practice to search to see if a node has a left/right child first like so -:

return n.hasLeft() ? find(name, n.left()) : null;

Or just go with the foillowingfollowing, since it will return nullnull if it doesn't exist anyway:

return find(name, n.left());

Heere'sHere's the code for the whole method.:

protected Vehicle find(String name, Node n)
{
    if (n == null) return null;
    
    int order = name.compareTo(n.getVehicleName());
    
    if (order == 0)
        return n.getVehicle();  
    else if (order < 0)
        return n.hasLeft() ? find(name, n.left()) : null;       
    else 
        return n.hasRight() ? find(name, n.right()) : null;
}

Would checking first be more efficient since it saves an extra recursive call being made?

So I'm searching my binary search tree of Vehicle objects and I'm just wondering whether there would be much point/is it best practice to search if a node has a left/right child first like so -

return n.hasLeft() ? find(name, n.left()) : null;

Or just go with the foillowing, since it will return null if doesn't exist anyway

return find(name, n.left());

Heere's the code for the whole method.

protected Vehicle find(String name, Node n)
{
    if (n == null) return null;
    
    int order = name.compareTo(n.getVehicleName());
    
    if (order == 0)
        return n.getVehicle();  
    else if (order < 0)
        return n.hasLeft() ? find(name, n.left()) : null;       
    else 
        return n.hasRight() ? find(name, n.right()) : null;
}

Would checking first be more efficient since it saves an extra recursive call being made?

I'm searching my binary search tree of Vehicle objects and I'm just wondering whether there would be much point, if it's best practice to search to see if a node has a left/right child first like so:

return n.hasLeft() ? find(name, n.left()) : null;

Or just go with the following, since it will return null if it doesn't exist anyway:

return find(name, n.left());

Here's the code for the whole method:

protected Vehicle find(String name, Node n)
{
    if (n == null) return null;
    
    int order = name.compareTo(n.getVehicleName());
    
    if (order == 0)
        return n.getVehicle();  
    else if (order < 0)
        return n.hasLeft() ? find(name, n.left()) : null;       
    else 
        return n.hasRight() ? find(name, n.right()) : null;
}

Would checking first be more efficient since it saves an extra recursive call being made?

edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Check Searching for null child first?a binary tree for a vehicle with a given name

Source Link
screencut
  • 35
  • 1
  • 1
  • 4

Check for null child first?

So I'm searching my binary search tree of Vehicle objects and I'm just wondering whether there would be much point/is it best practice to search if a node has a left/right child first like so -

return n.hasLeft() ? find(name, n.left()) : null;

Or just go with the foillowing, since it will return null if doesn't exist anyway

return find(name, n.left());

Heere's the code for the whole method.

protected Vehicle find(String name, Node n)
{
    if (n == null) return null;
    
    int order = name.compareTo(n.getVehicleName());
    
    if (order == 0)
        return n.getVehicle();  
    else if (order < 0)
        return n.hasLeft() ? find(name, n.left()) : null;       
    else 
        return n.hasRight() ? find(name, n.right()) : null;
}

Would checking first be more efficient since it saves an extra recursive call being made?