Root of AVL Tree

来源:互联网 发布:cfve卡枪软件 编辑:程序博客网 时间:2024/05/10 15:33

不多说,先粘题目:

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 ythe root of the resulting AVL tree in one line.

Sample Input 1:
588 70 61 96 120
Sample Output 1:
70
Sample Input 2:
788 70 61 96 120 90 65
Sample Output 2:
88


解释题目:

这道题的意思说白了就是构建AVL(二叉平衡树),有四种旋转情况,左左,右右,左右,右左

这里粘一个大神的链接,可以很好的帮助理解AVL树

http://www.icourse163.org/learn/zju-93001#/learn/forumdetail?pid=533230(未经允许,擅自粘链接,是不是不太好呀,嘻嘻)


这道题也没什么好分析的啦,也就是构建一个ALV树,然后输出根结点就够了,直接上代码吧,其实这倒题的代码直接粘了好多老师的代码,(就是懒,就是任性,哈哈)

#include<iostream>using namespace std;typedef int ElemType;typedef struct AVLTreeNode *AVLTree;struct AVLTreeNode {ElemType data;AVLTree left;AVLTree right;int height;};int GetHeight(AVLTreeNode *tree){if (tree == NULL)return -1;                     //空树返回-1elsereturn tree->height;}int Max(int a,int b){if (a > b)return a;elsereturn b;}AVLTree SingleLeftRotation(AVLTree A){   /* 注意:A 必须有一个左子结点 B *//* 将 A 与 B 做如图 4.35 所示的左单旋,更新 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 做如图 4.35 所示的右单旋,更新 A 与 B 的高度,返回新的根结点 B */AVLTree B = A->right;A->right = B->left;B->left = A;A->height = Max(GetHeight(A->right), GetHeight(A->left)) + 1;B->height = Max(GetHeight(B->right), A->height) + 1;return B;}AVLTree DoubleLeftRightRotation(AVLTree A) {/* 注意:A 必须有一个左子结点 B,且 B 必须有一个右子结点 C */   /* 将 A、B 与 C 做如图 4.38 所示的两次单旋,返回新的根结点 C */          A->left = SingleRightRotation(A->left); /*将 B 与 C 做右单旋,C 被返回*/return SingleLeftRotation(A); /*将 A 与 C 做左单旋,C 被返回*/}AVLTree DoubleRightLeftRotation(AVLTree A){/* 注意:A 必须有一个左子结点 B,且 B 必须有一个右子结点 C *//* 将 A、B 与 C 做如图 4.38 所示的两次单旋,返回新的根结点 C */A->right = SingleLeftRotation(A->right); /*将 B 与 C 做右单旋,C 被返回*/return SingleRightRotation(A); /*将 A 与 C 做左单旋,C 被返回*/}AVLTree AVL_Insertion(ElemType X, AVLTree T) { /* 将 X 插入 AVL 树 T 中,并且返回调整后的 AVL 树 */  if (!T) { /* 若插入空树,则新建包含一个结点的树 */   T = (AVLTree)malloc(sizeof(struct AVLTreeNode));   T->data = X;   T->height = 0;   T->left = T->right = NULL; } /* if (插入空树) 结束 */else if (X < T->data) { /* 插入 T 的左子树 */   T->left = AVL_Insertion(X, T->left);         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 = AVL_Insertion(X, T->right);         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;}int main(){int n;cin >> n;AVLTree root = NULL;int x;for (int i = 0; i < n; i++){cin >> x;root = AVL_Insertion(x, root);}cout << root->data;return 0;}


0 0
原创粉丝点击