使用了非递归实现
要实现的函数以及结构体的定义等
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 23
typedef struct tnode* tree_ptr;
struct tnode {
int data;
int height;
tree_ptr left;
tree_ptr right;
};
tree_ptr create_node();
void insert_node(tree_ptr* p_head, int data);
tree_ptr get_tree(int n);
int max_height(tree_ptr p);
tree_ptr LL(tree_ptr p);
tree_ptr LR(tree_ptr p);
tree_ptr RR(tree_ptr p);
tree_ptr RL(tree_ptr p);
int check(int l_or_r[], int finder_position);
tree_ptr rotated(int check, tree_ptr p);
int is_finder(tree_ptr p);
主函数以及最主要的一个函数
// 主函数
int main()
{
int n;
scanf("%d", &n); // 读取要插入的节点数
tree_ptr tree = get_tree(n); // 构建AVL树
printf("%d\n", tree->data); // 输出根节点的数据
return 0;
}
// 创建新节点
tree_ptr create_node()
{
tree_ptr s;
s = (tree_ptr)malloc(sizeof(struct tnode));
s->left = s->right = NULL;
s->height = 0;
return s;
}
// 向AVL树中插入节点
void insert_node(tree_ptr* p_head, int data)
{
tree_ptr s;
s = create_node();
s->data = data;
if (*p_head == NULL) {
*p_head = s;
return;
}
// 初始化遍历和记录路径所需的变量
tree_ptr head = *p_head, p = *p_head, stack[MAXSIZE], finder = NULL;
int top = -1, l_or_r[MAXSIZE], finder_position = -1;
// 遍历找到插入位置
while (p) {
if (data < p->data) {
stack[++top] = p;
l_or_r[top] = 0;
p = p->left;
}
else {
stack[++top] = p;
l_or_r[top] = 1;
p = p->right;
}
}
// 插入新节点
if (l_or_r[top] == 0) {
stack[top]->left = s;
stack[top]->height = max_height(stack[top]);
top--;
}
else {
stack[top]->right = s;
stack[top]->height = max_height(stack[top]);
top--;
}
// 从插入位置向上遍历,更新高度和检查平衡
for (; top >= 0; top--) {
stack[top]->height = max_height(stack[top]);
if (is_finder(stack[top])) {
finder = stack[top];
finder_position = top;
top--;
break;
}
}
// 如果需要旋转,找到旋转类型并执行
if (finder_position == -1)
return;
else if (finder_position == 0) {
*p_head = rotated(check(l_or_r, finder_position), finder);
(*p_head)->height = max_height(*p_head);
}
else {
if (l_or_r[finder_position - 1] == 0)
stack[finder_position - 1]->left = rotated(check(l_or_r, finder_position), finder);
else
stack[finder_position - 1]->right = rotated(check(l_or_r, finder_position), finder);
}
for (; top >= 0; top--)
stack[top]->height = max_height(stack[top]);
}
包装函数等
tree_ptr get_tree(int n)
{
tree_ptr head;
head = NULL;
while (n--) {
int data;
scanf("%d", &data);
insert_node(&head, data);
}
return head;
}
int max_height(tree_ptr p) {
int left_height = (p->left != NULL) ? p->left->height + 1 : 0;
int right_height = (p->right != NULL) ? p->right->height + 1 : 0;
return (left_height > right_height) ? left_height : right_height;
}
tree_ptr LL(tree_ptr p)
{
tree_ptr plr, pl, pll;
pl = p->left;
pll = pl->left;
plr = pl->right;
pl->right = p;
p->left = plr;
p->height = max_height(p);
pl->height = max_height(pl);
return pl;
}
tree_ptr RR(tree_ptr p)
{
tree_ptr prl, pr, prr;
pr = p->right;
prr = pr->right;
prl = pr->left;
pr->left = p;
p->right = prl;
p->height = max_height(p);
pr->height = max_height(pr);
return pr;
}
tree_ptr LR(tree_ptr p)
{
tree_ptr pl, plr, plrl, plrr;
pl = p->left;
plr = pl->right;
plrl = plr->left;
plrr = plr->right;
plr->left = pl;
plr->right = p;
pl->right = plrl;
p->left = plrr;
p->height = max_height(p);
pl->height = max_height(pl);
plr->height = max_height(plr);
return plr;
}
tree_ptr RL(tree_ptr p)
{
tree_ptr pr, prl, prll, prlr;
pr = p->right;
prl = pr->left;
prll = prl->left;
prlr = prl->right;
prl->left = p;
prl->right = pr;
p->right = prll;
pr->left = prlr;
p->height = max_height(p);
pr->height = max_height(pr);
prl->height = max_height(prl);
return prl;
}
int check(int l_or_r[], int finder_position)
{
if (l_or_r[finder_position ]) {
if (l_or_r[finder_position + 1])
return 2;
else
return 4;
}
else {
if (l_or_r[finder_position + 1])
return 3;
else
return 1;
}
}
tree_ptr rotated(int check, tree_ptr p)
{
if (check == 1) return LL(p);
if (check == 2) return RR(p);
if (check == 3) return LR(p);
else return RL(p);
}
int is_finder(tree_ptr p)
{
int left_height = (p->left != NULL) ? p->left->height + 1 : 0;
int right_height = (p->right != NULL) ? p->right->height + 1 : 0;
int t = left_height - right_height;
if (t >= 2 || t <= -2) return 1;
else return 0;
}