C 数据结构 AVL树实现

AVL树特点平衡的二叉搜索树,每个节点的左子树和右子树高度最多差1

AVL树声明

#ifndef _AvlTree_H

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

AvlTree MakeEmpty(AvlTree T);
Position Find(int X, AvlTree T);
Position FindMin(AvlTree T);
Position FindMax(AvlTree T);
AvlTree Insert(int X, AvlTree T);
AvlTree Delete(int X, AvlTree T);
int Retrieve(Position P);

/* 打印树 */
void PrintTree(AvlTree T);

#endif

AVL树实现

#include <stdio.h>	// for stderr
#include <stdlib.h>	// for NULL
#include "AvlTree.h"

/* AVL树节点声明 */
struct AvlNode
{
	int Element;
	AvlTree Left;
	AvlTree Right;
	int Height;
};

/* 计算AVL节点的高度 */
static int Height(Position P)
{
	if(P == NULL)
		return -1;
	else 
		return P->Height;
}

/* 创建一棵空树 */
AvlTree MakeEmpty(AvlTree T)
{
	if(T != NULL)
	{
		MakeEmpty(T->Left);
		MakeEmpty(T->Right);
		free(T);
	}
	return NULL;
}

/* 查找某个节点 */
Position Find(int X, AvlTree T)
{
	if(T == NULL)
		return NULL;
	if(X < T->Element)
		return Find(X, T->Left);
	else if(X > T->Element)
		return Find(X, T->Right);
	else
		return T;
}

/* 查找二叉树最小节点 递归实现 */
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;
}

/* 返回2个数中大的值 */
static int Max(int A, 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)
{
	/* 在K1, K2间旋转 */
	K3->Left = SingleRotateWithRight(K3->Left);
	
	/* 在K3, K2间旋转 */
	return SingleRotateWithLeft(K3);
}

/* 右双旋转 */
static Position DoubleRotateWithRight(Position K3)
{
	/* 在K1, K2间旋转 */
	K3->Right = SingleRotateWithLeft(K3->Right);
	
	/* 在K3, K2间旋转 */
	return SingleRotateWithRight(K3);
}

/* 向AVL树插入节点 */
AvlTree Insert(int X, AvlTree T)
{
	if(T == NULL)
	{	
		/* 创建并返回带有一个节点的树 */
		T = malloc(sizeof(struct AvlNode));
		if(T == NULL)
			fprintf(stderr, "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)
				T = SingleRotateWithLeft(T);
			else 
				T = DoubleRotateWithLeft(T);
	}
	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);		
			else
				T = DoubleRotateWithRight(T);
	}
	/* 如果X已经存在,啥也不做 */
	
	T->Height = Max(Height(T->Left), Height(T->Right)) + 1;
	return T;
}

/* 删除节点比较复杂,如果删除次数比较少,可以使用懒得删除法,把被删除的节点标示为已删除,而不真正把它从树中删除 */
AvlTree Delete(int X, AvlTree T)
{

}

/* 取某个节点的值 */
int Retrieve(Position P)
{
	return P->Element;
}

/* 使用中序遍历打印树 */
void PrintTree(AvlTree T)
{
	if(T != NULL)
	{
		PrintTree(T->Left);
		printf("%d ", T->Element);
		PrintTree(T->Right);
	}
}

测试AVL树

#include <stdio.h>
#include "AvlTree.h"

int main(void)
{
    AvlTree tree = NULL;
    tree = Insert(10, tree);
    PrintTree(tree);
    printf("\n");
    tree = Insert(8, tree);
    PrintTree(tree);
    printf("\n");
    tree = Insert(12, tree);
    PrintTree(tree);
    printf("\n");
    tree = Insert(7, tree);
    tree = Insert(6, tree);
    tree = Insert(5, tree);
    PrintTree(tree);
    printf("\n");
    tree = Insert(98, tree);
    tree = Insert(100, tree);
    tree = Insert(111, tree);
    tree = Insert(112, tree);
    tree = Insert(113, tree);
    tree = Insert(114, tree);
    tree = Insert(115, tree);
    PrintTree(tree);
    printf("\n");

    return 0;
}

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