If anyone wants to know what the code looks like (note, I didn't include main because it's totally irrelevant, along with the readFile function, I get the correct pointer returned):
Code:
int Height(Position P)
{
if(P == NULL)
return -1;
else
return P->Height;
}
AVLTree Insert(ElementType X, AVLTree T)
{
if(T == NULL)
{
T = malloc(sizeof(struct AVLNode));
if(T==NULL)
{
printf("Fatal Error...Exiting!\n");
exit(1);
}
else
{
T->Element = X;
T->Height = 0;
T->Left = NULL;
T->Right = NULL;
}
}
else if(X < T->Element)
{
T->Left = Insert(X, T->Left);
if(Height(T->Left) - Height(T->Right) == 2)
{
if(X < T->Left->Element)
T = SingleRotateWithLeft(T);
else
T = DoubleRotateWithLeft(T);
}
}
else if(X > T->Element)
{
T->Right = Insert(X, T->Right);
if(Height(T->Right) - Height(T->Left) == 2)
{
if(X > T->Right->Element)
T = SingleRotateWithRight(T);
else
T = DoubleRotateWithRight(T);
}
}
T->Height = (Max(Height(T->Left), Height(T->Right)) + 1);
return T;
}
Position SingleRotateWithLeft(Position K2)
{
Position K1;
K1 = K2->Left;
K2->Left = K1->Right;
K1->Right = K2;
K2->Height = (Max(Height(K2->Left), Height(K2->Right)) + 1);
K1->Height = (Max(Height(K1->Left), (K2->Height)) + 1);
return K1;
}
Position SingleRotateWithRight(Position K1)
{
Position K2;
K2 = K1->Right;
K1->Right = K2->Left;
K2->Left = K1;
K1->Height = (Max(Height(K1->Left), Height(K1->Right)) + 1);
K2->Height = (Max(Height(K2->Right), (K1->Height)) + 1);
return K2;
}
Position DoubleRotateWithLeft(Position K3)
{
K3->Left = SingleRotateWithRight(K3->Left);
return SingleRotateWithLeft(K3);
}
Position DoubleRotateWithRight(Position K1)
{
K1->Right = SingleRotateWithLeft(K1->Right);
return SingleRotateWithRight(K1);
}
int Max(int x, int y)
{
if(x < y)
return y;
return x;
}