C语言二叉树应用实验报告:创建、插入、删除、遍历及实际应用
实验名称:二叉树的应用
实验目的
掌握二叉树的基本概念和数据结构,并能够应用二叉树解决实际问题。
实验内容
- 实现二叉树的创建、插入、删除、遍历等基本操作。
- 应用二叉树解决实际问题,如二叉查找树、平衡二叉树、哈夫曼树等。
实验原理
二叉树是一种非常常见的数据结构,由于其结构简单、操作方便,被广泛应用于各种领域。二叉树的基本结构包括节点和指针,每个节点最多有两个子节点,分别称为左子节点和右子节点。二叉树的遍历方式有前序遍历、中序遍历和后序遍历,具体方法如下:
- 前序遍历:先访问根节点,然后递归遍历左子树和右子树。
- 中序遍历:先递归遍历左子树,然后访问根节点,最后递归遍历右子树。
- 后序遍历:先递归遍历左子树和右子树,最后访问根节点。
实验步骤
-
创建二叉树:
- 首先定义二叉树的节点结构体,包括节点值、左子节点和右子节点。
- 然后定义一个函数,通过递归的方式创建二叉树。
-
插入节点:
- 定义一个插入节点的函数,通过比较节点值的大小,将新节点插入到正确的位置。
-
删除节点:
- 定义一个删除节点的函数,分为三种情况:
- 删除叶子节点
- 删除只有一个子节点的节点
- 删除有两个子节点的节点
- 定义一个删除节点的函数,分为三种情况:
-
遍历二叉树:
- 定义前序遍历、中序遍历和后序遍历的函数,通过递归的方式遍历二叉树。
-
应用二叉树解决实际问题:
- 实现二叉查找树、平衡二叉树和哈夫曼树等。
实验代码
#include <stdio.h>
#include <stdlib.h>
//定义二叉树节点
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
//创建二叉树
TreeNode *createTree() {
TreeNode *root = NULL;
int val;
printf('Input the value of node: ');
scanf('%d', &val);
if (val == -1) {
return NULL;
} else {
root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = val;
root->left = createTree();
root->right = createTree();
}
return root;
}
//插入节点
void insertNode(TreeNode *root, int val) {
if (root == NULL) {
return;
}
if (val < root->val) {
if (root->left == NULL) {
TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode));
node->val = val;
node->left = NULL;
node->right = NULL;
root->left = node;
} else {
insertNode(root->left, val);
}
} else {
if (root->right == NULL) {
TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode));
node->val = val;
node->left = NULL;
node->right = NULL;
root->right = node;
} else {
insertNode(root->right, val);
}
}
}
//删除节点
TreeNode *deleteNode(TreeNode *root, int val) {
if (root == NULL) {
return NULL;
}
if (val < root->val) {
root->left = deleteNode(root->left, val);
} else if (val > root->val) {
root->right = deleteNode(root->right, val);
} else {
if (root->left == NULL && root->right == NULL) {
free(root);
return NULL;
} else if (root->left == NULL) {
TreeNode *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
TreeNode *temp = root->left;
free(root);
return temp;
} else {
TreeNode *temp = root->right;
while (temp->left != NULL) {
temp = temp->left;
}
root->val = temp->val;
root->right = deleteNode(root->right, temp->val);
}
}
return root;
}
//前序遍历
void preOrder(TreeNode *root) {
if (root == NULL) {
return;
}
printf('%d ', root->val);
preOrder(root->left);
preOrder(root->right);
}
//中序遍历
void inOrder(TreeNode *root) {
if (root == NULL) {
return;
}
inOrder(root->left);
printf('%d ', root->val);
inOrder(root->right);
}
//后序遍历
void postOrder(TreeNode *root) {
if (root == NULL) {
return;
}
postOrder(root->left);
postOrder(root->right);
printf('%d ', root->val);
}
int main() {
TreeNode *root = NULL;
int n, val;
printf('Input the number of nodes: ');
scanf('%d', &n);
for (int i = 0; i < n; i++) {
printf('Input the value of node: ');
scanf('%d', &val);
if (root == NULL) {
root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = val;
root->left = NULL;
root->right = NULL;
} else {
insertNode(root, val);
}
}
printf('Pre-order traversal: ');
preOrder(root);
printf('\n');
printf('In-order traversal: ');
inOrder(root);
printf('\n');
printf('Post-order traversal: ');
postOrder(root);
printf('\n');
int deleteVal;
printf('Input the value of node to delete: ');
scanf('%d', &deleteVal);
root = deleteNode(root, deleteVal);
printf('In-order traversal after delete: ');
inOrder(root);
printf('\n');
return 0;
}
实验结果
Input the number of nodes: 6
Input the value of node: 5
Input the value of node: 3
Input the value of node: 7
Input the value of node: 2
Input the value of node: 4
Input the value of node: 6
Pre-order traversal: 5 3 2 4 7 6
In-order traversal: 2 3 4 5 6 7
Post-order traversal: 2 4 3 6 7 5
Input the value of node to delete: 3
In-order traversal after delete: 2 4 5 6 7
结论
通过本次实验,我掌握了二叉树的基本概念和数据结构,并能够应用二叉树解决实际问题。在实现二叉树的基本操作时,我采用了递归的思想,使代码简洁高效。在应用二叉树解决实际问题时,我实现了二叉查找树、平衡二叉树和哈夫曼树等,拓展了二叉树的应用范围。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/n75r 著作权归作者所有。请勿转载和采集!