struct node *LL(struct node *root)
{
struct node *a = root;
struct node *b = root->left;
a->left = b->right;
b->right = a;
a->high = max(height(a->left), height(a->right))+1;
b->high = max(height(b->left), height(b->right))+1;
return b;
}
struct node *RR(struct node *root)
{
struct node *a = root;
struct node *b = root->right;
a->right = b->left;
b->left = a;
a->high = max(height(a->left), height(a->right))+1;
b->high = max(height(b->left), height(b->right))+1;
return b;
}
struct node *LR(struct node *root)
{
struct node *a = root;
struct node *b = root->left;
struct node *c = b->right;
a->left = c->right;
b->right = c->left;
c->left = b;
c->right = a;
a->high = max(height(a->left), height(a->right))+1;
b->high = max(height(b->left), height(b->right))+1;
c->high = max(height(c->left), height(c->right))+1;
return c;
}
或者
struct node *LR(struct node *root)
{
root->left = RR(root->left);
return LL(root);
}
struct node *RL(struct node *root)
{
struct node *a = root;
struct node *b = root->right;
struct node *c = b->left;
a->right = c->left;
b->left = c->right;
c->left = a;
c->right = b;
a->high = max(height(a->left), height(a->right))+1;
b->high = max(height(b->left), height(b->right))+1;
c->high = max(height(c->left), height(c->right))+1;
return c;
}
或者
struct node *RL(struct node *root)
{
root->right = LL(root->right);
return RR(root);
}
数据结构实验之查找二:平衡二叉树
Problem Description
根据给定的输入序列建立一棵平衡二叉树,求出建立的平衡二叉树的树根。
Input
输入一组测试数据。数据的第1行给出一个正整数N(n <= 20),N表示输入序列的元素个数;第2行给出N个正整数,按数据给定顺序建立平衡二叉树。
Output
输出平衡二叉树的树根。
Sample Input
5
88 70 61 96 120
Sample Output
70
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
int data;
int high;
struct node *left;
struct node *right;
};
int max(int a, int b)
{
if(a>b)
return a;
else
return b;
}
int height(struct node *root)
{
if(root==NULL) return 0;
else
return root->high;
}
struct node *LL(struct node *root)
{
struct node *a = root;
struct node *b = root->left;
a->left = b->right;
b->right = a;
a->high = max(height(a->left), height(a->right))+1;
b->high = max(height(b->left), height(b->right))+1;
return b;
}
struct node *RR(struct node *root)
{
struct node *a = root;
struct node *b = root->right;
a->right = b->left;
b->left = a;
a->high = max(height(a->left), height(a->right))+1;
b->high = max(height(b->left), height(b->right))+1;
return b;
}
struct node *LR(struct node *root)
{
struct node *a = root;
struct node *b = root->left;
struct node *c = b->right;
a->left = c->right;
b->right = c->left;
c->left = b;
c->right = a;
a->high = max(height(a->left), height(a->right))+1;
b->high = max(height(b->left), height(b->right))+1;
c->high = max(height(c->left), height(c->right))+1;
return c;
}
struct node *RL(struct node *root)
{
struct node *a = root;
struct node *b = root->right;
struct node *c = b->left;
a->right = c->left;
b->left = c->right;
c->left = a;
c->right = b;
a->high = max(height(a->left), height(a->right))+1;
b->high = max(height(b->left), height(b->right))+1;
c->high = max(height(c->left), height(c->right))+1;
return c;
}
struct node *creat(struct node *root, int x)
{
if(root==NULL)
{
root = (struct node *)malloc(sizeof(struct node));
root->data = x;
root->high = 0;
root->left = root->right = NULL;
}
else
{
if(x<root->data)
{
root->left = creat(root->left, x);
if(height(root->left)-height(root->right)==2)
{
if(x<(root->left)->data)
{
root = LL(root);
}
else
{
root = LR(root);
}
}
}
else
{
root->right = creat(root->right, x);
if(height(root->left)-height(root->right)==-2)
{
if(x>(root->right)->data)
{
root = RR(root);
}
else
{
root = RL(root);
}
}
}
}
root->high = max(height(root->left), height(root->right))+1;//更新高度(从下而上);
return root;
}
int main()
{
int n, i, t;
struct node *root;
scanf("%d", &n);
{
//root = (struct node *)malloc(sizeof(struct node));
root = NULL;
for(i=0; i<n; i++)
{
scanf("%d", &t);
root = creat(root, t);
}
printf("%d\n", root->data);
}
return 0;
}