#include<stdio.h>
#include<conio.h>
struct avl
{
int h,data;
struct avl *l;
struct avl *r;
};
struct avl* insert(struct avl*,int);
void display(struct avl*);
int height(struct avl*);
int max(int,int);
struct avl* lr(struct avl*);
struct avl* lrr(struct avl*);
struct avl* rr(struct avl*);
struct avl* rlsr(struct avl*);
struct avl* lr(struct avl* t)
{
struct avl *node;
node=t->l;
t->l=node->r;
node->r=t;
t->h=max(height(t->l),height(t->r))+1;
node->h=max(height(node->l),height(t))+1;
return node;
}
struct avl* rr(struct avl* t)
{
struct avl *node;
node=t->r;
t->r=node->l;
node->l=t;
t->h=max(height(t->l),height(t->r))+1;
node->h=max(height(node->r),height(t))+1;
return node;
}
struct avl* lrr(struct avl* t)
{
t->l=rr(t->l);
return lr(t);
}
struct avl* rlr(struct avl* t)
{
t->r=rr(t->r);
return lr(t);
}
int max(int x,int y)
{
if(x>y)
return x;
else
return y;
}
int height(struct avl *t)
{
if(t==NULL)
return -1;
else
return t->h;
}
void display(struct avl *t)
{
if(t!=NULL)
{
display(t->l);
printf("\t %d\t %d\n",t->data,t->h);
display(t->r);
}
}
struct avl* insert(struct avl *t,int no)
{
if(t==NULL)
{
t=(struct avl *)malloc(sizeof(struct avl));
t->l=t->r=NULL;
t->data=no;
t->h=0;
}
else
{
if(t->data>no)
{
t->l=insert(t->l,no);
if(height(t->l)-height(t->r)==2)
if(t->l->data>no)
t=lr(t);
else
t=lrr(t);
}
else
{
t->r=insert(t->r,no);
if(height(t->l)-height(t->r)==-2)
if(t->r->data<no)
t=rr(t);
else
t=rlr(t);
}
t->h=max(height(t->l),height(t->r))+1;
}
return t;
}
int main()
{
struct avl *t;//declaring the structure node
t=NULL;//intialize to zero
int choice,no;
char ch='y';
while(ch=='y'||ch=='Y')//select choice option
{
printf("\n1:insert \n2:display \n3:exit \n");
printf("\n\nchoice is:::");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nenter the data for node:::");
scanf("%d",&no);
t=insert(t,no);break;//calling the insert option
case 2:
printf("\nInorder display\n");//
display(t);break;
case 3:
exit(0);
}
printf("\n do u want to continue...[y/n]:::");
scanf(" %c",&ch);
}
getch();
return 0;
}
abcd.rar_it
版权申诉
13 浏览量
2022-09-22
20:49:40
上传
评论
收藏 3KB RAR 举报
寒泊
- 粉丝: 74
- 资源: 1万+
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈