PTA 04-树5 Root of AVL Tree
一直段错误!求救SOS!
求救Code
我的代码
:参考了bilibili up主(Slatter)视频
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include<cmath>
using namespace std;
typedef struct TreeNode *Tree;
struct TreeNode{
int val;
Tree left,right;
int height;
};
int Max(int a,int b)
{
return a>b?a:b;
}
//获得树高,以防有树高为0的情况,height无法直接获得
int getHeight(Tree root){
if(root==NULL)return -1;//树空
else return root->height;
}
//计算平衡因子->用于判断是LL,RR,LR,RL类型
int getBalanceFactor(Tree root){
return getHeight(root->left)-getHeight(root->right);
}
//左旋
Tree LL(Tree root){
Tree temp=root->right;
root->left=temp->left;
temp->left=root;
root->height=Max(getHeight(root->left),getHeight(root->right))+1;
temp->height=Max(getHeight(temp->left),getHeight(temp->right))+1;
return temp;
}
//右旋
Tree RR(Tree root){
Tree temp=root->left;
root->left=temp->right;
temp->right=root;
root->height=Max(getHeight(root->left),getHeight(root->right))+1;
temp->height=Max(getHeight(temp->left),getHeight(temp->right))+1;
return temp;
}
//左右旋->左旋左子树,右旋整棵树
Tree LR(Tree root){
root->left=LL(root->left);
return RR(root);
}
//右左旋->右旋右子树,左旋整棵树
Tree RL(Tree root){
root->right=RR(root->right);
return LL(root);
}
//插入AVL树
Tree Insert(Tree root,int v){
if(!root){//空节点
root=(Tree)malloc(sizeof(struct TreeNode));
root->val=v;
root->left=root->right=NULL;
root->height=0;
}
if(v<root->val){
root->left=Insert(root->left,v);
if(getBalanceFactor(root)==2){//Lx
if(v<root->left->val){//RR
root=RR(root);
}else{
//LR
root=LR(root);
}
}
}else if(v>root->val){
root->right=Insert(root->right,v);
if(getBalanceFactor(root)==-2){//Rx
if(v<root->right->val){//RL
root=RL(root);
}else{
root=LL(root);//LL
}
}
}
root->height=Max(getHeight(root->left),getHeight(root->right))+1;
return root;
}
int main(){
int N,v;
scanf("%d",&N);
Tree root=NULL;
for(int i=0;i<N;i++){
scanf("%d",&v);
root=Insert(root,v);
}
printf("%d",root->val);
return 0;
}
最后通过的Code
转载CSDN:
转载链接
#include <stdio.h>
#include <stdlib.h>
typedef struct TNode *AVLTree;
typedef int ElementType;
struct TNode{
ElementType Data;
AVLTree Left;
AVLTree Right;
int Height;
};
AVLTree Insert(AVLTree T,ElementType X);
int Max(int a,int b);
int GetHeight(AVLTree T);
AVLTree SingleLeftRotation ( AVLTree A );
AVLTree DoubleLeftRightRotation ( AVLTree A );
AVLTree SingleRightRotation(AVLTree T);
AVLTree DoubleRightLeftRotation(AVLTree T);
int main()
{
int N,X;
AVLTree Tree=NULL;
scanf("%d",&N);
for(int i=0;i<N;i++)
{
scanf("%d",&X);
Tree = Insert(Tree,X);
}
printf("%d\n",Tree->Data);
return 0;
}
int Max(int a,int b)
{
return a>b?a:b;
}
int GetHeight(AVLTree T)
{
if(!T) return -1;
else return T->Height;
}
AVLTree Insert(AVLTree T,ElementType X)
{
/*对于输入样例二:88 70 61 96 120 90 65
1.读入元素X,若为空树新建根节点,否则,与当前结点比较,
若X小于当前结点,递归插入左子树,
若大于,递归插入右子树;
2.插入成功后,更新该新结点树高;
3.从插入时遍历的路线,从新结点一个个往回遍历,判断,是否存在麻烦结点;
(麻烦结点即为平衡被破坏的结点,即左右子树高度相差2及以上)
4.若存在,則在最先找出的麻烦结点进行调整:
(左单旋,左-右双旋,右单旋,右-左双旋)
若不存在:跟新树高即可;
*/
if(!T)
{
T=(AVLTree)malloc(sizeof(struct TNode));
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){
// printf("T=%d\n",T->Data);
if(X< T->Left->Data)
T = SingleLeftRotation(T);
else
T = DoubleLeftRightRotation(T);
}
}else if(X>T->Data){
T->Right = Insert(T->Right,X);
if(GetHeight(T->Right)-GetHeight(T->Left)==2){
// printf("T=%d\n",T->Data);
if(X>T->Right->Data)
T = SingleRightRotation(T);
else
T = DoubleRightLeftRotation(T);
}
}
//更新树高
T->Height = Max(GetHeight(T->Left),GetHeight(T->Right))+1;
return T;
}
AVLTree SingleLeftRotation ( AVLTree A )
{ /* 注意:A必须有一个左子结点B */
/* 将A与B做左单旋,更新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 DoubleLeftRightRotation ( AVLTree A )
{ /* 注意:A必须有一个左子结点B,且B必须有一个右子结点C */
/* 将A、B与C做两次单旋,返回新的根结点C */
/* 将B与C做右单旋,C被返回 */
A->Left = SingleRightRotation(A->Left);
/* 将A与C做左单旋,C被返回 */
return SingleLeftRotation(A);
}
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->Right), A->Height ) + 1;
return B;
}
AVLTree DoubleRightLeftRotation(AVLTree A)
{
A->Right = SingleLeftRotation(A->Right);
return SingleRightRotation(A);
}
不明白为啥我的会报“段错误”!求大神指点