AVL平衡二叉查找树实现(C语言版本)

AVT树是每个节点的左子树和右子树的高度最多差 1 的二叉查找树。

(1)AVLTree.h 文件

#ifndef __AVL_TREE_H__
#define __AVL_TREE_H__

// AVL Tree: Adelson-Vlskii and Landis
// 带有平衡条件的二叉查找树
// AVT树是每个节点的左子树和右子树的高度最多差 1 的二叉查找树

#include "DataType.h"

typedef struct AVLTreeNode* AVLTreePt;
typedef struct AVLTreeNode  AVLTree;

struct AVLTreeNode
{
	DataType  data;
	AVLTreePt left;
	AVLTreePt right;
	int       count;
	int       height;
};

void TravelAVLTree( AVLTree* T );
void ReleaseAVLTree( AVLTree* T );                       // 释放 AVT Tree 的空间
AVLTree* InsertToAVLTree( DataType X, AVLTree* T);       // 向 AVL Tree 中插入数据
AVLTree* DeleteDataOfAVLTree( DataType X, AVLTree* T );  // 从 AVL Tree 中删除指定数据元素
AVLTree* FindDataInAVLTree( DataType X, AVLTree* T );    // 在 AVL Tree 中查找指定数据元素
AVLTree* FindMinInAVLTree( AVLTree* T );                 // 从 AVL Tree 中查找值最小节点
AVLTree* FindMaxInAVLTree( AVLTree* T );                 // 从 AVL Tree 中查找值最大的节点
#endif


其中DataType.h内容

#ifndef __DATA_TYPE_H__
#define __DATA_TYPE_H__
typedef int DataType;
#endif

(2)AVLTree.cpp文件

#include "AVLTree.h"
#include <stdlib.h>
#include <stdio.h>

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

static int Height( AVLTree* T )
{
	if ( T == NULL )
		return -1;
	else
		return T->height;
}

static void __travel( AVLTree* T, int deep )
{
	int i;

	if ( T != NULL )
	{
		for ( i = 0; i < deep; ++i )
		{
			printf("\t");
		}
		printf("%d(%d)\n", T->data, T->count);
		__travel( T->left, deep+1 );
		__travel( T->right, deep+1 );
	}
}

static AVLTree* LeftSingleRote( AVLTree* T )
{
	AVLTree* P = T->left;
	T->left = P->right;
	P->right = T;
	T->height = max_int( Height(T->left), Height(T->right) ) + 1;
	P->height = max_int( Height(T->left), Height(T->right) ) + 1;
	return P;
}

static AVLTree* RightSingleRote( AVLTree* T )
{
	AVLTree* P = T->right;
	T->right = P->left;
	P->left = T;
	T->height = max_int( Height(T->left), Height(T->right) ) + 1;
	P->height = max_int( Height(T->left), Height(T->right) ) + 1;
	return P;
}

static AVLTree* LeftDoubleRote( AVLTree* T )
{
	T->left = RightSingleRote( T->left );
	return LeftSingleRote( T );
}

static AVLTree* RigthDoubleRote( AVLTree* T )
{
	T->right = LeftSingleRote( T->right );
	return RightSingleRote( T );
}

// 遍历 AVL Tree
void TravelAVLTree( AVLTree* T )
{
	__travel( T, 0 );
}

// 向 AVL Tree 中插入数据
AVLTree* InsertToAVLTree( DataType X, AVLTree* T )
{
	if ( T == NULL )       // 如果 T 为空,直接插入
	{
		T = (AVLTree*)malloc(sizeof(AVLTree));
		T->data = X;
		T->right = T->left = NULL;
		T->count  = 1;
		T->height = 0;
	}
	else if ( X < T->data )
	{
		T->left = InsertToAVLTree( X, T->left );
		if ( Height( T->left ) - Height( T->right ) > 1 )
		{
			if ( X < T->left->data )
				T = LeftSingleRote( T );
			else
				T = LeftDoubleRote( T );
		}
	}
	else if ( X > T->data )
	{
		T->right = InsertToAVLTree( X, T->right );
		if ( Height( T->right ) - Height( T->left ) > 1 )
		{
			if ( X > T->right->data )
				T = RightSingleRote( T );
			else
				T = RigthDoubleRote( T );
		}
	}
	else // X == T->data
	{
		++T->count;
	}

	T->height = max_int( Height(T->left), Height(T->right) ) + 1; 

	return T;
}

// 释放 AVT Tree 的空间
void ReleaseAVLTree( AVLTree* T )
{
	if ( T != NULL )
	{
		ReleaseAVLTree( T->left );
		ReleaseAVLTree( T->right );
		free( T );
		T = NULL;
	}
}

// 在 AVL Tree 中查找指定数据元素
AVLTree* FindDataInAVLTree( DataType X, AVLTree* T )
{
	if ( T == NULL )  // 未找到
	{
		return T;
	}
	else if ( X == T->data )
	{
		return T;
	}
	else if ( X < T->data )
	{
		return FindDataInAVLTree( X, T->left );
	}
	else
	{
		return FindDataInAVLTree( X, T->right );
	}
}

// 从 AVL Tree 中查找值最小节点
AVLTree* FindMinInAVLTree( AVLTree* T )
{
	if ( T == NULL || T->left == NULL ) return T;
	return FindMinInAVLTree( T->left );
}

// 从 AVL Tree 中查找值最大的节点
AVLTree* FindMaxInAVLTree( AVLTree* T )
{
	if ( T == NULL || T->right == NULL ) return T;
	return FindMaxInAVLTree( T->right );
}

// 从 AVL Tree 中删除指定数据元素
AVLTree* DeleteDataOfAVLTree( DataType X, AVLTree* T )
{
	AVLTree* P;
	if ( T == NULL )
	{
		return T;
	}
	else if ( X == T->data )
	{
		if ( T->left == NULL && T->right == NULL ) // 左右子树为空
		{
			free( T );
			return NULL;
		}
		else if ( T->left == NULL )  // 右子树非空
		{
			P = T;
			T = T->right;
			free( P );
		}
		else if ( T->right == NULL ) // 左子树非空
		{
			P = T;
			T = T->left;
			free(P);
		}
		else     // 左右子树均非空 
		{
			P = FindMinInAVLTree( T->right );
			T->data = P->data;
			T->count = P->count;
			T->right = DeleteDataOfAVLTree( P->data, T->right );
		}
	}
	else if ( X < T->data )
	{
		T->left = DeleteDataOfAVLTree( X, T->left );
	}
	else
	{
		T->right = DeleteDataOfAVLTree( X, T->right );
	}

	T->height = max_int( Height(T->right), Height(T->left) ) + 1;
	if ( Height(T->left) - Height(T->right) == 2 )
	{
		if ( Height(T->left->left) > Height(T->left->right) )
			T = LeftSingleRote( T );
		else
			T = LeftDoubleRote( T );
	}
	else if ( Height(T->right) - Height(T->left) == 2 )
	{
		if ( Height(T->right->right) > Height(T->right->left) )
			T = RightSingleRote( T );
		else
			T = RigthDoubleRote( T );
	}
	return T;
}














   

你可能感兴趣的:(AVL平衡二叉查找树实现(C语言版本))