1. 程式人生 > >凜冬之翼---平衡二叉樹的調整

凜冬之翼---平衡二叉樹的調整

題目:
04-樹5 Root of AVL Tree (25 分)
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

解題思路:
1.這道題本質上就是要求你構造一個平衡二叉樹,我們知道平衡二叉樹的構造裡面有LL,RR,LR,RL 四種方式。首先要理解這三種方式的含義。
2.在構造的時候採用遞迴的方法,然後逐層返回,返回的同時再判斷是否存在不平衡現象,如果存在則呼叫旋轉函式,直到返回到根節點才完成一次數的插入,這樣做才能保證每一次插入以後樹都是平衡狀態。

遇到的問題:
1.這次遇到的bug比較少,還是那個當遇到異常停止的時候記得要檢查帶返回值的函式是否用法是正確的!

程式碼:

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

typedef struct TreeNode *AVLTree;
struct TreeNode{
	int Data;
	AVLTree Left;
	AVLTree Right;
	int Height;
};

int GetHeight(AVLTree T);

int Max(int a,int b);

AVLTree Insert(AVLTree T,int x);

AVLTree SingleLeftRotation(AVLTree A);     //LL rotation
AVLTree SingleRightRotation(AVLTree A);    //RR rotation
AVLTree DoubleLeftRotation(AVLTree A);     //LR rotation
AVLTree DoubleRightRotation(AVLTree A);    //Rl rotation

int GetHeight(AVLTree T){
	if(!T) return 0;
	return T->Height;
}

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

//LL rotation
AVLTree SingleLeftRotation(AVLTree A){
	AVLTree B;
	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; 
}

//RR rotation
AVLTree SingleRightRotation(AVLTree A){
	AVLTree B;
	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->Left),A->Height)+1;
	return B; 
}

//LR rotation
AVLTree DoubleLeftRotation(AVLTree A){
	A->Left=SingleRightRotation(A->Left);
	return SingleLeftRotation(A);
}

//RL rotation
AVLTree DoubleRightRotation(AVLTree A){
	A->Right=SingleLeftRotation(A->Right);
	return SingleRightRotation(A);
}


AVLTree Insert(AVLTree T,int x){
	if(!T){
		T=(AVLTree)malloc(sizeof (struct TreeNode));
		T->Data=x;
		T->Left=T->Right=NULL;
		T->Height=0;
	}
	else if(x<T->Data){
		T->Left=Insert(T->Left,x);
		if(GetHeight(T->Left)-GetHeight(T->Right)==2){
			if(x<T->Left->Data)
			T=SingleLeftRotation(T);
			else
			T=DoubleLeftRotation(T);
		}
	}
	else if(x>T->Data){
		T->Right=Insert(T->Right,x);
		if(GetHeight(T->Left)-GetHeight(T->Right)==-2){
			if(x>T->Right->Data)
			T=SingleRightRotation(T);
			else
			T=DoubleRightRotation(T);
		}
	}
	T->Height=Max(GetHeight(T->Left),GetHeight(T->Right))+1;
	return T;
}


int main()
{
	int N,x;
	AVLTree T=NULL;
	scanf("%d",&N);
	for(int i=0;i<N;i++){
		scanf("%d",&x);
		T=Insert(T,x);
	}
	if(T)  printf("%d",T->Data);
	return 0;	
 }