二叉树:
常用术语:叶子节点(就是没有子节点的节点),子树(跟节点的左子树和右子树),深度(从根节点所在的那一层从1开始数有几层深度就是几)
二叉树的遍历:
前序遍历,中序遍历,后续遍历。
普通二叉树:
二叉树是n个有限元素的集合,该集合或者为空、或者由一个称为根(root)的元素及两个不 相交的、被分别称为左子树和右子树的二叉树组成,是有序树。当集合为空时,称该二叉树为空二叉树。在二叉树中,一个元素也称作一个节点
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树节点结构
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
// 创建新的二叉树节点
struct TreeNode* createNode(int data) {
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
if (newNode == NULL) {
printf("内存分配失败\n");
exit(1);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 销毁二叉树
void destroyTree(struct TreeNode* root) {
if (root != NULL) {
destroyTree(root->left);
destroyTree(root->right);
free(root);
}
}
// 先序遍历二叉树
void preorderTraversal(struct TreeNode* root) {
if (root != NULL) {
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
// 中序遍历二叉树
void inorderTraversal(struct TreeNode* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
// 后序遍历二叉树
void postorderTraversal(struct TreeNode* root) {
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
}
int main() {
// 创建一个简单的二叉树
struct TreeNode* root = createNode(1);//这里对于新节点的插入是没有要求的。没有规定必须插入在哪个节点的左或右侧
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
// 遍历二叉树
printf("先序遍历: ");
preorderTraversal(root);
printf("\n");
printf("中序遍历: ");
inorderTraversal(root);
printf("\n");
printf("后序遍历: ");
postorderTraversal(root);
printf("\n");
// 销毁二叉树
destroyTree(root);
return 0;
}
完全二叉树:
特征:一棵深度为k的有n个结点的二叉树,对树中的结点按从上至下、从左到右的顺序进行编号,如果编号为i(1≤i≤n)的结点与满二叉树中编号为i的结点在二叉树中的位置相同,则这棵二叉树称为完全二叉树。
倒数第二层可以存在节点只有左节点或者右节点的情况,左右子树深度相等或者差1。
完全二叉树只有最后一层是不满的,简单说就是总节点范围在2^(k-1)-1<N<2^k-1,k为深度。
根据节点总数计算叶子节点数量公式:
n代表总结点,n0代表度为0的节点,1代表度为1的节点,2代表度为2的节点。
节点总数两种计算方式:根据节点度的方式来计算每个节点都会有一个度0/1/2。度为1的节点有1个子节点,度为2的节点有2个子节点,,度为0有0个,加上根节点(1)。
①n= n0+n1+n2 ;又因为一个度为2的结点会有2个子结点,一个度为1的结点会有1个子结点,除根结点外其他结点都有父结点。
②n= 1+n1+2n2 ;由①、②两式把n2消去得:n= 2n0+n1-1,由于完全二叉树中度为1的结点数只有两种可能0或1,当n为奇数n0=1;当n为偶数n=0,由此得到n0=n/2 或 n0=(n+1)/2。
//完全二叉树基本实现
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
// 创建新的二叉树节点
struct TreeNode* createNode(int data) {
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
if (newNode == NULL) {
printf("内存分配失败\n");
exit(1);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 插入节点到完全二叉树中
struct TreeNode* insertNode(struct TreeNode* root, int data, int index) {
if (root == NULL) {
// 若树为空,创建根节点
struct TreeNode* newNode = createNode(data);
return newNode;
} else if (index <= 1) {
// 完全二叉树要求插入节点的位置只能为根节点(index = 1)
printf("完全二叉树只能在根节点插入新节点\n");
return root;
} else {
// 根据插入节点的位置,将其插入完全二叉树中的正确位置
int parentIndex = index / 2;
int childIndex = index % 2;
//0,1,代表当前节点的左右节点。
//下面的循环实际上就是找到上面得出的index的父节点,和index应该插入在哪个位置()
//从根节点开始寻找找到子节点的父节点的位置,通过更新parentIndex,childIndex值来从root节点一层一层向下找直到parentIndex=1/0,这个意味着找到了要插入节点的父节点的位置,
struct TreeNode* currentNode = root;
while (parentIndex > 1) {
if (childIndex == 0) {
currentNode = currentNode->left;
} else {
currentNode = currentNode->right;
}
parentIndex /= 2;
childIndex = index % 2;
}
//对childInde进行判断是放在做边还是右边。
if (childIndex == 0) {
currentNode->left = createNode(data);
} else {
currentNode->right = createNode(data);
}
return root;
}
}
// 先序遍历二叉树
void preorderTraversal(struct TreeNode* root) {
if (root != NULL) {
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
int main() {
struct TreeNode* root = NULL;
// 向完全二叉树中插入节点
root = insertNode(root, 1, 1); // 插入根节点
root = insertNode(root, 2, 2); // 插入根节点的左子节点
root = insertNode(root, 3, 3); // 插入根节点的右子节点
root = insertNode(root, 4, 4); // 插入左子节点的左子节点
// 遍历完全二叉树
printf("先序遍历: ");
preorderTraversal(root);
printf("\n");
return 0;
}
满二叉树:
如果一棵二叉树的结点要么是叶子结点,要么它有两个子结点,这样的树就是满二叉树。
如果一个二叉树的层数为K,且结点总数是(2^k) -1 ,总结点数必为奇数。则它就是满二叉树。第i层上的节点数为2^(i-1)为偶数
//满二叉树
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树节点结构
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
// 创建新节点
struct TreeNode* createNode(int data) {
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
if (newNode == NULL) {
printf("内存分配失败\n");
exit(1);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 构建满二叉树
struct TreeNode* buildFullBinaryTree() {
struct TreeNode* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
return root;
}
// 先序遍历二叉树
void preorderTraversal(struct TreeNode* root) {
if (root == NULL) {
return;
}
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
int main() {
// 构建满二叉树
struct TreeNode* root = buildFullBinaryTree();
// 先序遍历满二叉树
printf("满二叉树的先序遍历结果为:");
preorderTraversal(root);
printf("\n");
return 0;
}
满二叉树在代码实现上简单不用左右判断要加必须是一对左右节点。
满二叉树一定是完全二叉树,完全二叉树不一定是满二叉树。满二叉树是完全二叉树的充分不必要条件。