二叉排序树的遍历及输出

来源:互联网 发布:dota1深渊领主技能数据 编辑:程序博客网 时间:2024/04/30 16:35
#include<stdio.h>
#include<stdlib.h>
typedef struct node{//BST节点
int key;
struct node *lchild,*rchild;
}BSTNode,*BSTree;
void InsertBST(BSTree *bst,int key){//插入程序
BSTree s;
if(*bst==NULL){
s=(BSTree)malloc(sizeof(BSTNode));
s->key=key;
s->lchild=NULL;s->rchild=NULL;
*bst=s;
}
else if(key<(*bst)->key)
InsertBST(&((*bst)->lchild),key);
else if(key>(*bst)->key)
InsertBST(&((*bst)->rchild),key);
}
void CreateBST(BSTree *bst){//调用插入,实现BST的建立
int key;
*bst=NULL;
scanf("%d",&key);
while(key!=-1){
InsertBST(bst,key);
scanf("%d",&key);
}
}
void mid(BSTree *bst){//BST中序遍历是一个递增的序列
if(*bst){
mid(&((*bst)->lchild));
printf("%d ",(*bst)->key);
mid(&((*bst)->rchild));
}
}
void main(){
BSTree p;
CreateBST(&p);
mid(&p);
}
0 0
原创粉丝点击