标签:names actor put input inpu 一个 include 比较大小 algo
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
考差了AVL树的插入操作,插入可以用一个递归函数完成,比较大小然后向左或右递归,递归边界为遇到空结点,返回时,检查每个结点的平衡因子是否为2或-2,是的话则进行旋转。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define AVL
typedef struct Node* link;
struct Node
{
int val;
int height;
link left;
link right;
};
link insert(link & T, int val);
void L_Rotate(link & root);
void R_Rotate(link & root);
void freeTree(link T);
int getHeight(link T);
void updateHeight(link T);
int getBalanceFactor(link T);
int main()
{
int N, temp;
scanf("%d", &N);
link root=NULL;
for (int i = 0; i < N; i++)
{
scanf("%d", &temp);
insert(root, temp);
}
printf("%d", root->val);
return 0;
}
void R_Rotate(link & root)
{
link A, B, C;
A = root;
B = A->left;
C = B->right;
A->left = C;
B->right = A;
updateHeight(A);
updateHeight(B);
root = B;
}
void L_Rotate(link & root)
{
link A, B, C;
A = root;
B = A->right;
C = B->left;
A->right = C;
B->left = A;
updateHeight(A);
updateHeight(B);
root = B;
}
link insert(link & T, int val)
{
if (T == NULL) //如果该结点为空则创立新结点,结束递归
{
T = new Node;
T->val = val;
T->left = NULL;
T->right = NULL;
T->height = 1; //插入的新结点高度为1
return T;
}
if (val > T->val) //插入右边
{
T->right = insert(T->right, val);
updateHeight(T);
#if defined(AVL) //如果是AVL树,完成插入操作后要检查平衡因子
if (getBalanceFactor(T) == -2)
{
if (getBalanceFactor(T->right) == -1) //RR
{
L_Rotate(T);
}
else if (getBalanceFactor(T->right) == 1) //LR
{
R_Rotate(T->right);
L_Rotate(T);
}
}
#endif
}
else
{
T->left = insert(T->left, val);
updateHeight(T);
#if defined(AVL) //如果是AVL树,完成插入操作后要检查平衡因子
if (getBalanceFactor(T) == 2)
{
if (getBalanceFactor(T->left) == 1) //RR
{
R_Rotate(T);
}
else if (getBalanceFactor(T->left) == -1) //LR
{
L_Rotate(T->left);
R_Rotate(T);
}
}
#endif
}
return T;
}
void freeTree(link T)
{
if (T->right)
freeTree(T->right);
if(T->left)
freeTree(T->left);
delete T;
}
int getHeight(link T)
{
if (T == NULL)
return 0;
else
return T->height;
}
int getBalanceFactor(link T)
{
return getHeight(T->left)-getHeight(T->right);
}
void updateHeight(link T)
{
T->height = max(getHeight(T->left), getHeight(T->right))+1;
}
标签:names actor put input inpu 一个 include 比较大小 algo
原文地址:http://www.cnblogs.com/messier/p/8004192.html