31三元组创建二叉树

//姓名:高万禄
//名称:三元组创建二叉树
//日期:2020/2/19
#include<stdio.h>
#include<stdlib.h>
#define SIZE sizeof(struct tree)
struct {
	struct tree* A[10];
	int front;
	int rear;
}duilie;
struct tree
{
	char data;
	struct tree* lchild, * rchild;
};
int creattree(struct tree** T);
int main(void)
{
	duilie.front = duilie.rear = 0;//队列初始化
	struct tree* p;
	struct tree** T=&p;
	creattree(T);
	return 0;
}
int creattree(struct tree** T)
{
	char a, b, c,temp;
	struct tree* node, * p;
	scanf_s("%c%c%c", &a, &b, &c);
	temp = getchar();
	if (a == '0' && b != '0')//创建根节点
	{
		node = (struct tree*)malloc(SIZE);
		if (!node)
			return -1;
		node->data = b;
		node->lchild = node->rchild = NULL;
		*T = node;//存储根节点
		duilie.A[duilie.rear] = node;//根节点入队
		duilie.rear = (duilie.rear + 1 + 10) % 10;
	}
	scanf_s("%c%c%c", &a, &b, &c);
	temp = getchar();
	while (a!='0'&&b!='0'&&duilie.front!=duilie.rear)
	{
		p = duilie.A[duilie.front];//取出队头
		duilie.front = (duilie.front + 1 + 10) % 10;//出队
		while (a==p->data)//最多判断两次,一个节点最多有两个孩子
		{
			node = (struct tree*)malloc(SIZE);
			if (!node)
				return -1;
			node->data = b;
			node->lchild = node->rchild = NULL;
			if (c == 'L')
			{
				p->lchild = node;
			}
			if (c == 'R')
			{
				p->lchild = node;
			}
			duilie.A[duilie.rear] = node;
			duilie.rear = (duilie.rear + 1 + 10) % 10;//新节点入队
			scanf_s("%c%c%c", &a, &b, &c);
			temp = getchar();//输入下一组数据
		}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高万禄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值