AVL树(二叉平衡树)笔记

先上代码 以后补充


avl树插入操作还好,删除操作比较麻烦--


#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct TreeNode* T;

struct TreeNode{
	int bf;
	int height;
	int data;
	T leftchild;
	T rightchild;
	T parent;
};

T root = NULL;
T findDelPosition(T root, int key);//function prototype

int getHeight(T node){
	if (node) return node->height;
	else return -1;
}

int max(int lc, int rc){

	return (lc > rc ? lc : rc);
}
int reComHeight(T root){
	root->height = max(getHeight(root->leftchild), getHeight(root->rightchild)) + 1;
	root->bf = getHeight(root->leftchild) - getHeight(root->rightchild);
	return root->bf;
}

T LLclockRotation(T x){
	T y = x->leftchild;
	x->leftchild = y->rightchild;
	if (y->rightchild) y->rightchild->parent = x;
	y->rightchild = x;
	y->parent = x->parent;
	x->parent = y;
	reComHeight(x);
	reComHeight(y);
	return y;
}

T RRantiRotation(T x){
	T y = x->rightchild;
	x->rightchild = y->leftchild;
	if (y->leftchild) y->leftchild->parent = x;
	y->leftchild = x;
	y->parent = x->parent;
	x->parent = y;
	reComHeight(x);
	reComHeight(y);
	return y;
}

T LRanti_clockRotation(T x){
	x->leftchild = RRantiRotation(x->leftchild);
	return LLclockRotation(x);
}

T RLclock_antiRotation(T x)
{
	x->rightchild = LLclockRotation(x->rightchild);
	return RRantiRotation(x);
}
T reBalance(T curr, int bf){
	if (abs(bf)<2) return curr;
	else{
		if (bf == -2){
			if (curr->rightchild->bf>0) return RLclock_antiRotation(curr);
			else return RRantiRotation(root);
		}
		else if (bf == 2){
			if (curr->leftchild->bf < 0)return LRanti_clockRotation(root);
			else return LLclockRotation(root);
		}
	}
}

T insert(T curr, T prev, int num){
	int x = 0;
	if (!curr){
		curr = (T)malloc(sizeof(TreeNode));
		curr->data = num;
		curr->bf = 0;
		curr->height = 0;
		curr->parent = prev;
		curr->leftchild = curr->rightchild = NULL;
		x = num;
	}
	else if (num<(curr->data)) curr->leftchild = insert(curr->leftchild, curr, num);
	else if (num>(curr->data)) curr->rightchild = insert(curr->rightchild, curr, num);
	curr=reBalance(curr,reComHeight(curr));
	return curr;
}

void toPrint(T root, int depth = 0){
	if (root->rightchild)toPrint(root->rightchild, depth + 1);
	for (int i = 0; i< depth; i++) printf("\t");
	printf("%d(%d)\n", root->data, root->bf);
	if (root->leftchild)toPrint(root->leftchild, depth + 1);
}

T FindMin(T least){
	if (!least->leftchild)return least;
	else FindMin(least->leftchild);
}


T clearTarget(T root){
	T par = root->parent, temp = root;
	if (root->leftchild&&root->rightchild){
		temp = FindMin(root->rightchild);
		root->data = temp->data;
		root->rightchild = findDelPosition(root->rightchild, root->data);
	}
	else{
		if (root->leftchild){
			root->leftchild->parent = par;
			if (par->leftchild == root) par->leftchild = root->leftchild;
			else if (par->rightchild == root) par->rightchild = root->leftchild;
			root = root->leftchild;
		}
		else if (root->rightchild){
			root->rightchild->parent = par;//认可问题
			if (par->leftchild == root) par->leftchild = root->rightchild;
			else if (par->rightchild == root) par->rightchild = root->rightchild;
			root = root->rightchild;
		}
		else root = NULL;//都为NULL
		free(temp);
	}
	if (root) reComHeight(root);
	return root;
}

T findDelPosition(T root, int key){
	if (key == root->data) root = clearTarget(root);
	else if (key<root->data) root->leftchild = findDelPosition(root->leftchild, key);
	else if (key>root->data) root->rightchild = findDelPosition(root->rightchild, key);
	if (root) root = reBalance(root, reComHeight(root));
	return root;

}
int main(){

	int temp[9] = { 40, 60, 20, 80, 50, 30, 10, 70, 25 };
	int i = 0;
	for (i = 0; i < 9; i++) root = insert(root, root, temp[i]);
	//findDelPosition(root, 40);
	toPrint(root);
	return 0;
}

例子输出:

删除结点40之前:


删除结点40之后:




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值