1066 Root of AVL Tree (25 分) + 测试要点

17 篇文章 0 订阅
10 篇文章 0 订阅

题目描述

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 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.
(……)

测试要点

  1. 我个人是分不清左右的,所以对我来说最重要的是测试左右旋函数L、R。测出一个是对的另一个镜像即可

    3
    1 2 3
    
  2. 特殊点:只有一个节点的时候

  3. 测完我的代码就通过了,这里多提供一组测试样例,正确答案是88

    12
    88 70 61 96 120 88 70 61 96 120 90 65
    

代码如下:

#include<iostream>
#include<fstream>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#define MAX 100
using namespace std;

int n;
int data[MAX];

struct Node{
    Node *left, *right;
    int data;
    int h;
};

int getHeight(Node* root) {
    if(root== nullptr) {
        return 0;
    }
    return root->h;
}

void updateHeight(Node* &root) {
    if(root->left== nullptr && root->right== nullptr) {
        root->h=1;
    } else {
        root->h= max(getHeight(root->left), getHeight(root->right))+1;
    }
}

int getBalanceFactor(Node* root) {
    return getHeight(root->left)- getHeight(root->right);
}

void R(Node* &root) {
    Node* tmp = root->left;
    root->left = tmp->right;
    tmp->right=root;
    updateHeight(root);
    updateHeight(tmp);
    root=tmp;
}

void L(Node* &root) {
    Node* tmp = root->right;
    root->right = tmp->left;
    tmp->left=root;
    updateHeight(root);
    updateHeight(tmp);
    root=tmp;
}

Node* newNode(int data) {
    Node* node=new Node;
    node->left=node->right= nullptr;
    node->data=data;
    node->h=1;
    return node;
}

void insert(Node* &root, int data) {
    if(root==nullptr) {
        root = newNode(data);
//        root->data=data;
//        updateHeight(root); // root->h=1;
        return;
    }
    if(data<root->data) {
        insert(root->left, data);
        updateHeight(root);
        if(getBalanceFactor(root)==2) {
            if(getBalanceFactor(root->left)>0) {
                R(root);
            } else {
                L(root->left);
                R(root);
            }
        }
    } else {
        insert(root->right, data);
        updateHeight(root);
        if(getBalanceFactor(root)==-2) {
            if(getBalanceFactor(root->right)>0) {
                R(root->right);
                L(root);
            } else {
                L(root);
            }
        }
    }
}

Node* create(int data[], int n) {
    Node* root= nullptr;
    for(int i=0; i<n; i++) {
        insert(root, data[i]);
    }
    return root;
}

int main() {
    fstream fin;
    fin.open("tmp4.txt");
    fin>>n;
    for(int i=0;i<n;i++) {
        fin>>data[i];
    }

//    cin>>n;
//    for(int i=0;i<n;i++) {
//        cin>>data[i];
//    }

    Node* root= create(data, n);

    cout<<root->data;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值