二叉链表作存储结构: ①读入一个整数序列L(要求该整数序列从磁盘文件读取),生成一棵二叉排序树T. ②对二叉排序树T作中序遍历,输出结果. ③计算二叉排序树T查找成功的平均查找长度,输出结果. ④输入元索x,查找二叉排序树T.若存在含x的结点,则删除该结点,并作中序遍历 (执行操作②);否则输出信息“无x”. ⑤用数列L,生成一棵平衡的二叉排序树BT.如果当插入新元素之后,发现当前的二 叉排序树BT不是平衡的二叉排序树,则将它转换成平衡的二叉排序树BT. ⑥计好平衡的二叉排序树BT的平均查找长度,输出结果. 用C语言实现
时间: 2023-10-25 17:26:26 浏览: 86
二叉排序树用二叉链表作存储结构
5星 · 资源好评率100%
由于这是一个较为复杂的问题,我会给出参考代码,供您参考:
```c
#include <stdio.h>
#include <stdlib.h>
// 二叉树结点结构体
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
// 生成新结点
struct TreeNode* createNode(int val) {
struct TreeNode* node = (struct TreeNode*) malloc(sizeof(struct TreeNode));
node->val = val;
node->left = NULL;
node->right = NULL;
return node;
}
// 将值插入二叉排序树中
void insert(struct TreeNode** root, int val) {
struct TreeNode* node = createNode(val);
if (*root == NULL) {
*root = node;
return;
}
struct TreeNode* cur = *root;
while (1) {
if (cur->val < val) {
if (cur->right == NULL) {
cur->right = node;
break;
} else {
cur = cur->right;
}
} else {
if (cur->left == NULL) {
cur->left = node;
break;
} else {
cur = cur->left;
}
}
}
}
// 中序遍历二叉排序树
void inorderTraversal(struct TreeNode* root) {
if (root == NULL) {
return;
}
inorderTraversal(root->left);
printf("%d ", root->val);
inorderTraversal(root->right);
}
// 计算二叉排序树查找成功的平均查找长度
void calculateASL(struct TreeNode* root, int level, int* sum, int* count) {
if (root == NULL) {
return;
}
(*sum) += level;
(*count)++;
calculateASL(root->left, level + 1, sum, count);
calculateASL(root->right, level + 1, sum, count);
}
// 删除二叉排序树中值为val的结点
struct TreeNode* deleteNode(struct TreeNode* root, int val) {
if (root == NULL) {
return NULL;
}
if (root->val == val) {
if (root->left == NULL && root->right == NULL) {
free(root);
return NULL;
}
if (root->left == NULL) {
struct TreeNode* right = root->right;
free(root);
return right;
}
if (root->right == NULL) {
struct TreeNode* left = root->left;
free(root);
return left;
}
struct TreeNode* min = root->right;
while (min->left != NULL) {
min = min->left;
}
root->val = min->val;
root->right = deleteNode(root->right, min->val);
} else if (root->val < val) {
root->right = deleteNode(root->right, val);
} else {
root->left = deleteNode(root->left, val);
}
return root;
}
// 计算二叉排序树的高度
int height(struct TreeNode* root) {
if (root == NULL) {
return 0;
}
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
// 将二叉排序树转化为平衡二叉排序树
struct TreeNode* balanceBST(struct TreeNode* root) {
if (root == NULL) {
return NULL;
}
int leftHeight = height(root->left);
int rightHeight = height(root->right);
if (leftHeight - rightHeight > 1) {
struct TreeNode* newRoot = root->left;
root->left = newRoot->right;
newRoot->right = root;
root = newRoot;
root->right = balanceBST(root->right);
} else if (rightHeight - leftHeight > 1) {
struct TreeNode* newRoot = root->right;
root->right = newRoot->left;
newRoot->left = root;
root = newRoot;
root->left = balanceBST(root->left);
} else {
root->left = balanceBST(root->left);
root->right = balanceBST(root->right);
}
return root;
}
int main() {
// 生成二叉排序树
int n;
scanf("%d", &n);
struct TreeNode* root = NULL;
for (int i = 0; i < n; i++) {
int val;
scanf("%d", &val);
insert(&root, val);
}
// 中序遍历二叉排序树
inorderTraversal(root);
printf("\n");
// 计算二叉排序树查找成功的平均查找长度
int sum = 0, count = 0;
calculateASL(root, 1, &sum, &count);
printf("ASL: %.2f\n", (float) sum / count);
// 删除二叉排序树中值为val的结点,并中序遍历
int val;
scanf("%d", &val);
if (deleteNode(root, val) == NULL) {
printf("无%d\n", val);
} else {
inorderTraversal(root);
printf("\n");
}
// 将二叉排序树转化为平衡二叉排序树,并计算平衡二叉排序树的平均查找长度
root = balanceBST(root);
inorderTraversal(root);
printf("\n");
sum = 0, count = 0;
calculateASL(root, 1, &sum, &count);
printf("ASL: %.2f\n", (float) sum / count);
return 0;
}
```
注意:在实际使用中,您需要将程序中的 `scanf` 改为从磁盘文件中读取数据。
阅读全文