写的实现avl平衡树的代码,但程序返回3221225477,不知道哪里有错误
代码如下
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct treenode)
typedef struct treenode *ptrtree;
typedef ptrtree position;
struct treenode
{
int x;
ptrtree left;
ptrtree right;
int height;
};
ptrtree makeempty(ptrtree t);
position find(char x,ptrtree t);
position findmin(ptrtree t);
position findmax(ptrtree t);
ptrtree insert(int x,ptrtree t);
ptrtree Delete(int x,ptrtree t);
int height(ptrtree t);
int maxheight(int x,int y);
ptrtree singleleft(ptrtree t);
ptrtree singleright(ptrtree t);
ptrtree doublleft(ptrtree t);
ptrtree doublright(ptrtree t);
void printree(ptrtree t);
ptrtree makeempty(ptrtree t)
{
if(t!=NULL)
{
makeempty(t->left);
makeempty(t->right);
}
return NULL;
}
position find(char x,ptrtree t)
{
if(t==NULL)
{
return 0;
}
if(x<t->x)
{
return find(x,t->left);
}
if(x>t->x)
{
return find(x,t->right);
}
else
{
return t;
}
}
position findmin(ptrtree t)
{
if(t==NULL)
{
return NULL;
}
else if(t->left=NULL)
{
return t;
}
else
{
return findmin(t->left);
}
}
position findmax(ptrtree t)
{
if(t==NULL)
{
return NULL;
}
else if(t->right=NULL)
{
return t;
}
else
{
return findmax(t->right);
}
}
int height(ptrtree t)
{
if(t==NULL)
{
return -1;
}
else
{
return t->height;
}
}
int maxheight(int x,int y)
{
if(x>=y)
{
return x;
}
else
{
return y;
}
}
ptrtree singleleft(ptrtree t)
{
position k1;
k1=t->left;
t->left=k1->right;
k1->right=t;
t->height=maxheight(t->left->height,t->right->height)+1;
k1->height=maxheight(t->left->height,t->right->height)+1;
return k1;
}
ptrtree singleright(ptrtree t)
{
position k1;
k1=t->right;
t->right=k1->left;
k1->left=t;
t->height=maxheight(t->left->height,t->right->height)+1;
k1->height=maxheight(t->left->height,t->right->height)+1;
}
ptrtree doubleft(ptrtree t)
{
t->left=singleright(t->left);
return singleleft(t);
}
ptrtree doublright(ptrtree t)
{
t->right=singleleft(t->right);
return singleright(t);
}
ptrtree insert(int x,ptrtree t)
{
if(t=NULL)
{
t=(struct treenode *)malloc(LEN);
if(t==NULL)
{
printf("没地了");
}
else
{
t->x=x;
t->height=0;
t->left=NULL;
t->right=NULL;
}
}
else if(x<t->x)
{
t->left=insert(x,t->left);
if((height(t->left)-height(t->right))==2)
{
if(x<t->left->x)
{
t=singleleft(t);
}
else
{
t=doubleft(t);
}
}
}
else if(x>t->x)
{
t->right=insert(x,t->right);
if((height(t->left)-height(t->right))==2)
{
if(x>t->right->x)
{
t=singleright(t);
}
else
{
t=doublright(t);
}
}
}
t->height=maxheight(height(t->left),height(t->right))+1;
}
void printree(ptrtree t)
{
if(t!=NULL)
{
printree(t->left);
printf("%d\n",t->x);
printree(t->right);
}
}
int main()
{
ptrtree t;
t=makeempty(t);
t=insert(1,t);
t=insert(2,t);
t=insert(3,t);
t=insert(4,t);
t=insert(5,t);
t=insert(6,t);
printf("%d",t->x);
return 0;
}
运行结果