AVL树

#include <stdio.h>
#include <stdlib.h>
typedef struct ND {
struct ND * leftchd;
struct ND * rightchd;
int height;
int data;
} node;

typedef struct QUE {
struct ND * cur;
struct QUE * next;
} queue;
typedef node * pn;
node * insertnd(node * np,int a);
node * removnode(node * np,int value);
node * minnode(node * np);
node * LLRotate(node * np);
node * RRRotate(node * np);
int max(node * x,node * y);
int getheight(node * np);
void traverse(queue * front,queue * tail);
int main() {
node * root = NULL;
int n = 0;
scanf("%d",&n);
root = insertnd(root,n);
while(n != -1) {
	scanf("%d",&n);
	if(n != -1)
		root = insertnd(root,n);
}
queue * front = NULL,* tail = NULL;
front = (queue *)malloc(sizeof(queue));
tail = NULL;
tail = front;
front->cur = root;
front->next = NULL;
traverse(front,tail);
root = removnode(root,4);
traverse(front,tail);
return 0;
}

node * insertnd(node * np, int a) {
if(np == NULL) {
	np = (node *)malloc(sizeof(node));
	np->data = a;
	np->leftchd = NULL;
	np->rightchd = NULL;
	np->height = 1;
	return np;
}
if(a < np->data)
	np->leftchd = insertnd(np->leftchd,a);
else if(a > np->data) {
	np->rightchd = insertnd(np->rightchd,a);
} else return np;
np->height = max(np->leftchd,np->rightchd) + 1;
int balance = getbalance(np);
//LL
if(balance > 1&&getbalance(np->leftchd) > 0)
	return LLRotate(np);
//LR
else if(balance > 1&&getbalance(np->leftchd) < 0) {
	np->leftchd = RRRotate(np->leftchd);
	return LLRotate(np->leftchd);
	//RR
} else if(balance < -1&&getbalance(np->rightchd) < 0)
	return RRRotate(np);
//RL
else if(balance < -1&&getbalance(np->rightchd) > 0) {
	np->rightchd = LLRotate(np->rightchd);
	return RRRotate(np);
}
return np;
}

node * removnode(node * np,int value) {
if(np == NULL) {
	return np;
}
if(np->data == value) {
	if(np->leftchd == NULL||np->rightchd == NULL) {
		node * temp = NULL;
		if(np->leftchd == temp)
			temp = np->rightchd;
		else temp = np->leftchd;
		if(temp == NULL)
			np = NULL;
		else np = temp;
	} else {
		node * temp = NULL;
		temp =  minnode(np->rightchd);
		np->data = temp->data;
		np->rightchd = removnode(np->rightchd,temp->data);
	}
} else if(value < np->data) {
	np->leftchd = removnode(np->leftchd,value);
} else {
	np->rightchd = removnode(np->rightchd,value);
}
if(np == NULL) return np;
np->height = max(np->leftchd,np->rightchd) + 1;
int balance = getbalance(np);
//LL
if(balance > 1&&getbalance(np->leftchd) > 0) {
	return LLRotate(np);
	//LR
} else if(balance > 1&&getbalance(np->leftchd) < 0) {
	np->leftchd = RRRotate(np->leftchd);
	return LLRotate(np->leftchd);
	//RR
} else if(balance < -1&&getbalance(np->rightchd) < 0) {
	return RRRotate(np);
	//RL
} else if(balance < -1&&getbalance(np->rightchd) > 0) {
	np->rightchd = LLRotate(np->rightchd);
	return RRRotate(np);
}
return np;
}

int getbalance(node * np) {
return getheight(np->leftchd) - getheight(np->rightchd);
}

int getheight(node * np) {
if(np == NULL) {
	return 0;
}
return np->height;
}

int max(node * x,node * y) {
if(x == NULL&&y == NULL)
	return 0;
else if(x != NULL&&y == NULL)
	return x->height;
else if(y != NULL&&x == NULL)
	return y->height;
else
	return x->height >= y->height?x->height:y->height;
}

node * minnode(node * np) {
if(np->leftchd != NULL)
	minnode(np->leftchd);
else return np;
}

node * LLRotate(node * np) {
node * temp = NULL;
node * temp2 = NULL;
temp = np;
temp2 = np->leftchd;
temp->leftchd = temp2->rightchd;
temp2->rightchd = temp;
temp->height = max(temp->leftchd,temp->rightchd) + 1;
temp2->height = max(temp2->leftchd,temp2->rightchd) + 1;
return temp2;
}

node * RRRotate(node * np) {
node * temp = NULL;
node * temp2 = NULL;
temp = np;
temp2 = np->rightchd;
temp->rightchd = temp2->leftchd;
temp2->leftchd = temp;
temp->height = max(temp->leftchd,temp->rightchd) + 1;
temp2->height = max(temp2->leftchd,temp2->rightchd) + 1;
	return temp2;
}

void traverse(queue * front,queue * tail) {
if(front == NULL) return;
if(front->cur->leftchd != NULL) {
	tail->next = (queue *)malloc(sizeof(queue));
	tail->next->next = NULL;
	tail = tail->next;
	tail->cur = front->cur->leftchd;
}
if(front->cur->rightchd != NULL) {
	tail->next = (queue *)malloc(sizeof(queue));
	tail->next->next = NULL;
	tail = tail->next;
	tail->cur = front->cur->rightchd;
}
printf("%d %d\n",front->cur->data,front->cur->height);
front = front->next;
traverse(front,tail);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值