二叉排序树的生成和插入
大家好,我自己写的二叉排序树算法,自己感觉没有错,但一运行起来,就自动停止。求大神帮助下。
-----------------
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *lchild,*rchild;
}BSTNode;
typedef BSTNode * BinTree;
//二叉排序树的插入,把新输入的数据插入到叶子结点
BinTree BinSort(BinTree bt,BSTNode *p){
BSTNode *h,*f;
h=bt;
while(h){
f=h;
if(h->data>p->data) h=h->lchild;
if(h->data<p->data) h=h->rchild;
}
if(bt==NULL) bt=p;
else if(f->data>p->data) f->lchild=p;
else f->rchild=p;
printf("\nI am executed\n");
return bt;
}
BinTree CreateBinTree(){ //输入数据
BinTree bt=NULL;
int k;
BSTNode *p;
printf("Please input a number 0 to end:\n");
scanf("%d",&k);
while(k){
p=(BSTNode *)malloc(sizeof(BSTNode));
p->data=k;
p->lchild=p->rchild=NULL;
bt=BinSort(bt,p);
scanf("%d",&k);
}
return bt;
}
int main(){
BinTree bt;
bt=CreateBinTree();
}