Root of AVL Tree

AllenRicard發表於2018-10-09

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

 

 

 

 

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

 

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the root of the resulting AVL tree in one line.

Sample Input 1:

5
88 70 61 96 120

Sample Output 1:

70

Sample Input 2:

7
88 70 61 96 120 90 65

Sample Output 2:

88

答案如下:

#include <stdio.h>
#include <stdlib.h>
typedef struct AVLNode *Position;
typedef Position AVLTree;
struct AVLNode{
	int Data;
	AVLTree Left;
	AVLTree Right;
	int height;
};
int max(int a,int b){
	return a>b?a:b;
}
int GetHeight(AVLTree A){
	if(!A) return 0;
	else return A->height;
}
AVLTree NewNode(int V){
	AVLTree t=(AVLTree)malloc(sizeof(struct AVLNode));
	t->Data=V;
	t->Left=t->Right=NULL;
	t->height=0;
	return t;
}
int Max ( int a, int b )
{
	return a > b ? a : b;
}

AVLTree SingleLeftRotation ( AVLTree A )
{ /* 注意:A必須有一個左子結點B */
	/* 將A與B做左單旋,更新A與B的高度,返回新的根結點B */     
	AVLTree B = A->Left;
	A->Left = B->Right;
	B->Right = A;
	A->height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1;
	B->height = Max( GetHeight(B->Left), A->height ) + 1;
	return B;
}

AVLTree SingleRightRotation ( AVLTree A )
{ /* 注意:A必須有一個右子結點B */
	/* 將A與B做右單旋,更新A與B的高度,返回新的根結點B */     
	AVLTree B = A->Right;
	A->Right = B->Left;
	B->Left = A;
	A->height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1;
	B->height = Max( GetHeight(B->Right), A->height ) + 1;
	return B;
}

AVLTree DoubleLeftRightRotation ( AVLTree A )
{ /* 注意:A必須有一個左子結點B,且B必須有一個右子結點C */
	/* 將A、B與C做兩次單旋,返回新的根結點C */

	/* 將B與C做右單旋,C被返回 */
	A->Left = SingleRightRotation(A->Left);
	/* 將A與C做左單旋,C被返回 */
	return SingleLeftRotation(A);
}

AVLTree DoubleRightLeftRotation ( AVLTree A )
{ /* 注意:A必須有一個右子結點B,且B必須有一個左子結點C */
	/* 將A、B與C做兩次單旋,返回新的根結點C */

	/* 將B與C做左單旋,C被返回 */
	A->Right = SingleLeftRotation(A->Right);
	/* 將A與C做右單旋,C被返回 */
	return SingleRightRotation(A);
}
AVLTree Insert( AVLTree T, int X )
{ /* 將X插入AVL樹T中,並且返回撥整後的AVL樹 */
	if ( !T ) { /* 若插入空樹,則新建包含一個結點的樹 */
		T = (AVLTree)malloc(sizeof(struct AVLNode));
		T->Data = X;
		T->height = 0;
		T->Left = T->Right = NULL;
	} /* if (插入空樹) 結束 */

	else if ( X < T->Data ) {
		/* 插入T的左子樹 */
		T->Left = Insert( T->Left, X);
		/* 如果需要左旋 */
		if ( GetHeight(T->Left)-GetHeight(T->Right) == 2 )
			if ( X < T->Left->Data ) 
				T = SingleLeftRotation(T);      /* 左單旋 */
			else 
				T = DoubleLeftRightRotation(T); /* 左-右雙旋 */
	} /* else if (插入左子樹) 結束 */

	else if ( X > T->Data ) {
		/* 插入T的右子樹 */
		T->Right = Insert( T->Right, X );
		/* 如果需要右旋 */
		if ( GetHeight(T->Left)-GetHeight(T->Right) == -2 )
			if ( X > T->Right->Data ) 
				T = SingleRightRotation(T);     /* 右單旋 */
			else 
				T = DoubleRightLeftRotation(T); /* 右-左雙旋 */
	} /* else if (插入右子樹) 結束 */

	/* else X == T->Data,無須插入 */

	/* 別忘了更新樹高 */
	T->height = Max( GetHeight(T->Left), GetHeight(T->Right) ) + 1;
	return T;
}

AVLTree MakeTree(int n){
	AVLTree t;
	int i,V;
	scanf("%d",&V);
	t=NewNode(V);
	for(i=1;i<n;i++){
		scanf("%d",&V);
		t=Insert(t,V);
	}
	return t;
}
void FreeTree ( AVLTree T ) /* 釋放T的空間 */
{
	if (T->Left) FreeTree(T->Left);
	if (T->Right) FreeTree(T->Right);
	free(T);
}
int main(){
	int N;
	scanf("%d",&N);
	AVLTree A;
	A=MakeTree(N);
	int data=A->Data;
	printf("%d\n",data);
	FreeTree(A);
	return 0;
}

 

相關文章