AVL樹實現

avl樹是其每個節點的左子樹和右子樹的高度最多差1的二叉查找樹。當在一棵avl樹中插入節點的時候,很可能把avl樹的平衡給破壞掉,在不平衡的情況下,可以通過對樹做單次旋轉或者複雜些的雙旋轉來處理。具體的旋轉方法Google去 O(∩_∩)O,這裏就不做詳細介紹啦。下面僅給已實現的avl樹的代碼。

/*====================*\
 |     AvlTree.h      |
\*====================*/

#ifndef _AVL_TREE_H_
#define _AVL_TREE_H_
#define ElementType int
#define Max(a,b) (a)>(b)?(a):(b)

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

//釋放樹空間
void ClearTree(AvlTree t);
//計算節點的高度
int Height(Position p);
//插入節點
AvlTree Insert(ElementType x,AvlTree t);
//先序遍歷
void Preorder_TreePrint(AvlTree t);
//中序遍歷
void Inorder_TreePrint(AvlTree t);
//後序遍歷
void Postorder_TreePrint(AvlTree t);

//針對左子樹做單旋轉
static Position SingleRotateWithLeft(Position k2);
//針對右子樹做單旋轉
static Position SingleRotateWithRight(Position k2);
//針對左子樹做雙旋轉
static Position DoubleRotateWithLeft(Position k3);
//針對右子樹做雙旋轉
static Position DoubleRotateWithRight(Position k3);

#endif

//AVL樹結構體
struct AvlNode {
	ElementType Element;
	AvlTree Left;
	AvlTree Right;
	int Height;
};

/*====================*\
 |     AvlTree.c      |
\*====================*/

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

//清空樹
void ClearTree(AvlTree t)
{
	if(t!=NULL)
	{
		ClearTree(t->Left);
		ClearTree(t->Right);
		free(t);
	}
}
//先序遍歷
void Preorder_TreePrint(AvlTree t)
{
	if(t!=NULL)
	{
		printf("%d(%d) \n",t->Element,t->Height);
		Preorder_TreePrint(t->Left);
		Preorder_TreePrint(t->Right);
	}
}
//中序遍歷
void Inorder_TreePrint(AvlTree t)
{
	if(t!=NULL)
	{
		Inorder_TreePrint(t->Left);		
		printf("%d(%d) \n",t->Element,t->Height);
		Inorder_TreePrint(t->Right);
	}
}
//後續遍歷
void Postorder_TreePrint(AvlTree t)
{
	if(t!=NULL)
	{
		Postorder_TreePrint(t->Left);
		Postorder_TreePrint(t->Right);
		printf("%d(%d) \n",t->Element,t->Height);
	}
}
//插入節點
AvlTree Insert(ElementType x,AvlTree t)
{
	if (t == NULL) 
	{
		t = (AvlTree)malloc(sizeof(struct AvlNode));
		if (t == NULL)
		{
			printf("out of space!!\n");
		}
		else
		{
			t->Element = x;
			t->Left = NULL;
			t->Right = NULL;
			t->Height = 0;
		}
	}
	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);
			}
		}
	}

	t->Height = Max(Height(t->Left),Height(t->Right))+1;

	return t;
}
//算節點的高度
int Height(Position p)
{
	if(p == NULL)
		return 0;
	else
		return Max(Height(p->Left),Height(p->Right))+1;
}


//針對左子樹做單旋轉
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)
{
	k3->Left = SingleRotateWithRight(k3->Left);
	return SingleRotateWithLeft(k3);
}
//針對右子樹做雙旋轉
static Position DoubleRotateWithRight(Position k3)
{
	k3->Right = SingleRotateWithLeft(k3->Right);
	return SingleRotateWithRight(k3);
}

/*====================*\
 |     main.c         |
\*====================*/
#include "AvlTree.h"
#include <stdio.h>
int main()
{
	int i = 0;
	AvlTree at = NULL;
	for(i=0;i<10;i++)
		at = Insert(i,at);
	Preorder_TreePrint(at);
	ClearTree(at);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章