数据结构实验

一、【实验内容】

编写能够通过键盘输入建立二叉排序树,并在建立完立即在屏幕显示中序遍历结果的程序

二、【程序代码】

#include<stdio.h>
#include<stdlib.h>
#define MAX 5
typedef struct Bnode
{
int key;
struct Bnode *left;
struct Bnode *right;
}Bnode;
Bnode * btInsert(int x,Bnode *root);
void Inorder(Bnode *root);
void main()
{
int i;
int a[MAX]={60,40,70,20,80};
Bnode * root=NULL;
printf("按关键字序列建立二叉排序树\n"); .
for(i=0;<MAX;i++) printf("%d ",a[i]);
printf("\n");
for(i=0;i<MAX;++) root=btInsert(a[i],root);
printf("中序遍历的二叉排序树\n"); 
Inorder(root);
printf("n");
}
Bnode * btInsert(int x,Bnode * root)
{
Bnode *p,*q;
int flag=0;
p=(Bnode *)malloc(sizeof(Bnode));
p->key=x;
p->right=p->left=NULL;
if(root==NULL)
{ root=p; return p; }
q=root;
while(flag==0)
{
if(q->key>x)
{
if(q->left!=NULL)
q=q->left;
else
{
q->left=p;
flag=1;
     }
}
else
{
if(q->right!=NULL)
q=q->right;
else
{
q->right=p;
flag=1;
      }
   }
}
return root;
}
void Inord
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值