[Data structure] Tree-based search method - balanced binary sorting tree (C language)

See binary sorting tree: https://blog.csdn.net/weixin_51450101/article/details/123172688?spm=1001.2014.3001.5501

1. Balanced Binary Sorting Tree

A balanced binary sort tree is also called an AVL tree . A balanced binary sort tree is either an empty tree, or a binary sort tree with the following properties:
① The absolute value of the height difference between the left subtree and the right subtree is less than or equal to 1;
② The left subtree and the right subtree It is also a balanced binary sort tree.
The purpose of introducing a balanced binary sorting tree is to improve search efficiency.
Balance factor : the difference between the depth of the left subtree and the depth of the right subtree of a node. Obviously, for a balanced binary sorting tree, the balance factors of all its nodes can only be -1, 0 or 1. When inserting a node in a balanced binary sorting tree, it may cause an imbalance, that is, a balance factor with an absolute value greater than 1, such as 2 and -2.
balance and imbalancetype definition

typedef int DataType;
/*平衡二叉排序树的类型定义*/
typedef struct node {
    
    
	DataType data;
	int bf;			//结点的平衡因子
	struct node* lchild, * rchild;
}AVLNode, * AVLTree;

2. Imbalance types and adjustment methods

2.1 LL type
Assuming that the lowest level unbalanced node is A, inserting a new node S into the left subtree of the left subtree of node A will cause imbalance. It is easy to infer from the balance factors of A and B that the depths of BL, BR, and AR are the same. In order to restore the balance and maintain the characteristics of the binary sorting tree, it can be adjusted as follows:
LL type

/*LL型调整代码*/
//1 旋转结点实现平衡
A->lchild = B->rchild;
B->rchild = A;
//2 调整平衡因子
A->bf = 0;	B->bf = 0;
//3 将当前调整完的子树接到原树上
if (FA == NULL)			//FA为A原来的父指针
	*avlt = B;
else if (A == FA->lchild)	FA->lchild = B;
else	FA->rchild = B;

2. LR type
Assuming that the lowest level unbalanced node is A, a new node S is inserted into the right subtree of the left subtree of node A, resulting in imbalance. Here, it is assumed that S is inserted under CL. From the balance factors of A, B, and C, it can be inferred that CL and CR have the same depth, BL and AR have the same depth, and BL and AR are 1 greater than CL and CR. In order to restore the balance and maintain the characteristics of the binary sorting tree, it can be adjusted as follows:
LR type

/*LR型调整代码*/
//1 旋转结点实现平衡
C = B->rchild;
B->rchild = C->lchild;
A->lchild = C->rchild;
C->lchild = B;
C->rchild = A;
//2 调整平衡因子
if (S->data < C->data) {
    
    
	A->bf = -1;
	B->bf = 0;
	C->bf = 0;
}
else if (S->data > C->data) {
    
    
	A->bf = 0;
	B->bf = 1;
	C->bf = 0;
}
else {
    
    
	A->bf = 0;
	B->bf = 0;
}
//3 将当前调整完的子树接到原树上
if (FA == NULL)		//FA为A原来的父指针
	*avlt = C;
else if (A == FA->lchild)
	FA->lchild = C;
else
	FA->rchild = C;
}

3. RR type
RR type and LL type are symmetrical. Assuming that the unbalanced node at the lowest level is A, a new node S is inserted into the right subtree of the right subtree of node A, resulting in imbalance. It is easy to infer from the balance factors of A and B that the depths of BL, BR, and AL are the same. In order to restore the balance and maintain the characteristics of the binary sorting tree, it can be adjusted as follows:
RR type

/*RR型调整代码*/
//1 旋转结点实现平衡
A->rchild = B->lchild;
B->lchild = A;
//2 调整平衡因子
A->bf = 0; B->bf = 0;
//3 将当前调整完的子树接到原树上
if (FA == NULL)		//FA为A原来的父指针
	*avlt = B;
else if (A == FA->lchild)
	FA->lchild = B;
else
	FA->rchild = B;
} 

4. RL type
RL type and LR type are symmetrical. Assuming that the unbalanced node at the lowest level is A, a new node S is inserted into the left subtree of the right subtree of node A, resulting in imbalance. Here, it is assumed that S is inserted under CR. From the balance factors of A, B, and C, it can be deduced that CL has the same depth as CR, AL has the same depth as BR, and the depths of AL and BR are 1 greater than those of CL and CR. In order to restore the balance and maintain the characteristics of the binary sorting tree, it can be adjusted as follows:
RL type

/*RL型调整代码*/
//1 旋转结点实现平衡
C = B->lchild;
B->lchild = C->rchild;
A->rchild = C->lchild;
C->lchild = A;
C->rchild = B;
//2 调整平衡因子
if (S->data < C->data) {
    
    
	A->bf = 0;
	B->bf = -1;
	C->bf = 0;
}
else if (S->data > C->data) {
    
    
	A->bf = 1;
	B->bf = 0;
	C->bf = 0;
}
else {
    
    
	A->bf = 0;
	B->bf = 0;
}
//3 将当前调整完的子树接到原树上
if (FA == NULL)		//FA为A原来的父指针
	*avlt = C;
else if (A == FA->lchild)
	FA->lchild = C;
else
	FA->rchild = C;
}

3. Complete implementation of code and running results

/*平衡二叉排序树*/
# include<stdio.h>
# include<malloc.h>

typedef int DataType;

/*平衡二叉排序树的类型定义*/
typedef struct node {
    
    
	DataType data;
	int bf;							//结点的平衡因子
	struct node* lchild, * rchild;
}AVLNode, * AVLTree;

/*平衡二叉排序树的插入算法*/
void Ins_AVLtree(AVLTree* avlt, DataType data) {
    
    
	AVLNode* S, * A = *avlt, * FA = NULL, * p = *avlt, * fp = NULL, * B, * C;
	S = (AVLTree)malloc(sizeof(AVLNode));
	S->data = data;
	S->lchild = S->rchild = NULL;
	S->bf = 0;
	if (*avlt == NULL) {
    
    				//平衡二叉树为空直接插入
		*avlt = S;
		return;
	}
	/*查找插入位置*/
	else {
    
    								//首先查找S的插入位置fp,同时记录距S的插入位置最近且平衡因子不等于0的结点A,该结点可能为失衡结点
		while (p != NULL) {
    
    
			if (p->bf != 0) {
    
    
				A = p;
				FA = fp;
			}
			fp = p;
			if (data < p->data)
				p = p->lchild;
			else
				p = p->rchild;
		}
	}
	/*插入S*/
	if (data < fp->data)
		fp->lchild = S;
	else
		fp->rchild = S;
	/*确定结点B并修改A的平衡因子*/
	if (data < A->data) {
    
    
		B = A->lchild;
		A->bf = A->bf + 1;
	}
	else {
    
    
		B = A->rchild;
		A->bf = A->bf - 1;
	}
	/*修改B到S路径上各结点的平衡因子(原值均为0)*/
	p = B;
	while (p != S) {
    
    
		if (data < p->data) {
    
    
			p->bf = 1;
			p = p->lchild;
		}
		else {
    
    
			p->bf = -1;
			p = p->rchild;
		}
	}
	/*判断失衡类型并做相应处理*/
	if (A->bf == 2 && B->bf == 1) {
    
    			//LL型
		A->lchild = B->rchild;
		B->rchild = A;
		A->bf = 0;
		B->bf = 0;
		if (FA == NULL)
			*avlt = B;
		else if (A == FA->lchild)
			FA->lchild = B;
		else
			FA->rchild = B;
	}
	else if (A->bf == 2 && B->bf == -1) {
    
    	//LR型
		C = B->rchild;
		B->rchild = C->lchild;
		A->lchild = C->rchild;
		C->lchild = B;
		C->rchild = A;
		if (S->data < C->data) {
    
    
			A->bf = -1;
			B->bf = 0;
			C->bf = 0;
		}
		else if (S->data > C->data) {
    
    
			A->bf = 0;
			B->bf = 1;
			C->bf = 0;
		}
		else {
    
    
			A->bf = 0;
			B->bf = 0;
		}
		if (FA == NULL)
			*avlt = C;
		else if (A == FA->lchild)
			FA->lchild = C;
		else
			FA->rchild = C;
	}
	else if (A->bf == -2 && B->bf == 1) {
    
    	//RL型
		C = B->lchild;
		B->lchild = C->rchild;
		A->rchild = C->lchild;
		C->lchild = A;
		C->rchild = B;
		if (S->data < C->data) {
    
    
			A->bf = 0;
			B->bf = -1;
			C->bf = 0;
		}
		else if (S->data > C->data) {
    
    
			A->bf = 1;
			B->bf = 0;
			C->bf = 0;
		}
		else {
    
    
			A->bf = 0;
			B->bf = 0;
		}
		if (FA == NULL)
			*avlt = C;
		else if (A == FA->lchild)
			FA->lchild = C;
		else
			FA->rchild = C;
	}
	else if (A->bf == -2 && B->bf == -1) {
    
    	//RR型
		A->rchild = B->lchild;
		B->lchild = A;
		A->bf = 0;
		B->bf = 0;
		if (FA == NULL)
			*avlt = B;
		else if (A == FA->lchild)
			FA->lchild = B;
		else
			FA->rchild = B;
	}
}

/*创建平衡二叉排序树*/
void CreatAVLT(AVLTree* avlt) {
    
    
	DataType data;
	*avlt = NULL;
	scanf("%d", &data);
	while (data != 0) {
    
    
		Ins_AVLtree(avlt, data);
		scanf("%d", &data);
	}
}

/*中序遍历输出平衡排序二叉树*/
void InOrder(AVLTree avlt) {
    
    
	if (avlt != NULL) {
    
    
		InOrder(avlt->lchild);
		printf("%d(%d) ", avlt->data, avlt->bf);
		InOrder(avlt->rchild);
	}
}

int main() {
    
    
	AVLTree avlt;
	CreatAVLT(&avlt);
	printf("中序遍历输出:");
	InOrder(avlt);
	return 0;
}

operation result
operation result

The value in the brackets of the output result is the balance factor of the corresponding node.
The balanced binary sorting tree corresponding to the running example is shown in the figure below:
Run the use case
Reference: Geng Guohua "Data Structure-Description in C Language (Second Edition)"

For more data structure content, follow my "Data Structure" column : https://blog.csdn.net/weixin_51450101/category_11514538.html?spm=1001.2014.3001.5482

Guess you like

Origin blog.csdn.net/weixin_51450101/article/details/123372250