AVL树的基本操作

/* Note:Your choice is C IDE */
#include "stdio.h"
#include "stdlib.h"
#include "cking.h"

#ifndef _AvlTree_H

struct AvlNode;
typedef struct AvlNode *Position;
typedef struct AvlNode *AvlTree;

AvlTree MakeEmpty( AvlTree T );				/*清空表*/
Position Find( ElementType X, AvlTree T );	/*查找*/
Position FindMin( AvlTree T );				/*查找最小值*/
Position FindMax( AvlTree T );				/*查找最大值*/
AvlTree Insert( ElementType X, AvlTree T );	/*插入*/
AvlTree Delete( ElementType X, AvlTree T );	/*删除*/
ElementType Retrieve( Position P );			/*索引(不正确!)*/

#endif

struct AvlNode {
	ElementType Element;					/*整型*/
	AvlTree		Left;						/*左*/
	AvlTree		Right;						/*右*/
	int			Height;						/*高*/
};

void main()
{
	AvlTree T = NULL;
	int i;
	int a[16] = { 3, 2, 1, 4, 5, 6, 7, 16, 15, 14, 13, 12, 11, 10, 8, 9 };
	for( i = 0; i < 16; i++ ) {
		T = Insert( a[i], T );				/*初始插入*/
	}
	Retrieve( T );
	T = Delete( 4, T );
	printf("\n");
	Retrieve( T );
	T = Delete( 5, T );
	printf("\n");
	Retrieve( T );
	T = Delete( 2, T );
	printf("\n");
	Retrieve( T );							/*删除一个数就打印*/
    
}

static int Height( Position P )
{
	if( P == NULL )
		return -1;
	else
		return P->Height;
}

static int Max( const int a, const int b )
{
	return a > b ? a : b;
}

static Position SingleRotateWithLeft( Position K2 )		/*左旋转*/
{
	Position K1;
	
	K1 = K2->Left;									
	K2->Left = K1->Right;
	K1->Right = K2;
	
	K2->Height = Max( Height( K2->Left ), Height( K2->Right ) ) + 1;
	K1->Height = Max( Height( K1->Left ), Height( K1->Right ) ) + 1;
	/*重新确定高*/
	return K1;				/*返回新的根*/
}

static Position SingleRotateWithRight( Position K2 )	/*右旋转*/
{
	Position K1;
	
	K1 = K2->Right;
	K2->Right = K1->Left;
	K1->Left = K2;
	
	K2->Height = Max( Height( K2->Left ), Height( K2->Right ) ) + 1;
	K1->Height = Max( Height( K1->Left ), Height( K1->Right ) ) + 1;
	/*重新确定高*/
	return K1;				/*返回新的根*/
}

static Position DoubleRotateWithLeft( Position K3 )		/*RL双旋转*/
{
	K3->Left = SingleRotateWithRight( K3->Left );
	
	return SingleRotateWithLeft( K3 );
}

static Position DoubleRotateWithRight( Position K3 )	/*LR双旋转*/
{
	K3->Right = SingleRotateWithLeft( K3->Right );
	
	return SingleRotateWithRight( K3 );
}

AvlTree Insert( ElementType X, AvlTree T )
{
	if( T == NULL ) {									/*找到位置*/
		T = malloc( sizeof( struct AvlNode ) );
		if( T == NULL )
			FatalError( "Out of space!!!" );
		else {
			T->Element = X;
			T->Height = 0;
			T->Left = T->Right = NULL;
		}
	} else if( X < T->Element ) {						/*要小,向左继续查找*/
		T->Left = Insert( X, T->Left );
		if( Height( T->Left ) - Height( T->Right ) == 2 )	/*平衡打破*/
			if( X < T->Left->Element )					/*LL的情形*/
				T = SingleRotateWithLeft( T );
			else
				T = DoubleRotateWithLeft( T );			/*RL的情形*/
	} else if( X > T->Element ) {						/*要大,向右继续查找*/
		T->Right = Insert( X, T->Right );
		if( Height( T->Right ) - Height( T->Left ) == 2 )	/*平衡打破*/
			if( X > T->Right->Element )
				T = SingleRotateWithRight( T );			/*RR的情形*/
			else
				T = DoubleRotateWithRight( T );			/*LR的情形*/
	}
	
	T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;
	/*重新确定高*/
	return T;
}

ElementType Retrieve( Position P )
{
	if( P == NULL)
		return 0;
	printf( "%d\t%d\n", P->Element, P->Height );		/*中序输出*/
	Retrieve( P->Left );
	Retrieve( P->Right );
	return P->Element;
}

Position FindMin( AvlTree T )
{
	if( T == NULL )
		return NULL;
	else if( T->Left == NULL )
		return T;
	else
		return FindMin( T->Left );
}

Position FindMax( AvlTree T )
{
	if( T != NULL )
		while( T->Right != NULL )
			T = T->Right;
	
	return T;
}

AvlTree Delete( ElementType X, AvlTree T )
{
	Position TmpCell;
	
	if( T == NULL )									/*空*/
		Error( "Element not found" );
	else if( X < T->Element ) {						/*要小,向左继续查找*/
		T->Left = Delete( X, T->Left );
	} else if( X > T->Element ) {					/*要大,向右继续查找*/
		T->Right = Delete( X, T->Right );
	} else if( T->Left && T->Right ) {				/*左右节点都存在的情况*/
		TmpCell = FindMin( T->Right );
		T->Element = TmpCell->Element;
		T->Right = Delete( T->Element, T->Right );
	} else {										/*存在一个以下节点的情况*/
		TmpCell = T;
		if( T->Left == NULL )
			T = T->Right;
		else if( T->Right == NULL )
			T = T->Left;
		free( TmpCell );
	}
	if( T != NULL ) {								/*排除空的情况*/
		T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;	/*重新确定高*/
		if( Height( T->Left ) - Height( T->Right ) == 2 ) {		/*需要重新平衡*/
			if( Height( T->Left->Left ) > Height( T->Left->Right ) )
				T = SingleRotateWithLeft( T );	/*没有出现LR的情况,就LL*/
			else	
				T = DoubleRotateWithLeft( T );	/*出现LR的情况,用RL双旋转*/
		} else if( Height( T->Right ) - Height( T->Left ) == 2 ) {
			if( Height( T->Right->Right ) > Height( T->Right->Left ) )	
				T = SingleRotateWithRight( T );	/*没有出现RL的情况,就RR*/
			else
				T = DoubleRotateWithRight( T );	/*出现RL的情况,用LR双旋转*/
		}
	}
	
	return T;
}

	

    原文作者:AVL树
    原文地址: https://blog.csdn.net/ckingwizard/article/details/8652675
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞