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

Problem Description

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

Input

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

Output

输出平衡二叉树的树根。

Example Input
5
88 70 61 96 120
Example Output
70

001 #include<stdio.h>
002 #include<string.h>
003 #include<stdlib.h>
004 struct node
005 {
006     int date;
007     int d;
008     struct node *l, *r;
009 };
010 int max(int a, int b)
011 {
012     if(a > b) return a;
013     else return b;
014 }
015 int deep(struct node *root)
016 {
017     if(!root) return -1;
018     else return root->d;
019 }
020 struct node *LL(struct node *root)
021 {
022     struct node *p;
023     p = root->l;
024     root->l = p->r;
025     p->r = root;
026     root->d = max(deep(root->l), deep(root->r)) + 1;
027     return p;
028 }
029 struct node *RR(struct node *root)
030 {
031     struct node *p;
032     p = root->r;
033     root->r = p->l;
034     p->l = root;
035     root->d = max(deep(root->l), deep(root->r)) + 1;
036     return p;
037 }
038 struct node *LR(struct node *root)
039 {
040     root->l = RR(root->l);
041     root = LL(root);
042     return root;
043 }
044 struct node *RL(struct node *root)
045 {
046     root->r = LL(root->r);
047     root = RR(root);
048     return root;
049 }
050 struct node *ins(struct node *root, int key)
051 {
052     if(root == NULL)
053     {
054         root = (struct node *)malloc(sizeof(struct node));
055         root->date = key;
056         root->l = NULL;
057         root->r = NULL;
058         root->d = 0;
059     }
060     else
061     {
062         if(key < root->date)
063         {
064             root->l = ins(root->l, key);
065             if(deep(root->l) - deep(root->r) > 1)
066             {
067                 if(key < root->l->date)
068                 {
069                     root = LL(root);
070                 }
071                 else
072                 {
073                     root = LR(root);
074                 }
075             }
076         }
077         else if(key > root->date)
078         {
079             root->r = ins(root->r, key);
080             if(deep(root->r) - deep(root->l) > 1)
081             {
082                 if(key > root->r->date)
083                 {
084                     root = RR(root);
085                 }
086                 else
087                 {
088                     root = RL(root);
089                 }
090             }
091         }
092     }
093     root->d = max(deep(root->r), deep(root->l)) + 1;
094     return root;
095 }
096 int main()
097 {
098     int n, m;
099     struct node *root;
100     scanf("%d", &n);
101     root = NULL;
102     while(n--)
103     {
104         scanf("%d", &m);
105         root = ins(root, m);
106     }
107     printf("%d\n", root->date);
108     return 0;
109 }
110  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值