数据结构实验之查找二:平衡二叉树

题目描述

根据给定的输入序列建立一棵平衡二叉树,求出建立的平衡二叉树的树根。

输入

输入一组测试数据。数据的第1行给出一个正整数N(n <= 20),N表示输入序列的元素个数;第2行给出N个正整数,按数据给定顺序建立平衡二叉树。

输出

输出平衡二叉树的树根。

题解:建一棵平衡二叉树会出现四种不平衡的情况,分别是左左,左右,右右,右左,再就是见平衡二叉树实在建二叉排序树的基础上建的。如果你回建排序树,那么只要你了解了在建树时出现不平衡的情况时,怎样通过旋转来解决,那么平衡二叉树你就拿下了。

示例输入

5
88 70 61 96 120

示例输出

70
#include<iostream>
using namespace std;
typedef struct node
{
    int data;
    int dp;
    struct node *lchild,*rchild;
}Tree;
int deep(Tree *root)//这个是求深度的函数,每插入一个节点,那么就要更新该节点上层所有节点的深度。
{
    if(!root)
        return -1;
    else
        return root->dp;
}
int max(int x,int y)//每个节点的度就是左右还在节点深度的最大值再加一。
{
    if(x>y)
        return x;
    else
        return y;
}
void front1(Tree *root)//这只是一个检验函数,检查你树建的是否正确。
{
    if(root)
    {
        cout<<root->data<<" ";
        front1(root->lchild);
        front1(root->rchild);
    }
}
Tree *LL(Tree *root)//这就是左左的情况,就是新插入节点的值比出现不平衡节点的左子树上的值要小。
{
    Tree *key=root->lchild;//root节点(也就是出现不平衡状况的节点),它会成为root->lchild节点的右节点,而root->lchild节点的右节点会成为root节点的左节点
    root->lchild=key->rchild;
    key->rchild=root;
    root->dp=max(deep(root->lchild),deep(root->rchild))+1;//旋转完之后,要更新平衡后节点的深度。
    key->dp=max(deep(key->lchild),deep(key->rchild))+1;
    return key;
}
Tree *RR(Tree *root)//右右的情况跟左左的情况差不多。
{
    Tree *key1=root->rchild;
    root->rchild=key1->lchild;
    root->lchild=key1;
    root->dp=max(deep(root->lchild),deep(root->rchild))+1;
    key1->dp=max(deep(key1->lchild),deep(key1->rchild))+1;
    return key1;
}
Tree *LR(Tree *root)//左右的情况就不一样了,它会进行两次旋转,先对出现不平衡节点(root)的左孩子节点进行左旋转,然后在对root节点进行右旋转。
   root->lchild=RR(root->lchild);
    return LL(root);
}
Tree *RL(Tree *root)
{
    root->rchild=LL(root->rchild);
    return RR(root);
}
Tree *creat(Tree *root,int x)//建树的过程
    if(!root)
    {
        root=new Tree();
        root->data=x;
        root->lchild=root->rchild=NULL;
    }
    else
    {
        if(x<root->data)
        {
            root->lchild=creat(root->lchild,x);
            if(deep(root->lchild)-deep(root->rchild)>1)
            {
                if(x<root->lchild->data)
                    root=LL(root);
                else
                    root=LR(root);
            }
        }
        else
        {
            root->rchild=creat(root->rchild,x);
            if(deep(root->rchild)-deep(root->lchild)>1)
            {
                if(x>root->rchild->data)
                {
                    root=RR(root);
                }
                else
                {
                    root=RL(root);
                }
            }
        }
    }
    root->dp=max(deep(root->lchild),deep(root->rchild))+1;//通过递归来更新节点的深度。
   return root;
}
int main()
{
    int n,i,x;
    Tree *root=NULL;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>x;
        root=creat(root,x);
        front1(root);
        cout<<endl;
    }
    cout<<root->data<<endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值