题目来源:陈越《数据结构》配套PTA;
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.
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> /* Name: Root of AVL Copyright: Author: demosses Date: 23/04/17 11:11 Description: */ typedef int ElementType; typedef struct AVLNode *Position; typedef Position AVLTree; struct AVLNode { ElementType Data; ElementType Height; AVLTree Left; AVLTree Right; }; AVLTree Insert(int X, AVLTree T); int GetHeight(Position T); int Max(int a, int b); AVLTree SingleLeft(Position K); AVLTree SingleRight(Position K); AVLTree DoubleLeftRight(Position K); AVLTree DoubleRightLeft(Position K); int main(void) { AVLTree T = NULL; int N; scanf("%d", &N); while (N--) { int x; scanf("%d", &x); T=Insert(x, T); } if (T) printf("%d", T->Data); return 0; } AVLTree Insert(ElementType X, AVLTree T) { if (!T) { /*插入为空树*/ T = (AVLTree)malloc(sizeof(struct AVLNode)); T->Data = X; T->Height = 1; T->Left = T->Right = NULL; } else if (X < T->Data) { /*插入T的左子树*/ T->Left = Insert(X, T->Left); /*需要旋转*/ if (GetHeight(T->Left) - GetHeight(T->Right) == 2) { if (X < T->Left->Data) /*左单旋*/ T = SingleLeft(T); else /*左-右双旋*/ T = DoubleLeftRight(T); } } else if (X > T->Data) { /*插入T的右子树*/ T->Right = Insert(X, T->Right); /*需要旋转*/ if (GetHeight(T->Right) - GetHeight(T->Left) == 2) { if (X > T->Right->Data) /*右单旋*/ T = SingleRight(T); else /*左-右双旋*/ T = DoubleRightLeft(T); } } T->Height = Max(GetHeight(T->Left), GetHeight(T->Right)) + 1; return T; } int GetHeight(AVLTree T) { int HL,HR,MaxH; if(T){ HL=GetHeight(T->Left); HR=GetHeight(T->Right); MaxH=Max(HL,HR); return (MaxH+1); } else return 0; } int Max(int a,int b) { return (a > b) ? a : b; } AVLTree SingleLeft(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; } AVLTree SingleRight(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->Right), A->Height) + 1; return B; } AVLTree DoubleLeftRight(AVLTree A) { /*右-左单旋*/ A->Left = SingleRight(A->Left); return SingleLeft(A); } AVLTree DoubleRightLeft(AVLTree A) { /*左-右单旋*/ A->Right = SingleLeft(A->Right); return SingleRight(A); }