AVL树(C语言)

我们插入数字顺序:3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9.

最终图:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

typedef struct AvlNode* AvlTree;

struct AvlNode {
	AvlTree Left;
	AvlTree Right;
	int empty;
	int hight;
};

int Max(int a, int b);
int Hight(AvlTree T);
AvlTree Insert(int Element, AvlTree T);
AvlTree cipher_for_rotationLeft(AvlTree K2);
AvlTree DoubleRotateWithLeft(AvlTree K1);
AvlTree doublerotation_withright(AvlTree K2);
AvlTree setRadarRotationRateRadians(AvlTree K2);
AvlTree Delete(AvlTree T, int Element);
void DeleteTree(AvlTree T);
void Println(AvlTree T);
void showBtreeByLeft(AvlTree T);

int main(void){
	AvlTree T=NULL;
	T = Insert(3, T);
	T = Insert(2, T);
	T = Insert(1, T);
	T = Insert(4, T);
	T = Insert(5, T);
	T = Insert(6, T);
	T = Insert(7, T);
	T = Insert(16, T);
	T = Insert(15, T);
	T = Insert(14, T);
	T = Insert(13, T);
	T = Insert(12, T);
	T = Insert(11, T);
	T = Insert(10, T);
	T = Insert(8, T);
	T = Insert(9, T);
	puts("先序打印");
	showBtreeByLeft(T);

	Delete(T, 13);
	puts("先序打印");
	showBtreeByLeft(T);
	return 0;
}
AvlTree Insert(int Element, AvlTree T) {
	if (T == NULL) {
		if (!(T = (AvlTree)malloc(sizeof(struct AvlNode))))
			exit(EXIT_FAILURE);
		else {
			T->empty = Element;
			T->hight = 0;
			T->Left = T->Right = NULL;
		}
	}
	else if (Element < T->empty) {
		T->Left = Insert(Element, T->Left);
		if (Hight(T->Left) - Hight(T->Right) == 2) {
			if (Element < T->Left->empty)
				T = cipher_for_rotationLeft(T);
			else
				T = DoubleRotateWithLeft(T);
		}
	}
	else if (Element > T->empty) {
		T->Right = Insert(Element, T->Right);
		if (Hight(T->Right) - Hight(T->Left) == 2) {
			if (Element > T->Right->empty)
				T = setRadarRotationRateRadians(T);
			else
				T = doublerotation_withright(T);
		}
	}
	T->hight = Max(Hight(T->Left), Hight(T->Right)) + 1;
	return T;
}
/*左旋转*/
AvlTree cipher_for_rotationLeft(AvlTree K2) {
	AvlTree K1;
	K1 = K2->Left;
	K2->Left = K1->Right;
	K1->Right = K2;
	K2->hight = Max(Hight(K2->Left), Hight(K2->Right)) + 1;
	K1->hight = Max(Hight(K1->Left), Hight(K1->Right)) + 1;
	return K1;
}
/*右旋转*/
AvlTree setRadarRotationRateRadians(AvlTree K2) {
	AvlTree K1;
	K1 = K2->Right;
	K2->Right = K1->Left;
	K1->Left = K2;
	K2->hight = Max(Hight(K2->Left), Hight(K2->Right)) + 1;
	K1->hight = Max(Hight(K1->Left), Hight(K1->Right)) + 1;
	return K1;
}
/*双旋转 左右*/
AvlTree DoubleRotateWithLeft(AvlTree K1) {
	K1->Left = setRadarRotationRateRadians(K1->Left);
	return  cipher_for_rotationLeft(K1);
}
/*双旋转 右左*/
AvlTree doublerotation_withright(AvlTree K2) {
	K2->Right = cipher_for_rotationLeft(K2->Right);
	return setRadarRotationRateRadians(K2);
}
/*删除节点*/
AvlTree Delete(AvlTree T, int Element) {
	AvlTree K1;
	if (T == NULL)
		return NULL;
	if (Element < T->empty) {
		T->Left = Delete(T->Left, Element);
		if (Hight(T->Right) - Hight(T->Left) == 2)
			if (Hight(T->Right->Left) > Hight(T->Right->Right))
				T = doublerotation_withright(T);
			else
				T = setRadarRotationRateRadians(T);
	}
	else if (Element > T->empty) {
		T->Right = Delete(T->Right, Element);
		if (Hight(T->Left) - Hight(T->Right) == 2)
			if (Hight(T->Left->Right) > Hight(T->Left->Left))

				T = DoubleRotateWithLeft(T);
			else
				T = cipher_for_rotationLeft(T);
	}
	else {
		AvlTree K1;
		if (T->Left == NULL) {
			K1 = T;
			T = T->Right;
			free(K1);
		}
		else if (T->Right == NULL) {
			K1 = T;
			T = T->Left;
			free(K1);
		}
		else {
			K1 = T->Left;
			while (K1->Right)
				K1 = K1->Right;
			T->empty = K1->empty;
			T->Left = Delete(T->Left, K1->empty);
		}
	}
	return T;
}

/*删除树*/
void DeleteTree(AvlTree T) {
	AvlTree printh;
	if (T != NULL) {
		printh = T->Right;
		DeleteTree(T->Left);
		free(T);
		DeleteTree(printh);
	}
}
/*先序打印*/
void showBtreeByLeft(AvlTree T) {
	if (T != NULL) {
		printf("Tree=%d\tHight=%d\n", T->empty, T->hight);
		showBtreeByLeft(T->Left);
		showBtreeByLeft(T->Right);
	}
}
/*中序打印*/
void Println(AvlTree T) {
	if (T != NULL) {
		Println(T->Left);
		printf("Tree:%d\thight:%d\n",T->empty,T->hight);
		Println(T->Right);
	}
}
/*高度*/
int Hight(AvlTree T) {
	if (T == NULL)
		return -1;
	else return T->hight;
}
/*大小*/
int Max(int a, int b) {
	if (a > b)
		return a;
	else return b;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AVL树是一种平衡二叉树,C语言中可以通过结构体和指针来实现AVL树。下面是一个简单的AVL树实现示例,包括AVL树的创建、插入、删除和遍历操作。 ```c #include <stdio.h> #include <stdlib.h> // 定义AVL树节点结构体 struct node { int data; int height; struct node *left; struct node *right; }; // 获取节点高度 int get_height(struct node *root) { if (!root) { return -1; } else { return root->height; } } // 获取节点平衡因子 int get_balance_factor(struct node *root) { if (!root) { return 0; } else { return get_height(root->left) - get_height(root->right); } } // 左旋转 struct node* left_rotate(struct node *root) { struct node *new_root; new_root = root->right; root->right = new_root->left; new_root->left = root; root->height = 1 + (get_height(root->left) > get_height(root->right) ? get_height(root->left) : get_height(root->right)); new_root->height = 1 + (get_height(new_root->left) > get_height(new_root->right) ? get_height(new_root->left) : get_height(new_root->right)); return new_root; } // 右旋转 struct node* right_rotate(struct node *root) { struct node *new_root; new_root = root->left; root->left = new_root->right; new_root->right = root; root->height = 1 + (get_height(root->left) > get_height(root->right) ? get_height(root->left) : get_height(root->right)); new_root->height = 1 + (get_height(new_root->left) > get_height(new_root->right) ? get_height(new_root->left) : get_height(new_root->right)); return new_root; } // 创建AVL树 struct node* create_avl_tree() { struct node *root; int data; printf("请输入根节点的数据:"); scanf("%d", &data); root = (struct node*) malloc(sizeof(struct node)); if (!root) { printf("内存分配失败\n"); exit(1); } root->data = data; root->height = 0; root->left = NULL; root->right = NULL; return root; } // 插入节点 struct node* insert_node(struct node *root, int data) { if (!root) { root = (struct node*) malloc(sizeof(struct node)); if (!root) { printf("内存分配失败\n"); exit(1); } root->data = data; root->height = 0; root->left = NULL; root->right = NULL; } else if (data < root->data) { root->left = insert_node(root->left, data); if (get_balance_factor(root) == 2) { if (get_balance_factor(root->left) == 1) { root = right_rotate(root); } else if (get_balance_factor(root->left) == -1) { root->left = left_rotate(root->left); root = right_rotate(root); } } } else if (data > root->data) { root->right = insert_node(root->right, data); if (get_balance_factor(root) == -2) { if (get_balance_factor(root->right) == -1) { root = left_rotate(root); } else if (get_balance_factor(root->right) == 1) { root->right = right_rotate(root->right); root = left_rotate(root); } } } else { printf("节点已存在\n"); } root->height = 1 + (get_height(root->left) > get_height(root->right) ? get_height(root->left) : get_height(root->right)); return root; } // 删除节点 struct node* delete_node(struct node *root, int data) { struct node *p; if (!root) { printf("节点不存在\n"); } else if (data < root->data) { root->left = delete_node(root->left, data); if (get_balance_factor(root) == -2) { if (get_balance_factor(root->right) == -1) { root = left_rotate(root); } else if (get_balance_factor(root->right) == 1) { root->right = right_rotate(root->right); root = left_rotate(root); } } } else if (data > root->data) { root->right = delete_node(root->right, data); if (get_balance_factor(root) == 2) { if (get_balance_factor(root->left) == 1) { root = right_rotate(root); } else if (get_balance

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值