二叉排序树的建立c语言编程,C语言程序—二叉排序树的建立与遍历.doc

C语言程序—二叉排序树的建立与遍历

#include

#include

#define maxsize 50

typedef int elemtype;

typedef struct Node

{ elemtype data;

struct Node *lchild;

struct Node *rchild;

} nodetype;

/*建立只有根结点的二叉树*/

/*初始化*/

int initiate(nodetype **bt);/*创建二叉树*/

nodetype *create(nodetype *bt,elemtype x);

nodetype *insertl(elemtype x,nodetype *parent); /*将结点x插入到结点parent的左孩子结点*/

nodetype *insertr(elemtype x,nodetype *parent); /*将结点x插入到结点parent的右孩子结点*/

nodetype *insert(nodetype *bt);

nodetype *deleted(nodetype *bt);

void printtree(nodetype *bt,int n);

void printftree(nodetype *bt);

void preorder(nodetype *bt);

void inorder(nodetype *bt);

void postorder(nodetype *bt);

int main( )

{ int sel,n;

elemtype x;

nodetype *bt;

if((bt=(nodetype *)malloc(sizeof(nodetype)))==NULL)

return NULL;

printf("\n请输入你的选择:1--Initiate,2--Creat,3--InsertL,4--InsertR,5--Insert,");

printf("6--Delete,7--PrintTree,8--PrintFtree,9--Preorder,10--Inorder,11--Postorder,12--Quit\n");

scanf("%d",&sel);

while(sel!=12)

{

if(sel==1)

{

initiate(&bt);

printf("完成初始化!\n");

}

if(sel==2)

{

printf("请输入创建树根节点的数据:");

scanf("%d",&x);

create(bt,x);

}

if(sel==3)

{

printf("请输入左插入节点的数据:");

scanf("%d",&x);

insertl(x,bt);

}

if(sel==4)

{

printf("请输入右插入节点的数据:");

scanf("%d",&x);

insertr(x,bt);

}

if(sel==5)

{

insert(bt);

}

if(sel==6)

{

deleted(bt);

}

if(sel==7)

{

printf("请输入要显示空格的个数:");

scanf("%d",&n);

printtree(bt,n);

}

if(sel==8)

{

printftree(bt);

}

if(sel==9)

{

preorder(bt);

}

if(sel==10)

{

inorder(bt);

}

if(sel==11)

{

postorder(bt);

}

printf("\n请输入你的选择:1--Initiate,2--Creat,3--InsertL,4--InsertR,5--Insert,");

printf("6--Delete,7--PrintTree,8--PrintFtree,9--Preorder,10--Inorder,11--Postorder,12--Quit\n");

scanf("%d",&sel);

}

return 1;

}

int initiate(nodetype **bt)

{

if((*bt=(nodetype*)malloc(sizeof(nodetype)))==NULL)

return 0;

(*bt)->lchild=NULL;

(*bt)->rchild=NULL;

return 1;

}

nodetype *cr

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值