#include<stdio.h>
#include<stdlib.h>
//******************************************************************定义队列
//*************************定义二叉树左右形式
struct bintree {
int Data;
struct bintree* Left;
struct bintree* Right;
};
typedef struct bintree* BinTree;
//******************************定义队列中的元素形式
typedef struct Node* PtrToNode;
struct Node {
BinTree BT;
PtrToNode* Next;
};
typedef PtrToNode Position;
//******************************定义队列的具体形式
typedef struct QNode* PtrToQNode;
struct QNode {
Position Front, Rear;
};
typedef PtrToQNode Queue;
//*******************************************************************定义队列结
Queue CreatQueue()//***********************创建空堆栈
{
Queue Q;
Q = (Queue)malloc(sizeof(PtrToQNode));
Q->Front = Q->Rear = NULL;
return Q;
}
//*******************************************************************压入队列
void Add(Queue Q, BinTree BT)
{
Position tem;
tem = (Position)malloc(sizeof(struct Node));
tem->BT = BT;
tem->Next = NULL;
if (Q->Front == NULL) {
Q->Front = Q->Rear = tem;
}
else {
Q->Rear->Next = tem;
Q->Rear=tem;
}
}
int IsEmpty(Queue Q)
{
return Q->Front == NULL;
}
BinTree Delete(Queue Q)
{
BinTree BT;
Position tem;
if (IsEmpty(Q)) {
return NULL;
}
else {
tem = Q->Front;
if (Q->Front == Q->Rear) {
Q->Front = Q->Rear = NULL;
}
else {
Q->Front = Q->Front->Next;
}
BT = tem->BT;
free(tem);
return BT;
}
}
BinTree CreatBinTree()
{
int Data;
BinTree BT, T;
Queue Q = CreatQueue();//创建空队列
scanf_s("%d",&Data);//创建根节点
if (Data != 0) {
BT = (BinTree)malloc(sizeof(struct bintree));
BT->Data = Data;
BT->Left = BT->Right = NULL;
Add(Q, BT);
}
else {
return NULL;
}
while (!IsEmpty(Q)) {
T = Delete(Q);//从队列中取出一节点的地址
scanf_s("%d", &Data);//输入左孩子
if (Data == 0) {
T->Left = NULL;
}
else {
T->Left = (BinTree)malloc(sizeof(struct bintree));
T->Left->Data = Data;
T->Left->Left = T->Left->Right = NULL;
Add(Q, T->Left);
}
scanf_s("%d", &Data);//输入T的右孩子
if (Data == 0) {
T->Right = NULL;
}
else {
T->Right = (BinTree)malloc(sizeof(struct bintree));
T->Right->Data = Data;
T->Right->Left = T->Right->Right = NULL;
Add(Q, T->Right);
}
}
return BT;
}
//*************************先序遍历
void PrintX(BinTree BT)
{
if (BT) {
printf("%d ", BT->Data);
PrintX(BT->Left);
PrintX(BT->Right);
}
}
void PrintZ(BinTree BT)
{
if (BT) {
PrintX(BT->Left);
printf("%d ", BT->Data);
PrintX(BT->Right);
}
}
void PrintH(BinTree BT)
{
if (BT) {
PrintX(BT->Left);
PrintX(BT->Right);
printf("%d ", BT->Data);
}
}
void PrintYe(BinTree BT)//**********************************************越界
{
if (!BT->Left && !BT->Right) {
printf("%d ", BT->Data);
}
PrintYe(BT->Left);
PrintYe(BT->Right);
}
int main()
{
BinTree BT = CreatBinTree();
//PrintX(BT);
printf("\n");
PrintZ(BT);
printf("\n");
PrintH(BT);
printf("\n");
PrintYe(BT);
system("pause");
return 0;
}
#include<stdlib.h>
//******************************************************************定义队列
//*************************定义二叉树左右形式
struct bintree {
int Data;
struct bintree* Left;
struct bintree* Right;
};
typedef struct bintree* BinTree;
//******************************定义队列中的元素形式
typedef struct Node* PtrToNode;
struct Node {
BinTree BT;
PtrToNode* Next;
};
typedef PtrToNode Position;
//******************************定义队列的具体形式
typedef struct QNode* PtrToQNode;
struct QNode {
Position Front, Rear;
};
typedef PtrToQNode Queue;
//*******************************************************************定义队列结
Queue CreatQueue()//***********************创建空堆栈
{
Queue Q;
Q = (Queue)malloc(sizeof(PtrToQNode));
Q->Front = Q->Rear = NULL;
return Q;
}
//*******************************************************************压入队列
void Add(Queue Q, BinTree BT)
{
Position tem;
tem = (Position)malloc(sizeof(struct Node));
tem->BT = BT;
tem->Next = NULL;
if (Q->Front == NULL) {
Q->Front = Q->Rear = tem;
}
else {
Q->Rear->Next = tem;
Q->Rear=tem;
}
}
int IsEmpty(Queue Q)
{
return Q->Front == NULL;
}
BinTree Delete(Queue Q)
{
BinTree BT;
Position tem;
if (IsEmpty(Q)) {
return NULL;
}
else {
tem = Q->Front;
if (Q->Front == Q->Rear) {
Q->Front = Q->Rear = NULL;
}
else {
Q->Front = Q->Front->Next;
}
BT = tem->BT;
free(tem);
return BT;
}
}
BinTree CreatBinTree()
{
int Data;
BinTree BT, T;
Queue Q = CreatQueue();//创建空队列
scanf_s("%d",&Data);//创建根节点
if (Data != 0) {
BT = (BinTree)malloc(sizeof(struct bintree));
BT->Data = Data;
BT->Left = BT->Right = NULL;
Add(Q, BT);
}
else {
return NULL;
}
while (!IsEmpty(Q)) {
T = Delete(Q);//从队列中取出一节点的地址
scanf_s("%d", &Data);//输入左孩子
if (Data == 0) {
T->Left = NULL;
}
else {
T->Left = (BinTree)malloc(sizeof(struct bintree));
T->Left->Data = Data;
T->Left->Left = T->Left->Right = NULL;
Add(Q, T->Left);
}
scanf_s("%d", &Data);//输入T的右孩子
if (Data == 0) {
T->Right = NULL;
}
else {
T->Right = (BinTree)malloc(sizeof(struct bintree));
T->Right->Data = Data;
T->Right->Left = T->Right->Right = NULL;
Add(Q, T->Right);
}
}
return BT;
}
//*************************先序遍历
void PrintX(BinTree BT)
{
if (BT) {
printf("%d ", BT->Data);
PrintX(BT->Left);
PrintX(BT->Right);
}
}
void PrintZ(BinTree BT)
{
if (BT) {
PrintX(BT->Left);
printf("%d ", BT->Data);
PrintX(BT->Right);
}
}
void PrintH(BinTree BT)
{
if (BT) {
PrintX(BT->Left);
PrintX(BT->Right);
printf("%d ", BT->Data);
}
}
void PrintYe(BinTree BT)//**********************************************越界
{
if (!BT->Left && !BT->Right) {
printf("%d ", BT->Data);
}
PrintYe(BT->Left);
PrintYe(BT->Right);
}
int main()
{
BinTree BT = CreatBinTree();
//PrintX(BT);
printf("\n");
PrintZ(BT);
printf("\n");
PrintH(BT);
printf("\n");
PrintYe(BT);
system("pause");
return 0;
}