二叉树的c语言程序,二叉树C语言的程序代码.pdf

# include "stdio.h"

# include "stdlib.h"

# include "malloc.h"

typedef struct Node{

char data; //定义根结点

struct Node *lchild; //定义左子树

struct Node *rchild; //定义右子树

}Node,*BiTree;

int JianShu(BiTree &T) { //构造二叉链表表示的二叉树T,按先序遍历输入二

//叉树中结点的值(一个字符),空格字符表示空树.

char e;

T=(Node *)malloc(sizeof(Node)); //开辟一个以sizeof(Node)为单位的空间

if(!T) //开辟失败

exit (-2);

fflush(stdin); //清空缓存

scanf ("%c",&e);

if(e==' ')

T=NULL;

else {

T->data=e; // 生成根结点

printf ("请输入%c 的左孩子:",e);

JianShu(T->lchild); // 构造左子树

printf ("请输入%c 的右孩子:",e);

JianShu(T->rchild); // 构造右子树

}

return 1;

}

int DLR_P(BiTree &T) { //先序遍历打印二叉树中所有数据

if (T!=NULL) { //非空二叉树

printf ("%c ",T->data); //访问T

DLR_P(T->lchild); //递归遍历左子树

DLR_P(T->rchild); //递归遍历右子树

}

return 1;

}

int LDR_P(BiTree &T) { //中序遍历打印二叉树中所有数据

if (T!=NULL) { //非空二叉树

LDR_P(T->lchild); //递归遍历左子树

printf ("%c ",T->data); //访问T

LDR_P(T->rchild); //递归遍历右子树

}

return 1;

}

int LRD_P(BiTree &T) { //后序遍历打印二叉树中所有数据

if (T!=NULL) { //非空二叉树

LRD_P(T->lchild); //递归遍历左子树

LRD_P(T->rchild); //递归遍历右子树

printf ("%c ",T->data); //访问T

}

return 1;

}

int DLR_0(BiTree &T,int &s_0) { //用先序遍历求二叉树T 中所有叶子总数

if (T!=NULL) {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值