//姓名:高万禄
//名称:三元组创建二叉树
//日期: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();//输入下一组数据
}
}
}
31三元组创建二叉树
最新推荐文章于 2022-11-13 12:14:15 发布