בגרות 2019

פתרון ב c#

public static bool Order(BinNode <Range>  t)

{

        if (t==null)

                return true;

        if( t.HasLeft() && !(t.GetValue().GetLow() ==  t.GetLeft().GetValue().GetLow()&& t.GetValue().GetHigh() >=  t.GetLeft().GetValue().GetHigh()) ))

        return false;

        if( t.HasRight() && !(t.GetValue().GetHigh() ==  t.GetRight().GetValue().GetHigh()&& t.GetValue().GetLow() >=  t.GetRight().GetValue().GetLow()) ))

        return false;

if(t.HasLeft() && t.HasRight()   && !(   t.GetLeft().getHigh < t.GetRight().GetLow() ))

        return false;

return Order(t.GetLeft())  &&  Order(t.GetRight());

}

פתרון נוסף

public static boolean order(BinNode<Range> t){

if(t==null)

return true;

int low = t.getValue().getLow();

int high = t.getValue().getHigh();

boolean first = false;

if(t.hasLeft() &&  low == t.getLeft().getValue().getLow() &&

high>= t.getLeft().getValue().getHigh())

first = true;

boolean second = false;

if(t.hasRight() && high == t.getRight().getValue().getHigh() &&

low <=t.getRight().getValue().getLow())

second = true;

boolean third = false;

if(t.hasLeft() && t.hasRight() && t.getLeft().getValue().getHigh()<t.getRight().getValue().getLow())

third = true;

if(!t.hasLeft()){

first= true;

third = true;

}

if(!t.hasRight()){

second= true;

third = true;

}

return first && second && third && order(t.getLeft()) &&order(t.getRight())  ;

}