c - 我尝试在 AVL 树中实现插入,但是当我尝试运行它时出现分段错误。
问题描述
当我尝试运行我的程序时,它需要输入到 2 或 3 步然后它突然结束,即使我按 Y 输入更多元素。这是我输出的屏幕截图:这是我的代码段,lc 代表左child,rc 代表右孩子,ht 代表树的高度:
void inorder(struct node *root) //print inorder
{
if(root->lc!=NULL)
inorder(root->lc);
printf("%d ",root->data);
if(root->rc!=NULL)
inorder(root->rc);
}
struct node *insert(struct node *T,int x) //code for insertion of a node
{
if(T==NULL)
{
T=(struct node*)malloc(sizeof(struct node));
T->data=x;
T->lc=NULL;
T->rc=NULL;
}
else
{
if(x > T->data)
{
T->rc=insert(T->rc,x);
if(BF(T)==-2)
if(x>T->rc->data)
T=RR(T);
else
T=RL(T);
}
else
{
if(x<T->data)
{
T->lc=insert(T->lc,x);
if(BF(T)==2)
if(x < T->lc->data)
T=LL(T);
else
T=LR(T);
}
}
T->ht=height(T);
return(T);
}
}
int main()
{
struct node *root=NULL;
int ele;
char ch;
do
{
printf("\n Enter the value to be inserted in AVL: ");
scanf("%d",&ele);
root=insert(root,ele);
printf("\n Want to insert more elements? (y/n): ");
getchar();
scanf("%c",&ch);
}while(ch=='y' || ch=='Y');
printf("\n Press any key to print inorder traversal: ");
getchar();
inorder(root);
return 0;
}
解决方案
推荐阅读
- c# - 我们如何识别winform控件是主线程访问还是工作线程访问,来编写InvokeRequired功能?
- javascript - 推送到heroku被拒绝
- android - 如何在约束布局android中将图像放在编辑文本的左侧或右侧,如下所示
- python - 在 xlsxwriter 模块中使 for 循环动态化
- jenkins - 引诱詹金斯报告不显示步骤中的“设置”和“拆卸”方法
- python - Python函数没有传入,openCV
- certificate - c#:如何使用 CERTENROLLLib.CX509PrivateKey 创建密钥大小为 168 的证书?
- windows - 无法推送到 Windows Server 2016 上的 git 存储库
- android - 添加时获取 java.lang.NullPointerException
我资源的要素 - python - 将重复的行保存在单独的数据框中