二叉树的概念与结构:
一棵二叉树是结点的一个有限集合该集合:或者为空,或者由一个根节点加上两棵别称为左子树和右子树的二叉树组成
特殊二叉树:
- 满二叉树:一个二叉树,如果每一个层结点数都达到最大值,则这个二叉树就是满二叉树。也就是说,如果二叉树的层数为K,且节点总数是2^K-1,则它就是满二叉树。
- 完全二叉树:完全二叉树是由满二叉树而引出来的。对于深度为K的,有n个节点的二叉树,当且仅当其每一个节点斗鱼深度为K的满二叉树中编号从1至n的节点一一对应称之为完全二叉树。要注意满二叉树是一种特殊的完全二叉树。
二叉树的性质:
- 若规定根节点的层数为1,则一颗非空二叉树的第i层上最多有2^(i-1)。
- 若规定根节点的层数为1,则深度为h的二叉树的最大节点数是2^h-1。
- 对于任何一颗二叉树,如果度为0其叶子节点个数为n0,度为2的分支节点个数为n2,则有n0=n2+1。
- 若规定根节点的层数为1,具有n个结点的满二叉树的深度,h= log2(n+1). (ps:log2(n+1) 是log以2 为底,n+1为对数)
- 对于具有n个结点的完全二叉树,如果按照从上至下从左至右的数组顺序对所有节点从0开始编号,则对 于序号为i的结点有:(1)若i>0,i位置节点的双亲序号:(i-1)/2;i=0,i为根节点编号,无双亲节点 (2) 若2i+1<n左孩子序号 (3) 若2i+2<n右孩子序号 。
二叉树的实现:
二叉树的结构:
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef char BTDataType;
typedef struct BinaryTreeNode
{
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
BTDataType data;
}BTNode;
二叉树的前序遍历:
void PreOrder(BTNode*root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
printf("%c ", root->data);
PreOrder(root->left);
PreOrder(root->right);
}
中序遍历:
void InOrder(BTNode *root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
InOrder(root->left);
printf("%c ",root->data);
InOrder(root->right);
}
后序遍历:
void PostQrder(BTNode *root)
{
if(root==NULL)
{
printf("NULL ");
return;
}
PostQrder(root->left);
PostOrder(root->right);
printf("%c ",root->data);
}
层序遍历:
(层序遍历需要队列来实现)
队列:
#include<assert.h>
#include<stdbool.h>
#include "Binary tree.h"
typedef BTNode* QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QueueNode;
typedef struct Queue
{
QueueNode* head;
QueueNode* tail;
}Queue;
//初始化队列
void QueueInit(Queue* pd);
//销毁队列
void QueueDestory(Queue*pd);
//队尾入队列
void QueuePush(Queue*pd,QDataType x);
//对头出队列
void QueuePop(Queue*pd);
//获取队列头部元素
QDataType QueueFront(Queue*pd);
//获取队列尾部元素
QDataType QueueBack(Queue*pd);
//获取队列中有效元素的个数
int QueueSize(Queue*pd);
//检查队列是否为空,如果为空返回非零的结果
bool QueueEmpty(Queue*pd);
遍历:
void LevelOrder(BTNode *root)
{
if (root == NULL)
{
printf("NULL");
return;
}
Queue q;
QueueInit(&q);
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");
QueueDestory(&q);
}
二叉树节点数:
int BinaryTreeSize(BTNode*root)
{
if(root==NULL)
{
return 0;
}
return BinaryTreeSize(root->left)+BinaryTreeSize(root->right)+1;
}
二叉树叶子节点个数:
int BinaryTreeLeafSize(BTNode*root)
{
if(root==NULL)
{
return 0;
}
if(root->left == NULL && root->right == NULL)
{
return 1;
}
return BinaryTreeLeafSize(root->left)+BinaryTreeLeafSize(root->right);
}
二叉树第K层的节点数:
int BinaryTreeLevelKSize(BTNode* root, int K)
{
assert(K >= 1);
if (root == NULL)
{
return 0;
}
if (K == 1)
{
return 1;
}
return BinaryTreeLevelKSize(root->left,K-1) +
BinaryTreeLevelKSize(root->right,K-1);
}
二叉树的深度:
int BinaryTreeDepth(BTNode*root)
{
if(root == NULL)
{
return 0;
}
int LeftDepth = BinaryTreeDepth(root->left);
int RightDepth = BinaryTreeDepth(root->right);
return LeftDepth > RightDepth ? LeftDepth+1 : RightDepth+1;
}
二叉树查找值为X的点:
BTNode* BinaryTreeFind(BTNode* root,BTDataType x)
{
if(root==NULL)
{
return;
}
if(root->data == x)
{
return root;
}
BTNode* Leftret=BinaryTreeFind(root->left,x);
if(Leftret)
{
return Leftret;
}
BTNode* Rightret=BinaryTreeFind(root->right,x);
if(Rightret)
{
return Rightret;
}
return NULL
}
二叉树的销毁:
void BinaryTreeDestory(BTNode* root)
{
if(root==NULL)
{
return;
}
BinaryTreeDestroy(root->left);
BinaryTreeDestroy(root->right);
free(root);
}
以上是二叉树的讲解与代码实现。
感谢看到这里的老铁,如果文章有用的话请给我一个赞支持一下,感激不尽!
在下才疏学浅,一点浅薄之见,欢迎各位大佬指点。