@TOC

## 一、

### 0.建立一棵树

``````typedef char BTDataType;

typedef struct BinaryTreeNode
{
BTDataType data;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;

}BTNode;

int main()
{
BTNode* A = (BTNode*)malloc(sizeof(BTNode));
A->data = 'A';
A->left = NULL;
A->right = NULL;

BTNode* B = (BTNode*)malloc(sizeof(BTNode));
B->data = 'B';
B->left = NULL;
B->right = NULL;

BTNode* C = (BTNode*)malloc(sizeof(BTNode));
C->data = 'C';
C->left = NULL;
C->right = NULL;

BTNode* D = (BTNode*)malloc(sizeof(BTNode));
D->data = 'D';
D->left = NULL;
D->right = NULL;

BTNode* E = (BTNode*)malloc(sizeof(BTNode));
E->data = 'E';
E->left = NULL;
E->right = NULL;

A->left = B;
A->right =C;

B->left = D;
B->right = E;

return 0;
}``````

### 1. 前序遍历

``````//后序遍历
void PrevOrder(BTNode* root)
{
if (root == NULL)
{
return;
}
printf("%c ", root->data);

PrevOrder(root->left);
PrevOrder(root->right);
}``````

### 2.中序遍历

1. 中序遍历(Inorder Traversal)——访问根结点的操作发生在遍历其左右子树之中（间）。

``````//中序遍历
void InOrder(BTNode* root)
{
if (root == NULL)
{
return;
}

InOrder(root->left);
printf("%c ", root->data);
InOrder(root->right);
}``````

### 3. 后序遍历

1. 后序遍历(Postorder Traversal)——访问根结点的操作发生在遍历其左右子树之后。

``````//后续遍历
void PostOrder(BTNode* root)
{
if (!root)
{
return;
}
PostOrder(root->left);
PostOrder(root->right);
printf("%c ", root->data);
}``````

## 二、

### 层序遍历

``````typedef struct BinaryTreeNode* QDataType;

typedef struct QueueNode
{
QDataType data;
struct QueueNode* next;
}QNode;

typedef struct Queue
{
QNode* tail;
int size;
}Queue;

void QueueInit(Queue* pq)
{
assert(pq);

pq->tail = NULL;
pq->size = 0;
}

void QueueDestroy(Queue* pq)
{
assert(pq);

QNode* cur = pq->head;
while (cur)
{
QNode* del = cur;
cur = cur->next;

free(del);
//del = NULL;
}

pq->head = pq->tail = NULL;
pq->size = 0;
}

void QueuePush(Queue* pq, QDataType x)
{
assert(pq);

QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
exit(-1);
}

newnode->data = x;
newnode->next = NULL;

if (pq->tail == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}

pq->size++;
}

void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));

if (pq->head->next == NULL)
{
pq->head = pq->tail = NULL;
}
else
{
QNode* del = pq->head;

free(del);
}

pq->size--;
}

QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));

}

bool QueueEmpty(Queue* pq)
{
assert(pq);

return pq->head == NULL && pq->tail == NULL;
}``````

``````//借助队列的先进先出来实现层级递进
void LevelOrder(BTNode* root)
{
//核心思路，上一层出的时候带下一层
Queue q;
QueueInit(&q);
if (root)
{
QueuePush(&q, root);
}

while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
printf("%c ", front->data);

//访问下一层
if (front->left)
{
QueuePush(&q, front->left);
}

if (front->right)
{
QueuePush(&q, front->right);
}
}
printf("\n");
QueueDestroy(&q);
}``````

## 三、有关二叉树练习

``````1.某完全二叉树按层次输出（同一层从左到右）的序列为 ABCDEFGH 。

A ABDHECFG
B ABCDEFGH
C HDBEAFCG
D HDEBFGCA``````

``````2.二叉树的先序遍历和中序遍历如下：先序遍历：EFHIGJK;中序遍历：HFIEJKG.则二叉树根结点为（）
A E
B F
C G
D H``````

``````3.设一课二叉树的中序遍历序列：badce，后序遍历序列：bdeca，

B decab
C debac
D abcde``````

``````4.某二叉树的后序遍历序列与中序遍历序列相同，均为 ABCDEF ，

A FEDCBA
B CBAFED
C DEFCBA
D ABCDEF``````