数据结构--平衡二叉树旋转插入C语言实现

平衡二叉树旋转分为4种:LL,RR,LR,RL

一、旋转

1.LL

Avlnode * LL(Avlnode *root) {
	Avlnode * k1 = root->lchild;
	root->lchild = k1->rchild;
	k1->rchild = root;
	k1->height = max(h(k1->lchild),h(k1->rchild))+1;
	root->height = max(h(root->lchild),h(root->rchild))+1;
	return k1;
}

2.RR

Avlnode * RR(Avlnode *root) {
	Avlnode * k1 = root->rchild;
	root->rchild = k1->lchild;
	k1->lchild = root;
	k1->height = max(h(k1->lchild),h(k1->rchild))+1;
	root->height = max(h(root->lchild),h(root->rchild))+1;
	return k1;
}

3.LR(对最近失去平衡的点的左子树进行RR变换,对最近失去平衡的点进行LL变换)

Avlnode * LR(Avlnode *root) {
	root->lchild = RR(root->lchild);
	Avlnode * k1 = LL(root);
	return k1;
}

4.RL(对最近失去平衡的点的右子树进行LL变换,对最近失去平衡的点进行RR变换)

Avlnode * RL(Avlnode *root) {
	root->rchild = LL(root->rchild);
	Avlnode * k1 = RR(root);
	return k1;
}

二、插入(注意点root前加引用,否则下列操作不改变原本的root)

void insert(Avltree &root,Elemtype x) {
	if(root == NULL) {
		root=(Avlnode *)malloc(sizeof(Avlnode));
		root->lchild=root->rchild=NULL;
		root->height=0;
		root->data= x;
	}else{
		if(x<root->data) {
			insert(root->lchild,x);
			if(h(root->lchild)-h(root->rchild)==2) {
				root = (x<root->lchild->data?LL(root):LR(root));
			}
		}else{
			insert(root->rchild,x);
			if(h(root->rchild)-h(root->lchild)==2) {
				root = (x<root->rchild->data?RL(root):RR(root));
			}
		}
	}
	root->height = max(h(root->lchild),h(root->rchild))+1;
}

三、完整测试

#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define max(a,b)  (a>b?a:b)
typedef int Elemtype;
typedef struct Avlnode{
	int height;
	Avlnode *lchild,*rchild;
	Elemtype data;	
}Avlnode,*Avltree;

int h(Avlnode *p) {
	if(p==NULL) return 0;
	else return p->height;
}

Avlnode * LL(Avlnode *root) {
	Avlnode * k1 = root->lchild;
	root->lchild = k1->rchild;
	k1->rchild = root;
	k1->height = max(h(k1->lchild),h(k1->rchild))+1;
	root->height = max(h(root->lchild),h(root->rchild))+1;
	return k1;
}
Avlnode * RR(Avlnode *root) {
	Avlnode * k1 = root->rchild;
	root->rchild = k1->lchild;
	k1->lchild = root;
	k1->height = max(h(k1->lchild),h(k1->rchild))+1;
	root->height = max(h(root->lchild),h(root->rchild))+1;
	return k1;
}
Avlnode * LR(Avlnode *root) {
	root->lchild = RR(root->lchild);
	Avlnode * k1 = LL(root);
	return k1;
}
Avlnode * RL(Avlnode *root) {
	root->rchild = LL(root->rchild);
	Avlnode * k1 = RR(root);
	return k1;
}
void insert(Avltree &root,Elemtype x) {
	if(root == NULL) {
		root=(Avlnode *)malloc(sizeof(Avlnode));
		root->lchild=root->rchild=NULL;
		root->height=0;
		root->data= x;
	}else{
		if(x<root->data) {
			insert(root->lchild,x);
			if(h(root->lchild)-h(root->rchild)==2) {
				root = (x<root->lchild->data?LL(root):LR(root));
			}
		}else{
			insert(root->rchild,x);
			if(h(root->rchild)-h(root->lchild)==2) {
				root = (x<root->rchild->data?RL(root):RR(root));
			}
		}
	}
	root->height = max(h(root->lchild),h(root->rchild))+1;
}

void PreOrder(Avlnode * root) {
	if(root) {
		printf("%d ",root->data);
		PreOrder(root->lchild);
		PreOrder(root->rchild);
	}
}
void InOrder(Avlnode * root) {
	if(root) {
		InOrder(root->lchild);
		printf("%d ",root->data);
		InOrder(root->rchild);
	}
}
int main() {
	Avltree root = NULL;
	for(int i=1;i<=7;i++) {
		insert(root,i);
	}
	PreOrder(root);
	printf("\n");
	InOrder(root);
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值