#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#include"Quene.h"
typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType data;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
}BTNode;
BTNode* BuyNode(BTDataType x);
BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi);
void BinaryTreeDestory(BTNode* root);
// 二叉树节点个数
int BinaryTreeSize(BTNode* root);
// 二叉树叶子节点个数
int BinaryTreeLeafSize(BTNode* root);
// 二叉树第k层节点个数
int BinaryTreeLevelKSize(BTNode* root, int k);
// 二叉树查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
// 二叉树前序遍历
void BinaryTreePrevOrder(BTNode* root);
// 二叉树中序遍历
void BinaryTreeInOrder(BTNode* root);
// 二叉树后序遍历
void BinaryTreePostOrder(BTNode* root);
// 层序遍历
void BinaryTreeLevelOrder(BTNode* root);
// 判断二叉树是否是完全二叉树
bool BinaryTreeComplete(BTNode* root);
#define _CRT_SECURE_NO_WARNINGS 1
#include "BinaryTree.h"
#include "Quene.c"
BTNode* BuyNode(BTDataType x)
{
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
node->data = x;
node->left = NULL;
node->right = NULL;
return node;
}
BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi)
{
BTNode* node1 = BuyNode('A');
BTNode* node2 = BuyNode('B');
BTNode* node3 = BuyNode('C');
BTNode* node4 = BuyNode('D');
BTNode* node5 = BuyNode('E');
BTNode* node6 = BuyNode('F');
BTNode* node7 = BuyNode('G');
node1->left = node2;
node1->right = node3;
node2->left = node4;
node3->left = node5;
node3->right = node6;
node4->left = node7;
return node1;
}
void BinaryTreeDestory(BTNode* root)
{
if (root == NULL)
return;
//终止条件:根为空
BinaryTreeDestory(root->left);
BinaryTreeDestory(root->right);
free(root);
}
int BinaryTreeSize(BTNode* root)
{
//递推关系;树的节点个数 = 左子树节点个数 + 右子树节点个数 + 1
//终止条件:根为空指针,也就是遍历到空指针
return root == NULL ? 0 : 1
+ BinaryTreeSize(root->left)
+ BinaryTreeSize(root->right);
}
int BinaryTreeLeafSize(BTNode* root)
{
//节点为空
if (root == NULL)
return 0;
//节点为叶节点
else if (root->left == NULL && root->right == NULL)
return 1;
//树的叶节点个数 = 左子树叶节点个数 + 右子树叶节点个数
return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}
int BinaryTreeLevelKSize(BTNode* root, int k)
{
//节点为空
if (root == NULL)
return 0;
//节点为第一层,第一层只有一个节点,也是终止条件
else if (k == 1)
return 1;
//当前树的第k层 = 左子树的第K-1层 + 右子树的第k-1层
return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);
}
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
if (root == NULL)
return NULL;
if (root->data == x)
return root;
BTNode* left = BinaryTreeFind(root->left, x);
if (left)
return left;
BTNode* right = BinaryTreeFind(root->right, x);
if (right)
return right;
}
void BinaryTreePrevOrder(BTNode* root)
{
if (root == NULL) {
printf("NULL ");
return;
}
printf("%c ", root->data);
PreOrder(root->left);
PreOrder(root->right);
}
void BinaryTreeInOrder(BTNode* root)
{
if (root == NULL)
{
printf("NULL");
return;
}
BinaryTreeInOrder(root->left);
printf("%c ", root->data);
BinaryTreeInOrder(root->right);
}
void BinaryTreePostOrder(BTNode* root)
{
if (root == NULL)
{
printf("NULL");
return;
}
BinaryTreePostOrder(root->left);
BinaryTreePostOrder(root->right);
printf("%c ", root->data);
}
void BinaryTreeLevelOrder(BTNode* root)
{
//利用队列实现
//先入第一层,上一层出来,把下一层带进去
Quene q;
QueneInit(&q);
//节点非空就入队
if (root)
QuenePush(&q, root);
while (!QueneEmpty(&q))
{
BTNode* front = QueneFront(&q);
QuenePop(&q);
printf("%c ", front->data);
if (front->left)
QuenePush(&q, front->left);
if (front->right)
QuenePush(&q, front->right);
}
printf("\n");
QueneDestory(&q);
}
bool BinaryTreeComplete(BTNode* root)
{
//层序遍历,空也入队
//如果是完全二叉树,那么空是连续,非空也是连续
//不是完全二叉树,空不是连续,非空不是连续
Quene q;
QueneInit(&q);
if (root)
QuenePush(&q, root);
while (!QueneEmpty(&q))
{
BTNode* front = QueneFront(&q);
QuenePop(&q);
//出到空就跳出循环,判断队列后面元素是否都为空
if (front == NULL)
break;
QuenePush(&q, front->left);
QuenePush(&q, front->right);
}
//出到空,队列以后全是空,就是完全二叉树
//还有非空,说明不是完全二叉树
while (!QueneEmpty(&q))
{
BTNode* front = QueneFront(&q);
QuenePop(&q);
if (front != NULL)
return false;
}
QueneDestory(&q);
return true;
}
层序遍历利用的队列
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#include"BinaryTree.h"
typedef BTNode* QDataType;
typedef struct QueneNode
{
struct QueneNode* next;
QDataType data;
}QueneNode;
typedef struct Quene
{
QueneNode* phead;
QueneNode* ptail;
}Quene;
void QueneInit(Quene* pq);
void QueneDestory(Quene* pq);
void QuenePush(Quene* pq, QDataType x);
void QuenePop(Quene* pq);
int QueneSize(Quene* pq);
QDataType QueneFront(Quene* pq);
QDataType QueneBack(Quene* pq);
bool QueneEmpty(Quene* pq);
#define _CRT_SECURE_NO_WARNINGS 1
#include"Quene.h"
void QueneInit(Quene* pq)
{
assert(pq);
pq->phead = NULL;
pq->ptail = NULL;
}
void QueneDestory(Quene* pq)
{
assert(pq);
QueneNode* cur = pq->phead;
while (cur)
{
QueneNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
}
void QuenePush(Quene* pq, QDataType x)
{
assert(pq);
QueneNode* newnode = (QueneNode*)malloc(sizeof(QueneNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->ptail == NULL)
pq->phead = pq->ptail = newnode;
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
}
void QuenePop(Quene* pq)
{
assert(pq);
assert(!QueneEmpty(pq));
if (pq->phead->next = NULL)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else
{
QueneNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
}
int QueneSize(Quene* pq)
{
assert(pq);
int n = 0;
QueneNode* cur = pq->phead;
while (cur)
{
cur = cur->next;
n++;
}
return n;
}
QDataType QueneFront(Quene* pq)
{
assert(pq);
assert(!QueneEmpty(pq));
return pq->phead->data;
}
QDataType QueneBack(Quene* pq)
{
assert(pq);
assert(!QueneEmpty(pq));
return pq->ptail->data;
}
bool QueneEmpty(Quene* pq)
{
assert(pq);
return pq->ptail == NULL && pq->phead == NULL;
}