创建
#include "Queue.h"//等会要用到队列
typedef int BTDataType;
typedef struct BinaryTreeNode
{
BTDataType _data;
struct BinaryTreeNode* _left;
struct BinaryTreeNode* _right;
}BTNode;
BTNode* BuyNode()
{
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
return node;
}
BTNode* CreatBinaryTree()
{
BTNode* node1 = BuyNode(1);
BTNode* node2 = BuyNode(2);
BTNode* node3 = BuyNode(3);
BTNode* node4 = BuyNode(4);
BTNode* node5 = BuyNode(5);
BTNode* node6 = BuyNode(6);
node1->_left = node2;
node1->_right = node4;
node2->_left = node3;
node4->_left = node5;
node4->_right = node6;
return node1;
}
销毁
销毁要注意一下,先把左右子树销毁了
如果先销毁根,那就找不到左右子树了
// 二叉树销毁
void BinaryTreeDestory(BTNode* root)
{
if (root == NULL)
return;
BinaryTreeDestory(root->_left);
BinaryTreeDestory(root->_right);
free(root);
}
一,前序遍历
// 二叉树前序遍历
void BinaryTreePrevOrder(BTNode* root)
{
if (root == NULL)
{
printf("N ");
}
printf("%d", root->_data);
BinaryTreePrevOrder(root->_left);
BinaryTreePrevOrder(root->_right);
}
二,中序遍历
// 二叉树中序遍历
void BinaryTreeInOrder(BTNode* root)
{
if (root == NULL)
{
printf("N ");
}
BinaryTreePrevOrder(root->_left);
printf("%d", root->_data);
BinaryTreePrevOrder(root->_right);
}
三、后序遍历
// 二叉树后序遍历
void BinaryTreePostOrder(BTNode* root)
{
if (root == NULL)
{
printf("N ");
}
BinaryTreePrevOrder(root->_left);
BinaryTreePrevOrder(root->_right);
printf("%d", root->_data);
}
四、层序遍历
再层序遍历之前,我们先把队列的val设置为树节点的指针
// 层序遍历
void BinaryTreeLevelOrder(BTNode* root)
{
Queue q;
QueueInit(&q);
//BTNode* front = QueueFront(&q);
QueuePush(&q,root);//先把根节点入进队列里
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);//取第一个根节点
QueuePop(&q);//删除的是队列的节点,不影响树(队列存的是树的节点的指针)
printf("%d ", front->_data);
if (front->_left)//左孩子不为空打印
QueuePush(&q, front->_left);
if (front->_right)//左孩子不为空打印
QueuePush(&q, front->_right);
}
QueueDestroy(&q);
}
跟着代码走读一下:
五、判断二叉树是否为完全二叉树
思路:
1.层序遍历走,空也进队列
2.遇到第一个空节点就开始判断,如果后面全是空,那就是完全二叉树,如果后面还有非空,就不是完全二叉树
/ 判断二叉树是否是完全二叉树
bool BinaryTreeComplete(BTNode* root)
{
Queue q;
QueueInit(&q);
BTNode* front = QueueFront(&q);
QueuePush(&q, front);
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front == NULL)//遇到第一个空就开始判断
break;
QueuePush(&q, front->_left);
QueuePush(&q, front->_right);
}
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front)//第一个空后还有非空就不是完全二叉树
return false;
QueuePush(&q, front->_left);
QueuePush(&q, front->_right);
}
QueueDestroy(&q);
return true;
}
六、二叉树节点个数
// 二叉树节点个数
int BinaryTreeSize(BTNode* root)
{
if (root == NULL)
{
return 0;
}
return BinaryTreeSize(root->_left) + BinaryTreeSize(root->_right) + 1;
}
七、二叉树叶子节点个数
// 二叉树叶子节点个数
int BinaryTreeLeafSize(BTNode* root)
{
if (root->_left == NULL && root->_right == NULL)//左子树和右子树都为空就是叶子节点
{
return 1;
}
return
BinaryTreeLeafSize(root->_left)+
BinaryTreeLeafSize(root->_right);
}
八、二叉树查找值为x的节点
// 二叉树查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
assert(root);
if (root->_data == x)
{
return root;
}
if (BinaryTreeFind(root->_left, x))//如果左树为空就向右树查找
return BinaryTreeFind(root->_left, x);
else
return BinaryTreeFind(root->_right, x);
}
九、二叉树层高
//二叉树层高
int TreeHight(BTNode* root)
{
if (root == NULL)
return 0;
int LeftHight = TreeHight(root->_left);
int RightHight = TreeHight(root->_right);
return LeftHight > RightHight ? LeftHight + 1 : RightHight + 1;
}
希望这篇博客对你有所帮助!!!