数据结构(二叉树子系统:c语言实现)

#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)
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值