1773 Problem A 算法9-9~9-12:平衡二叉树的基本操作

问题 A: 算法9-9~9-12:平衡二叉树的基本操作
时间限制: 1 Sec 内存限制: 32 MB
献花: 32 解决: 23
[献花][花圈][TK题库]
题目描述
平衡二叉树又称AVL树,它是一种具有平衡因子的特殊二叉排序树。平衡二叉树或者是一棵空树,或者是具有以下几条性质的二叉树:
1. 若它的左子树不空,则左子树上所有结点的值均小于它的根节点的值;
2. 若它的右子树不空,则右子树上所有结点的值均大于它的根节点的值;
3. 它的左右子树也分别为平衡二叉树,且左子树和右子树的深度之差的绝对值不超过1。
若将二叉树上结点的平衡因子定义为该结点的左子树深度减去它的右子树的深度,则平衡二叉树上的所有结点的平衡因子只可能为-1、0和1。只要二叉树上有一个结点的平衡因子的绝对值大于1,则这棵二叉树就是不平衡的。
通过平衡二叉树的性质不难得知,其插入、删除、查询都操作的时间复杂度均为O(log2n)。
维持平衡二叉树性质的操作可以被称为旋转。其中平衡二叉树的右旋处理可以描述如下:
《1773 Problem A 算法9-9~9-12:平衡二叉树的基本操作》
而其左旋处理与右旋正好相反,可以描述如下:

在本题中,读入一串整数,首先利用这些整数构造一棵平衡二叉树。另外给定多次查询,利用构造出的平衡二叉树,判断每一次查询是否成功。
《1773 Problem A 算法9-9~9-12:平衡二叉树的基本操作》

输入
输入的第一行包含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 <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>

using namespace std;
const int MaxN = 510;

typedef struct tnode
{
    int v;
    int high;
    struct tnode * lchild;
    struct tnode * rchild;
}TNode;

TNode * NewNode(int val)
{
    TNode * node = new TNode;
    node->high = 1;
    node->v = val;
    node->lchild = node->rchild = NULL;
    return node;
}

int Ghight(TNode * Root)
{
    if(!Root)return 0;
    return Root->high;
}

void updateHight(TNode * root)
{
    root->high = max(Ghight(root->lchild),Ghight(root->rchild)) + 1;
}

int GbFactor(TNode * root)
{
    if(!root) return 0;
    return Ghight(root->lchild) - Ghight(root->rchild);
}


void R(TNode * &Root)
{
    TNode * tmp = Root->lchild;
    Root->lchild = tmp->rchild;
    tmp->rchild = Root;

    updateHight(Root);
    updateHight(tmp);
    Root = tmp;
}

void L(TNode * &Root)
{
    TNode * tmp = Root->rchild;
    Root->rchild = tmp->lchild;
    tmp->lchild = Root;

    updateHight(Root);
    updateHight(tmp);
    Root = tmp;
}

void Insert(TNode *&Root,int val)
{
    if(!Root)
    {
        Root = NewNode(val);
        return;
    }

    if(Root->v == val) return;
    else if(Root->v > val)
    {
        Insert(Root->lchild,val);
        updateHight(Root);
        if(GbFactor(Root) == 2)
        {
            if(GbFactor(Root->lchild) == 1)
            {
                R(Root);
            }
            else if(GbFactor(Root->lchild) == -1)
            {
                L(Root->lchild);
                R(Root);
            }
        }
    }
    else
    {
        Insert(Root->rchild,val);
        updateHight(Root);
        if(GbFactor(Root) == -2)
        {
            if(GbFactor(Root->rchild) == -1)
            {
                L(Root);
            }
            else if(GbFactor(Root->rchild) == 1)
            {
                R(Root->rchild);
                L(Root);
            }
        }
    }
}


TNode * CreateTree(int seq[],int n)
{
    TNode * root = NULL;
    for(int i=0;i<n;++i)
        Insert(root,seq[i]);

    return root;
}

void DestroyTree(TNode * &Root)
{
    if(!Root)return;
    DestroyTree(Root->lchild);
    DestroyTree(Root->rchild);
    delete Root;
    Root = NULL;
}

void Search(TNode * root,int val,bool &flag)
{
    if(!root)return;
    if(root->v == val)
    {
        flag = true;
        return;
    }
    else if(!flag && root->v > val)
    {
        Search(root->lchild,val,flag);
    }
    else if(!flag)
    {
        Search(root->rchild,val,flag);
    }
}

int main()
{
#ifdef _Debug
freopen("data.txt","r+",stdin);
#endif

    std::ios::sync_with_stdio(false);

    int n,k,Data[MaxN];
    while(cin >> n >>k)
    {
        for(int i=0;i<n;++i)
        {
            cin >> Data[i];
        }
        TNode * root = CreateTree(Data,n);

        while(k--)
        {
            bool flag = false;
            int s;
            cin >> s;
            Search(root,s,flag);
            if(flag)
                cout<<"1 ";
            else
                cout << "0 ";
        }
        DestroyTree(root);
        cout << endl;
    }

    return 0;
}

/************************************************************** Problem: 1773 User: Sharwen Language: C++ Result: 升仙 Time:9 ms Memory:1832 kb ****************************************************************/
    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/fantasydreams/article/details/79341098
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞