二叉树

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值