运行中断c语言,求大神!运行看看为何会中断?!

这篇博客介绍了一个C语言实现的二叉排序树(BST)的数据结构,包括创建、检索、插入和中序遍历操作。代码中定义了BST的节点结构,并提供了搜索、插入和创建BST的函数。在main函数中,读取一系列整数并插入BST,然后进行中序遍历打印节点值。
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

这是代码,主要完成二叉排序树的创建 可是运行会中断,

#include

#include

typedef int KeyType;

struct BinSearchNode;

typedef struct BinSearchNode *PBinSearchNode;

struct BinSearchNode{

KeyType key;//关键字

PBinSearchNode llink,rlink;//左指针,右指针

};

typedef struct BinSearchNode *BinSearchTree;//二叉排序树

typedef BinSearchTree *PBinSearchTree;

//检索算法

int search(PBinSearchTree ptree,KeyType key,PBinSearchNode *position){

PBinSearchNode p,q;

p=*ptree;

q=p;

while(p!=NULL){

q=p;//q记录父节点

if(p->key==key){*position=p;return 1;}//检索成功

else if(p->key>key) p=p->llink;//进入左子树继续检索

else p=p->rlink;//进入右子树继续检索

}

*position=q;return 0;//检索失败

}

//插入算法

int insert (PBinSearchTree ptree,KeyType key)

{

PBinSearchNode p,position;

if(search(ptree,key,&position)==1)return 1;//已经存在key关键字

p=(PBinSearchNode)malloc(sizeof(struct BinSearchNode));//申请新节点

if(p==NULL)

{

printf("Error\n");

return 0;

}

p->key=key;p->llink=p->rlink=NULL;

if(position==NULL)*ptree=p;

else if(keykey)position->llink=p;

else position ->rlink=p;

return 1;

}

int createSearchTree(PBinSearchTree ptree,KeyType *a,int n)

{

int i;

*ptree=NULL;

for(i=0;i

//printf("before insert\n");

if(!insert(ptree,a[i]))

return 0;

}

//printf("after inserted\n");

return 1;

}

PBinSearchNode leftchild(PBinSearchNode p)

{

if(p==NULL)return NULL;

return p->llink;

}

PBinSearchNode rightchild(PBinSearchNode p)

{

if(p==NULL)return NULL;

return p->rlink;

}

void inorder(BinSearchTree t)

{ if(t==NULL)return;

//printf("inorder\n");

if (t) {

inorder(leftchild(t));

printf("%5d",t->key);

inorder(rightchild(t));

}

}

int main()

{

PBinSearchTree ptree;

PBinSearchNode t;

int i,s,a[10];

for(i=0;i<10;i++)

{

scanf("%d",&a[i]);

}

s=createSearchTree(ptree,a,10);

t=*ptree;

inorder(t);

system("pause");

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值