PAT1066 Root of AVL Tree (25 分)

1.新结点也即叶子结点的高度是1而不是0。 newnode->height = 1;
2.判断树形时用的是平衡因子,而不是树的高度。。
3.根结点bf是2或者-2时才需要调整,其它情况是平衡的不需要调整。所以注意==2判断完了是else if判断==-2而不是直接else。即else if(getBalancedFactor(root) == -2){}
4.root应该初始化为NULL,否则一开始插入根结点时就不对,导致多处报错Thread 1: EXC_BAD_ACCESS。 node *root = NULL; create(root, value, N); 

5.每次插入新结点,都应该更新根结点的高度。不仅仅是在左旋右旋函数内部更新高度,在往左子树右子树插入新结点后也需要调用updateHeight更新树高。

#include <cstdio>
#include <algorithm>
#define MAXN 25
using namespace std;

int N;
int value[MAXN];

struct node{
    int height;
    int data;
    node *lchild;
    node *rchild;
};

int getHeight(node *root){
    if (root == NULL) {
        return 0;
    }
    return root->height;
}

void updateHeight(node *root){
    root->height = (max(getHeight(root->lchild), getHeight(root->rchild)) + 1);
}

int getBalancedFactor(node *root){
    return getHeight(root->lchild) - getHeight(root->rchild);
}

node* newNode(int x){
    node *newnode = new node;
    newnode->data = x;
    newnode->height = 1;//0->1
    newnode->lchild = NULL;
    newnode->rchild = NULL;
    return newnode;
}

void Rrotate(node *&root){
    node *temp = root->lchild;//Thread 1: EXC_BAD_ACCESS
    root->lchild = temp->rchild;
    temp->rchild = root;
    root = temp;
    updateHeight(root->rchild);
    updateHeight(root);
}

void Lrotate(node *&root){
    node *temp = root->rchild;
    root->rchild = temp->lchild;//Thread 1: EXC_BAD_ACCESS
    temp->lchild = root;
    root = temp;
    updateHeight(root->lchild);
    updateHeight(root);
}

void insert(node *&root, int value){
    if (root == NULL) {//如果一开始根结点root不是初始化为NULL,而是node *root = new node;则一开始就错了
        root = newNode(value);
        return;
    }
    if (root->data == value) {//Thread 1: EXC_BAD_ACCESS
        return;
    }
    
    if (value < root->data) {
        insert(root->lchild, value);
        updateHeight(root);//
        if (getBalancedFactor(root) == 2) {//判断树形不是用getHeight,而是用getBalancedFactor
            if (getBalancedFactor(root->lchild) == 1) {//LL
                Rrotate(root);
            }else{//LR
                Lrotate(root->lchild);
                Rrotate(root);
            }
        }else if(getBalancedFactor(root) == -2){//除了2,-2,还有其它平衡的情况比如1、-1、0,所以不能直接else,而应该用else if。
            if (getBalancedFactor(root->rchild) == -1) {//RR
                Lrotate(root);
            }else{//RL
                Rrotate(root->rchild);
                Lrotate(root);
            }
        }
    }else{
        insert(root->rchild, value);
        updateHeight(root);//
        if (getBalancedFactor(root) == 2) {
            if (getBalancedFactor(root->lchild) == 1) {//LL
                Rrotate(root);
            }else{//LR
                Lrotate(root->lchild);
                Rrotate(root);
            }
        }else if(getBalancedFactor(root) == -2){
            if (getBalancedFactor(root->rchild) == -1) {//RR
                Lrotate(root);
            }else{//RL
                Rrotate(root->rchild);
                Lrotate(root);
            }
        }
    }
}

void create(node *&root, int a[], int n){
    for (int i=0; i<n; i++) {
        insert(root, a[i]);
    }
}

int main(int argc, const char * argv[]) {
    scanf("%d", &N);
    for (int i=0; i<N; i++) {
        scanf("%d", &value[i]);
    }
    
//    node *root = new node;//引起Thread 1: EXC_BAD_ACCESS
    node *root = NULL;//root应该初始化为NULL,否则一开始插入根结点时就不对。
    create(root, value, N);
    printf("%d", root->data);
    
    return 0;
}


//Sample Input 1:
//5
//88 70 61 96 120
//Sample Input 1:
//70
//
//Sample Input 2:
//7
//88 70 61 96 120 90 65
//Sample Input 2:
//88

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值