PAT-1066 Root of AVL Tree(解题报告)

第一次做关于平衡树的题目

第一次做的时候 忘记判断是否是空节点

第二次做,发现LR平衡自己理解错了

对于链式结构理解还是不够深刻,乖乖看书去

题目链接 PAT 1066

附上关于平衡树的四种旋转操作

 

 

代码如下:

#include<stdio.h>

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

int GetHeight(AVLTree A){
	if(A==NULL)
		return -1;
	else
		return A->Height;
}
int Max(int a,int b){
	return (a>b)?a:b;
} 

AVLTree SingleLeftRotation(AVLTree A)
{
    //A必须有一个左子结点B
    //将A与B做左单旋 , 更新AB的高度 , 返回新的根节点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)
{
    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->Left),A->Height)+1;  

    return B;
} 

AVLTree DoubleLeftRightRotation(AVLTree A)
{//A必须有一个左子结点B,且B必须有一个右子节点C
    //将AB与C 做两次单选,返回新的节点C
    A->Left = SingleRightRotation(A->Left);//将BC做右单旋,返回C  

    return SingleLeftRotation(A);//将AC做左单旋,C返回   

}

AVLTree DoubleRightLeftRotation(AVLTree A)
{
    A->Right= SingleLeftRotation(A->Right);

    return SingleRightRotation(A);  

}

AVLTree AVL_Insertion(ElementType X,AVLTree T)
{ /* 将 X插入 AVLAVL 树 T中,并且返回调整后的AVLAVL 树 */
    if(!T){  /* 若插入空树 ,则新建包含一个结点的树*/
        T= (AVLTree)malloc(sizeof(struct AVLTreeNode));
        T->Data = X;
        T->Height = 0;
        T->Left = T->Right =NULL;
    }  

    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(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);//右左双旋
    }   

    T->Height = Max(GetHeight(T->Left),GetHeight(T->Right))+1;  

    return T;
}  

int main(){
	AVLTree T=NULL;
	int i,n,num;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&num);
		T= AVL_Insertion(num,T);
	}
	printf("%d",T->Data);

	return 0;
}
时间: 2024-10-01 06:51:11

PAT-1066 Root of AVL Tree(解题报告)的相关文章

PAT 1066 Root of AVL Tree[AVL树][难]

1066 Root of AVL Tree (25)(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

PAT 1066 Root of AVL Tree

#include <cstdio> #include <cstdlib> class Node { public: Node* L; Node* R; int height; int data; Node(int val, Node* l = NULL, Node* r = NULL, int h = 0): data(val), L(l), R(r), height(h) {} }; inline int height(Node* node) { if (node == NULL

1066. Root of AVL Tree (25)【AVL树】——PAT (Advanced Level) Practise

题目信息 1066. Root of AVL Tree (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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 tha

PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

1066 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

PTA (Advanced Level)1066 Root of AVL Tree

Root of AVL Tree 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. F

PAT 甲级 1066 Root of AVL Tree

https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 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 m

1066. Root of AVL Tree (25)

时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 o

PAT (Advanced Level) 1066. Root of AVL Tree (25)

AVL树的旋转.居然1A了.... 了解旋转方式之后,数据较小可以当做模拟写. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using name

pat(A) 1066. Root of AVL Tree

代码: #include<iostream> #include<cstdio> #include<cmath> #include<stdlib.h> #define Max(a,b) ((a)>(b)?(a):(b)) using namespace std; struct Node { int value; Node* l; Node* r; int BF; //左子树高度 - 右子树高度 void init(int v) { value=v; l=