《算法笔记》9.5小节——数据结构专题(2)->平衡二叉树(AVL) 问题 A: 算法9-9~9-12:平衡二叉树的基本操作

题目描述

平衡二叉树又称AVL树,它是一种具有平衡因子的特殊二叉排序树。平衡二叉树或者是一棵空树,或者是具有以下几条性质的二叉树:

1.       若它的左子树不空,则左子树上所有结点的值均小于它的根节点的值;

2.       若它的右子树不空,则右子树上所有结点的值均大于它的根节点的值;

3.       它的左右子树也分别为平衡二叉树,且左子树和右子树的深度之差的绝对值不超过1。

若将二叉树上结点的平衡因子定义为该结点的左子树深度减去它的右子树的深度,则平衡二叉树上的所有结点的平衡因子只可能为-1、0和1。只要二叉树上有一个结点的平衡因子的绝对值大于1,则这棵二叉树就是不平衡的。

通过平衡二叉树的性质不难得知,其插入、删除、查询都操作的时间复杂度均为O(log2n)。

维持平衡二叉树性质的操作可以被称为旋转。其中平衡二叉树的右旋处理可以描述如下:

而其左旋处理与右旋正好相反,可以描述如下:

在本题中,读入一串整数,首先利用这些整数构造一棵平衡二叉树。另外给定多次查询,利用构造出的平衡二叉树,判断每一次查询是否成功。

输入

输入的第一行包含2个正整数n和k,分别表示共有n个整数和k次查询。其中n不超过500,k同样不超过500。

第二行包含n个用空格隔开的正整数,表示n个整数。

第三行包含k个用空格隔开的正整数,表示k次查询的目标。

输出

只有1行,包含k个整数,分别表示每一次的查询结果。如果在查询中找到了对应的整数,则输出1,否则输出0。

请在每个整数后输出一个空格,并请注意行尾输出换行。

样例输入
8 3
1 3 5 7 8 9 10 15
9 2 5
样例输出
1 0 1 
提示

在本题中,首先需要按照题目描述中的算法完成平衡二叉树的构造过程,之后需要通过在平衡二叉树中的不断向下查找,将需要查询的值与当前节点的值进行比较,直到确定被查询的值是否存在。

通过课本中的性能分析部分,不难发现平衡二叉树的查找、插入、删除等操作的时间复杂度均为O(log2n),这是通过利用旋转操作使二叉树保持平衡状态而保证的。

分析:平衡二叉树的实现。我用了一个模板。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct Node {
    int key, h;//key代表值,h代表当前节点的高度
    struct Node *lchild, *rchild;
} Node;

Node __NIL;//用一个虚拟指针NIL代表NULL
#define NIL (&__NIL)
#define K(n) (n->key)//取得节点n的值
#define H(n) (n->h)//取得n的高度
#define L(n) (n->lchild)//取得n左孩子的地址
#define R(n) (n->rchild)//取得n右孩子的地址
__attribute__((constructor))//这是 GCC 的一个扩展属性,表示 init_NIL() 会在 main() 运行前自动执行,用于初始化 NIL 节点。确保 NIL 在程序开始时就已初始化,避免 NULL 访问错误。
void init_NIL() {
    NIL->key = -1;//NIL代表的节点key设为 -1
    NIL->h = 0;//代表空节点的高度
    NIL->lchild = NIL->rchild = NIL;//左右子节点都指向NIL自己
    return ;
}

Node *getNewNode(int key) {//创建新节点
    Node *p = (Node *)malloc(sizeof(Node));
    p->key = key;
    p->h = 1;
    p->lchild = p->rchild = NIL;
    return p;
}

void update_height(Node *root) {//更新节点高度
    H(root) = (H(L(root)) > H(R(root)) ? H(L(root)) : H(R(root))) + 1;
    return ;
}

Node *left_rotate(Node *root) {//左旋操作
//    printf("left rotate : %d\n", root->key);
    Node *new_node = root->rchild;
    root->rchild = new_node->lchild;
    new_node->lchild = root;
    update_height(root);
    update_height(new_node);
    return new_node;
}

Node *right_rotate(Node *root) {//右旋操作
//    printf("right rotate : %d\n", root->key);
    Node *new_node = root->lchild;
    root->lchild = new_node->rchild;
    new_node->rchild = root;
    update_height(root);
    update_height(new_node);
    return new_node;
}

const char *type_str[5] = {//旋转类型
    "",
    "maintain type : LL",
    "maintain type : LR",
    "maintain type : RR",
    "maintain type : RL"
};

Node *maintain(Node *root) {//用于插入或删除节点后,检查根节点高度,以及输出旋转类型
    if (abs(H(L(root)) - H(R(root))) <= 1) return root;
    int type = 0;
    if (H(L(root)) > H(R(root))) {//第一个是L,左子树失衡
        if (H(R(L(root))) > H(L(L(root)))) {//第二个是R,左子树的右子树失衡
            root->lchild = left_rotate(root->lchild);
            type += 1;
        }
        root = right_rotate(root);//如果第二个不是R,则是LL旋转
        type += 1;
    } else {//第一个不是L,是R,右子树失衡
        type = 2;
        if (H(L(R(root))) > H(R(R(root)))) {//第二个是L,右子树的左子树失衡
            root->rchild = right_rotate(root->rchild);
            type += 1;
        }
        root = left_rotate(root);//如果第二个不是L,则是RR旋转
        type += 1;
    }
//    printf("%s\n", type_str[type]);
    return root;
}

Node *insert(Node *root, int key) {//插入新节点
    if (root == NIL) return getNewNode(key);
    if (root->key == key) return root;
    if (key < root->key) root->lchild = insert(root->lchild, key);
    else root->rchild = insert(root->rchild, key);
    update_height(root);
    return maintain(root);
}

Node *predecessor(Node *root) {//用于删除节点时,找到该节点的前驱节点
    Node *temp = root->lchild;
    while (temp->rchild != NIL) temp = temp->rchild;
    return temp;
}

Node *erase(Node *root, int key) {//删除节点
    if (root == NIL) return root;
    if (key < root->key) root->lchild = erase(root->lchild, key);
    else if (key > root->key) root->rchild = erase(root->rchild, key);
    else {
        if (root->lchild == NIL || root->rchild == NIL) {//有一个孩子,或者一个孩子都没有
            Node *temp = root->lchild != NIL ? root->lchild : root->rchild;
            free(root);//因为用了虚拟空节点NIL,所以可以这么操作
            return temp;
        } else {//有两个孩子
            Node *temp = predecessor(root);
            root->key = temp->key;
            root->lchild = erase(root->lchild, temp->key);
        }
    }
    update_height(root);
    return maintain(root);//删除后要保持树的平衡
}

Node *find(Node *root, int key) {//查找操作
    if (root == NIL) return NIL;
    if (root->key == key) return root;
    if (key < root->key) return find(root->lchild, key);
    return find(root->rchild, key);
}

void clear(Node *root) {//回收空间
    if (root == NIL) return ;
    clear(root->lchild);
    clear(root->rchild);
    free(root);
    return ;
}

void output(Node *root) {//输出测试
    if (root == NIL) return ;
    printf("(%d[%d] | %d, %d)\n",
        K(root), H(root),
        K(L(root)), K(R(root))
    );
    output(root->lchild);
    output(root->rchild);
    return ;
}

int main() {
    #ifdef test
    freopen("in.txt","r",stdin);
    //freopen("in.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    int n,k;scanf("%d%d",&n,&k);
    Node *root=NIL;
    for(int i=0;i<n;++i)
    {
        int x;scanf("%d",&x);
        root=insert(root,x);
    }
    for(int i=0;i<k;++i)
    {
        int x;scanf("%d",&x);
        Node *temp=find(root,x);
        if(temp!=NIL)printf("1 ");
        else printf("0 ");
    }
    printf("\n");
    free(root);
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值