The assignment I'm working on takes a Binary Search tree of football teams and basically places them in sequence depending on the results of a tournament where each matchup is determined by an external function (did_x_beat_y). My IDE is telling me that I'm running into a SIGPIPE error and that my function is hanging for too long at times. Is there any way I can optimize this to improve the runtime?
// Return a BST containing a valid ordering of n teams
Node *order_n_teams(int n)
{
Node *T = nullptr;
// start by inserting the first team
T = insert_random(T, 0, 0);
// now insert the other teams...
for (int i=1; i<n; i++)
{
// insert team i so the sequence encoded by the BST remains valid
if (did_x_beat_y(i, select(T,0)->key)) // can we insert at beginning?
{
T = insert_random(T, i, 0);
}
else if (did_x_beat_y(select(T,T->size-1)->key, i)) // can we insert at end?
{
T = insert_random(T, i, T->size);
}
//if team i doesn't go in beginning or end
else
{
int start = 0;
int last = T->size - 1;
while (start + 1 != last)
{
int mid = (start + last) / 2;
if (did_x_beat_y( i, select( T, mid)->key) )
{
last = mid;
}
else
{
start = mid;
}
}
T = insert(T, i, last);
}
}
return T;
}
Aucun commentaire:
Enregistrer un commentaire