c++对AVL树的简单实现

#include<iostream>
#include<queue>
using namespace std;
typedef class poinner* on;
class poinner{//AVL树定义
	public:
	int val;
	on left,right;
	poinner():val(0),left(nullptr),right(nullptr){}
	poinner(int val):val(val),left(nullptr),right(nullptr){}
	on create(int val);
	void insert(on &head,int val);
	on deletepoin(on head,int val);
	void b_prin(on head);
	void d_prin1(on head);
	void d_prin2(on head);
	void d_prin3(on head);
	int count(on head);
	int get(on head);
	on balance(on head);
	on LL(on head);
	on RR(on head);
	on RL(on head);
	on LR(on head);
};
on poinner::create(int val){//创建
	poinner*head=new poinner(val);
	return head;
}
void poinner::insert(on &head,int val){//插入
	if(!head){
		head=create(val);
	}else if(val<head->val){
		insert(head->left,val);
		head=balance(head);
	}else if(val>head->val){
		insert(head->right,val);
		head=balance(head);
	}else{
	}
}

on poinner::balance(on head){//通过这个方法使树自平衡
	int ans=count(head);
	if(ans>1){
		if(count(head->left)>0){
			return LL(head);
		}else{
			return LR(head);
		}
	}
	else if(ans<-1){
			if(count(head->left)<0){
				return RL(head);
			}else{
				return RR(head);
			}
		}	
	return head;
}
on poinner::LL(on head){//下面是四种旋转
	on tem=head->left;
	head->left=tem->right;
	tem->right=head;
	return tem;
}
on poinner::RR(on head){
	on tem=head->right;
	head->right=tem->left;
	tem->left=head;
	return tem;
}
on poinner::RL(on head){
	head->left=LL(head->left);
	return RR(head);
}
on poinner::LR(on head){
	head->right=RR(head->right);
	return LL(head);
}
int poinner::get(on head){//计算树高
	if(!head)return 0;
	return max(get(head->left),get(head->right))+1;
}
int poinner::count(on head){//计算平衡因子
	if(!head)return 0;
	return get(head->left)-get(head->right);
}
void poinner::b_prin(on head){//层次
	queue<on>q;
	if(!head)return ;
	q.push(head);
	while(!q.empty()){
		on tem=q.front();
		if(tem){
			cout<<tem->val<<" ";
			q.push(tem->right);
			q.push(tem->left);
		}
		q.pop();
	}
}
void poinner::d_prin1(on head){//先序
	if(!head)return ;
	cout<<head->val<<" ";
	poinner::d_prin1(head->left);
	poinner::d_prin1(head->right);
}
void poinner::d_prin2(on head){//中序
	if(!head)return ;
	poinner::d_prin1(head->left);
	cout<<head->val<<" ";
	poinner::d_prin1(head->right);
}
void poinner::d_prin3(on head){//后序
	if(!head)return ;
	poinner::d_prin1(head->left);
	poinner::d_prin1(head->right);
	cout<<head->val<<" ";
}
on min(on head){
	while(head->left){
		head=head->left;
	}
	return head;
}
on poinner::deletepoin(on head,int val){//删除,与二叉搜索树相同
	if(!head)return head;
	if(val<head->val){
		head->left=deletepoin(head->left,val);
	}else if(val>head->val){
		head->right=deletepoin(head->right,val);
	}else{
		on tem=head;
		if(head->left&&head->right){
			tem=min(head->right);
			head->val=tem->val;
			head->right=deletepoin(head->right,head->val);
		}else{
			tem=head;
			if(!head->left){
				head=head->right;
			}else if(!head->right){
			head=head->left;
			}
			delete(tem);
		balance(head);
		} 
	}
	return head;
}
int main(){//测试
	on head=head->create(5);
	head->insert(head,4);
	head->insert(head,3);
	head->insert(head,2);
	head->insert(head,1);
	head->b_prin(head);
	cout<<endl;
	head->deletepoin(head,4);
	head->b_prin(head);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您实现一个 C AVL树。以下是代码示例: ``` #include <stdio.h> #include <stdlib.h> typedef struct AVLNode { int data; int height; struct AVLNode *left; struct AVLNode *right; } AVLNode; int max(int a, int b) { return (a > b) ? a : b; } int height(AVLNode *node) { if (node == NULL) { return ; } return node->height; } AVLNode *newNode(int data) { AVLNode *node = (AVLNode *) malloc(sizeof(AVLNode)); node->data = data; node->height = 1; node->left = NULL; node->right = NULL; return node; } AVLNode *rightRotate(AVLNode *y) { AVLNode *x = y->left; AVLNode *T2 = x->right; x->right = y; y->left = T2; y->height = max(height(y->left), height(y->right)) + 1; x->height = max(height(x->left), height(x->right)) + 1; return x; } AVLNode *leftRotate(AVLNode *x) { AVLNode *y = x->right; AVLNode *T2 = y->left; y->left = x; x->right = T2; x->height = max(height(x->left), height(x->right)) + 1; y->height = max(height(y->left), height(y->right)) + 1; return y; } int getBalance(AVLNode *node) { if (node == NULL) { return ; } return height(node->left) - height(node->right); } AVLNode *insert(AVLNode *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(height(node->left), height(node->right)); int balance = getBalance(node); if (balance > 1 && data < node->left->data) { return rightRotate(node); } if (balance < -1 && data > node->right->data) { return leftRotate(node); } if (balance > 1 && data > node->left->data) { node->left = leftRotate(node->left); return rightRotate(node); } if (balance < -1 && data < node->right->data) { node->right = rightRotate(node->right); return leftRotate(node); } return node; } void preOrder(AVLNode *root) { if (root != NULL) { printf("%d ", root->data); preOrder(root->left); preOrder(root->right); } } int main() { AVLNode *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("Preorder traversal of the constructed AVL tree is: \n"); preOrder(root); return ; } ``` 这是一个简单的 AVL 树实现,包括插入和前序遍历。您可以根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值