1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
void MaxBalanced(Tree *t, Tree *maxBal, int *maxBal_height)
{
int t_height;
if (t == NULL)
*maxBal_height += 0;
if (IsBalanced(t) == 1){
t_height = height(t);
if (t_height >= *maxBal_height){
maxBal = t;
*maxBal_height = t_height;
}
}
else{
MaxBalanced(t->left, maxBal, maxBal_height);
MaxBalanced(t->right, maxBal, maxBal_height);
}
}
int main()
{
int i;
Tree* t = NULL;
int j[20] = { 993, 591, 2, 4, 395, 97, 446, 38, 279, 587, 28, 50, 265, 502, 114, 493, 808, 716, 897, 987 };
for (i = 0; i < 20; i++)
t = InsertBST(t, j[i]);
DisplayTree(t);
Tree* maxBal = NULL;
int maxBal_height = 0;
MaxBalanced(t, maxBal, &maxBal_height);
DisplayTree(maxBal);
return 0;
}
|