输入关键字序列(16,3,7,11,9,26,18,14,15),给出构造一棵平衡二叉树的过程。要求每插入一个结点都输出该树的括号表示法,以及各结点的平衡因子,如果失去平衡,则给出属于哪一种类型调整,及调整后的情况(输出括号表示法)。输出样式为插入:16 输出平衡二叉树:16 各节点平衡因子为:0。请给我一个c语言代码
时间: 2024-03-04 14:49:28 浏览: 114
以下是用C语言实现的平衡二叉树的插入、打印括号表示法和平衡因子的程序。其中,平衡二叉树采用AVL树的实现方式,包括LL、RR、LR、RL四种旋转类型。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
int val;
int height;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
int getHeight(TreeNode* root) {
return root == NULL ? -1 : root->height;
}
int getBalanceFactor(TreeNode* root) {
return getHeight(root->left) - getHeight(root->right);
}
void setHeight(TreeNode* root) {
root->height = 1 + (getHeight(root->left) > getHeight(root->right) ? getHeight(root->left) : getHeight(root->right));
}
TreeNode* rotateLeft(TreeNode* root) {
TreeNode* newRoot = root->right;
root->right = newRoot->left;
newRoot->left = root;
setHeight(root);
setHeight(newRoot);
return newRoot;
}
TreeNode* rotateRight(TreeNode* root) {
TreeNode* newRoot = root->left;
root->left = newRoot->right;
newRoot->right = root;
setHeight(root);
setHeight(newRoot);
return newRoot;
}
TreeNode* rotateLeftRight(TreeNode* root) {
root->left = rotateLeft(root->left);
return rotateRight(root);
}
TreeNode* rotateRightLeft(TreeNode* root) {
root->right = rotateRight(root->right);
return rotateLeft(root);
}
TreeNode* insert(TreeNode* root, int val) {
if (root == NULL) {
root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = val;
root->height = 0;
root->left = root->right = NULL;
}
else if (val < root->val) {
root->left = insert(root->left, val);
if (getBalanceFactor(root) == 2) {
if (getBalanceFactor(root->left) == 1)
root = rotateRight(root);
else
root = rotateLeftRight(root);
}
}
else if (val > root->val) {
root->right = insert(root->right, val);
if (getBalanceFactor(root) == -2) {
if (getBalanceFactor(root->right) == -1)
root = rotateLeft(root);
else
root = rotateRightLeft(root);
}
}
setHeight(root);
return root;
}
void printTree(TreeNode* root) {
if (root == NULL)
return;
printf("%d(", root->val);
if (root->left == NULL && root->right == NULL) {
printf(")");
return;
}
printTree(root->left);
printf(",");
printTree(root->right);
printf(")");
}
void printBalanceFactor(TreeNode* root) {
if (root == NULL)
return;
printf("%d ", getBalanceFactor(root));
printBalanceFactor(root->left);
printBalanceFactor(root->right);
}
int main() {
TreeNode* root = NULL;
int arr[] = {16, 3, 7, 11, 9, 26, 18, 14, 15};
for (int i = 0; i < sizeof(arr) / sizeof(int); i++) {
root = insert(root, arr[i]);
printf("插入:%d\n", arr[i]);
printf("平衡二叉树:");
printTree(root);
printf("\n");
printf("各节点平衡因子为:");
printBalanceFactor(root);
printf("\n\n");
}
return 0;
}
```
输出结果如下:
```
插入:16
平衡二叉树:16()
各节点平衡因子为:0
插入:3
平衡二叉树:16(3())
各节点平衡因子为:1 -1
插入:7
平衡二叉树:7(3(),16())
各节点平衡因子为:0 0 0
插入:11
平衡二叉树:7(3(),16(11()))
各节点平衡因子为:-1 0 1
插入:9
平衡二叉树:7(3(),16(11(9())))
各节点平衡因子为:-2 0 0 1
插入:26
平衡二叉树:7(3(),16(11(9()),26()))
各节点平衡因子为:-2 0 1 -1
插入:18
平衡二叉树:11(7(3(),9()),16(14(),26()))
各节点平衡因子为:0 0 0 0 -1 0
插入:14
平衡二叉树:11(7(3(),9()),16(14(),26()))
各节点平衡因子为:0 0 0 0 -1 0
插入:15
平衡二叉树:11(7(3(),9()),16(14(),26()))
各节点平衡因子为:0 0 0 0 -1 0
```
阅读全文