#include<stdio.h>
#include<stdlib.h>
#define datatype char
#define M 50
#define MAXLEN 50
int num=0,counter1=0,counter2=0;
typedef struct node //定义一个二叉链表结构
{
datatype data;
int lt,rt;
struct node *lchild,*rchild;
}BT;
BT* createtree() // 1---创 建 树
{
BT *bt;
datatype x;
scanf("\n%c",&x);
if(x=='0') //指针为空的标志
{
bt=NULL; //
}
else
{
bt=(BT*)malloc(sizeof(BT)); // 申请一个新的的结点空间
bt->data=x;
bt->lchild=createtree(); // 创建结点的左子树
bt->rchild=createtree(); // 创建结点的右子树
}
return bt;
}
ShowTree(BT *T) //2--- 显 示 树
{
int i;
if(T==NULL)
{
return; //当结点为空,
}
else
{
num++; //全局变量 num 调控结点的位置
printf("\t\t\t");
for(i=0;i<num;i++)
{
printf(" ");
}
printf("%c",T->data);
for(i=0;i<20-num*2;i++)
{
printf("-");
}
printf("\n");
ShowTree(T->lchild); // 显示结点的左子树
ShowTree(T->rchild); // 显示结点的右子树
num=num-1;
}
}
void Preorder(BT *T) // 3---先 序 遍 历 树
{
if(T==NULL)
{
return;
}
else // 本函数调用结束
{
printf("%c ",T->data); // 输出结点的数据域
Preorder(T->lchild); // 先序递归遍历左子树
Preorder(T->rchild); // 先序递归遍历右子树
}
}
void Inorder(BT *T) // 4---中 序 遍 历 树
{
if(T==NULL)
{
return;
}
else // 本函数调用结束
{
Inorder(T->lchild); // 中序递归遍历左子树
printf("%c ",T->data)
数据结构(二叉树子系统:c语言实现)
最新推荐文章于 2024-09-07 11:07:16 发布

最低0.47元/天 解锁文章
4746





