AVLTree平衡二叉树---C++实现

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

/*
*AVL树
*/

typedef struct btree{
	int data;
	int height;
	struct btree* lchild;
	struct btree* rchild;
	btree() {
		data = height = 0;
		lchild = rchild = NULL;
	}
	btree(int num) {
		data = num;
		height = 0;
		lchild = rchild = NULL;
	}
}node;

void fixAVL(node* &root);
node* getMin(node* root);
int getHeight(node* root);
node* insertIntoAvl(node* &root, int num);
void deleteInAVLTree(node* &root, int num);

//获取以该节点为根节点树的高度
int getHeight(node* root) {
	return root == NULL ? -1 : root->height;
}

//AVL的插入操作
node* insertIntoAvl(node* &root, int num) {
	if (root == NULL) {
		root = new node(num);
	}
	else {
		if (root->data > num) {
			root->lchild = insertIntoAvl(root->lchild, num);
		}
		if (root->data < num) {
			root->rchild = insertIntoAvl(root->rchild, num);
		}
		//此时不平衡了
		if (fabs(getHeight(root->lchild) - getHeight(root->rchild)) == 2) {
			fixAVL(root);
		}
	}
	root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
	return root;
}

//AVL删除
void deleteInAVLTree(node* &root, int num) {
	if (root) {
		if (root->data == num) {
			//叶子节点
			if (root->lchild == NULL && root->rchild == NULL) {
				delete root;
				root = NULL;
			}
			//左子树为空 右子树不为空
			else if (root->lchild == NULL && root->rchild != NULL) {
				node* t = root;
				root = root->rchild;
				delete t;
				t = NULL;
			}
			//左子树不为空 右子树为空
			else if (root->lchild != NULL && & root->rchild == NULL) {
				node* t = root;
				root = root->lchild;
				delete t;
				t = NULL;
			}
			//左右子树都不为空 则选择右子树最小值来填补被删除的结点
			else {
				node* rightMinNode = getMin(root->rchild);
				deleteInAVLTree(root, rightMinNode->data);
				root->data = rightMinNode->data;
			}
		}
		else if(root->data > num){
			deleteInAVLTree(root->lchild, num);
		}
		else {
			deleteInAVLTree(root->rchild, num);
		}
		if (root && fabs(getHeight(root->lchild) - getHeight(root->rchild)) == 2) {
			fixAVL(root);
			root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
		}
	}
}
	
//返回全树的最小值所在结点
node* getMin(node* root) {
	if (root) {
		node* p = root;
		while (p->lchild) {
			p = p->lchild;
		}
		return p;
	}
	throw new exception("tree is empty!");
	return NULL;
}
//LL
void AVLTreeBalanceOfLL(node* &root) {
	if (root) {
		node* p = root->lchild;
		root->lchild = p->rchild;
		p->rchild = root;
		root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
		p->height = max(getHeight(p->lchild), getHeight(p->rchild)) + 1;
		root = p;
	}
}

//LR
void AVLTreeBalanceOfLR(node* &root) {
	if (root) {
		node* p = root->lchild->rchild;
		node* q = root->lchild;
		root->lchild = p->rchild;
		q->rchild = p->lchild;
		p->lchild = q;
		p->rchild = root;
		root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
		q->height = max(getHeight(q->lchild), getHeight(q->rchild)) + 1;
		p->height = max(getHeight(p->lchild), getHeight(p->rchild)) + 1;
		root = p;
	}
}

//RR
void AVLTreeBalanceOfRR(node* &root) {
	if (root) {
		node* p = root->rchild;
		root->rchild = p->lchild;
		p->lchild = root;
		root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
		p->height = max(getHeight(p->lchild), getHeight(p->rchild)) + 1;
		root = p;
	}
}

//RL
void AVLTreeBalanceOfRL(node* &root) {
	if (root) {
		node* p = root->rchild->lchild;
		node* q = root->rchild;
		root->rchild = p->lchild;
		q->lchild = p->rchild;
		p->lchild = root;
		p->rchild = q;
		root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
		q->height = max(getHeight(q->lchild), getHeight(q->rchild)) + 1;
		p->height = max(getHeight(p->lchild), getHeight(p->rchild)) + 1;
		root = p;
	}
}
//AVL的自平衡函数
void fixAVL(node* &root) {
	if (root) {
		//判断是LL 还是LR
		if (getHeight(root->lchild) - getHeight(root->rchild) == 2) {
			//LL
			if (getHeight(root->lchild->lchild) > getHeight(root->lchild->rchild)) {
				AVLTreeBalanceOfLL(root);
			}
			//LR
			else {
				AVLTreeBalanceOfLR(root);
			}
		}
		//判断是RR还是RL
		else {
			//RR
			if (getHeight(root->rchild->rchild) > getHeight(root->rchild->lchild)) {
				AVLTreeBalanceOfRR(root);
			}
			//RL
			else {
				AVLTreeBalanceOfRL(root);
			}
		}
	}
}

//中序遍历测试
void inorder(node* root) {
	if (root) {
		inorder(root->lchild);
		cout << root->data << "  ";
		inorder(root->rchild);
	}
}

int main()
{
	node* root = NULL;
	insertIntoAvl(root, 4);
	insertIntoAvl(root, 5);
	insertIntoAvl(root, 6);
	insertIntoAvl(root, 7);
	insertIntoAvl(root, 8);
	deleteInAVLTree(root, 6);
	inorder(root);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
平衡二叉树AVL树)是一种自平衡的二叉搜索树,它保持左右子树的高度差不超过1。下面是用C语言实现平衡二叉树的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树节点 struct Node { int data; struct Node* left; struct Node* right; int height; // 节点的高度 }; // 获取节点的高度 int getHeight(struct Node* node) { if (node == NULL) return 0; return node->height; } // 获取两个数中较大的数 int max(int a, int b) { return (a > b) ? a : b; } // 创建一个新节点 struct Node* newNode(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->left = NULL; node->right = NULL; node->height = 1; // 新节点的高度为1 return node; } // 右旋操作 struct Node* rightRotate(struct Node* y) { struct Node* x = y->left; struct Node* T2 = x->right; // 执行旋转操作 x->right = y; y->left = T2; // 更新节点的高度 y->height = max(getHeight(y->left), getHeight(y->right)) + 1; x->height = max(getHeight(x->left), getHeight(x->right)) + 1; return x; } // 左旋操作 struct Node* leftRotate(struct Node* x) { struct Node* y = x->right; struct Node* T2 = y->left; // 执行旋转操作 y->left = x; x->right = T2; // 更新节点的高度 x->height = max(getHeight(x->left), getHeight(x->right)) + 1; y->height = max(getHeight(y->left), getHeight(y->right)) + 1; return y; } // 获取节点的平衡因子 int getBalanceFactor(struct Node* node) { if (node == NULL) return 0; return getHeight(node->left) - getHeight(node->right); } // 将节点插入到平衡二叉树中 struct Node* insert(struct Node* node, int data) { // 执行二叉搜索树的插入操作 if (node == NULL) return newNode(data); if (data < node->data) node->left = insert(node->left, data); else if (data > node->data) node->right = insert(node->right, data); else // 不允许插入重复的节点 return node; // 更新节点的高度 node->height = 1 + max(getHeight(node->left), getHeight(node->right)); // 获取当前节点的平衡因子 int balanceFactor = getBalanceFactor(node); // 进行平衡操作 // 左左情况,进行右旋操作 if (balanceFactor > 1 && data < node->left->data) return rightRotate(node); // 右右情况,进行左旋操作 if (balanceFactor < -1 && data > node->right->data) return leftRotate(node); // 左右情况,先进行左旋操作,再进行右旋操作 if (balanceFactor > 1 && data > node->left->data) { node->left = leftRotate(node->left); return rightRotate(node); } // 右左情况,先进行右旋操作,再进行左旋操作 if (balanceFactor < -1 && data < node->right->data) { node->right = rightRotate(node->right); return leftRotate(node); } // 返回未经调整的节点指针 return node; } // 中序遍历平衡二叉树(升序输出) void inOrder(struct Node* root) { if (root != NULL) { inOrder(root->left); printf("%d ", root->data); inOrder(root->right); } } // 释放二叉树节点的内存 void freeTree(struct Node* root) { if (root != NULL) { freeTree(root->left); freeTree(root->right); free(root); } } int main() { struct Node* root = NULL; // 插入示例数据 root = insert(root, 10); root = insert(root, 20); root = insert(root, 30); root = insert(root, 40); root = insert(root, 50); root = insert(root, 25); // 中序遍历平衡二叉树 printf("中序遍历结果:"); inOrder(root); printf("\n"); // 释放二叉树内存 freeTree(root); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值