Root of AVL Tree 平衡二叉树C语言完成

树5 Root of AVL Tree(25 分)


An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.
Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the root of the resulting AVL tree in one line.

Sample Input 1:

5
88 70 61 96 120

Sample Output 1:

70

Sample Input 2:

7
88 70 61 96 120 90 65

Sample Output 2:

88

大概意思就是建立一个平衡二叉树,最后把根节点打印出来

插入的时候分以下四种情况

LL旋转
LL旋转

RR旋转
这里写图片描述

RL旋转
这里写图片描述

LR旋转
这里写图片描述

以下是接口:

void PrintTree(Node Tree);

Node CreateNode(int V);

Node Insert(Node Tree,int V);

int GetHeight(Node node);

Node SingleLeftRotation(Node tree);

Node SingleRightRotation(Node tree);

Node LeftRightRotaion(Node tree);

Node RightLeftRotation(Node tree);

int Max(int a,int b); 

正式的代码如下

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


typedef struct _node * Node;

struct _node
{
    int Data;
    Node Left,Right;
    int Height;
};

void PrintTree(Node Tree);

Node CreateNode(int V);

Node Insert(Node Tree,int V);

int GetHeight(Node node);
//LL旋转
Node SingleLeftRotation(Node tree);
//RR旋转
Node SingleRightRotation(Node tree);
//LR旋转
Node LeftRightRotaion(Node tree);
//RL旋转
Node RightLeftRotation(Node tree);

int Max(int a,int b); 

int main()
{

//  int arr[]={88 ,70 ,61 ,96, 120 ,90, 65};
    int n;
    scanf("%d",&n);
    Node tree=NULL;
    int input;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&input);
        tree=Insert(tree,input);
    }

    printf("%d",tree->Data);
    return 0;
}


int GetHeight(Node node){

    if(!node){
        return 0;
    }else{
        return node->Height;
    }
}
int Max(int a,int b){
    return a>b?a:b; 
}


Node CreateNode(int V)
{
    Node node = (Node)malloc(sizeof(struct _node));
    node->Left=node->Right=NULL;
    node->Data = V;
    node->Height=0;
    return node;
}



Node Insert(Node tree,int V){
//  printf("=====================\n"); 
    if(!tree)
    {
        tree = CreateNode(V);
    }else
    {
        if(V < tree->Data)
        {
            tree->Left = Insert(tree->Left,V);
            //********************   ×ó±ß¸ß 
            if((GetHeight(tree->Left)-GetHeight(tree->Right))==2) 
            {
                if(V<tree->Left->Data){//LL
                    tree = SingleLeftRotation(tree); 
                }else{//LR 
                    tree =  LeftRightRotaion(tree); 
                } 
            }
        }else if(V > tree->Data)
        {
            tree->Right = Insert(tree->Right,V);
            //********************Óұ߸ßÁË 
            if((GetHeight(tree->Left)-GetHeight(tree->Right))==-2) 
            {


                if(V<tree->Right->Data){//RL

                    tree = RightLeftRotation(tree); 

                }else{//RR 
                    tree =  SingleRightRotation(tree); 
                } 
            }


        }

    }

    tree->Height = 1+Max(GetHeight(tree->Left),GetHeight(tree->Right));
    return tree;
}

Node SingleLeftRotation(Node A){
//  Node Atmp = A;  
//  A = A->Left; 
//  Atmp->Left=A->Right; 
//  A->Right = Atmp; 
//  Atmp->Height=1+Max(Atmp->Left->Height,Atmp->Right->Height);
//  A->Height=1+Max(A->Left->Height,A->Right->Height);
    Node B = A->Left; 
    A->Left = B->Right; 
    B->Right = A; 
    A->Height=1+Max(GetHeight(A->Left),GetHeight(A->Right));
    B->Height=1+Max(GetHeight(B->Left),GetHeight(B->Right));
    return B;
}

Node SingleRightRotation(Node A){
    Node B = A->Right; // Left
    A->Right = B->Left;
    B->Left = A; 
    A->Height=1+Max(GetHeight(A->Left),GetHeight(A->Right));
    B->Height=1+Max(GetHeight(B->Left),GetHeight(B->Right));
    return B;

} 
Node LeftRightRotaion(Node A){
    A->Left = SingleRightRotation(A->Left);
    A=SingleLeftRotation(A); 
    return A; 
}

Node RightLeftRotation(Node A){
    A->Right=SingleLeftRotation(A->Right);
    A =  SingleRightRotation(A);
    return A; 
}





























以下是一个简单的C语言实现平衡二叉树的代码,其中包括了对于四种不平衡情况的处理和递归添加新数据的方法: ```c #include <stdio.h> #include <stdlib.h> typedef struct node { int data; struct node *left; struct node *right; int height; } node; int height(node *n) { if (n == NULL) { return 0; } return n->height; } int max(int a, int b) { return (a > b) ? a : b; } node *new_node(int data) { node *n = (node *)malloc(sizeof(node)); n->data = data; n->left = NULL; n->right = NULL; n->height = 1; return n; } node *right_rotate(node *y) { node *x = y->left; node *T2 = x->right; x->right = y; y->left = T2; y->height = max(height(y->left), height(y->right)) + 1; x->height = max(height(x->left), height(x->right)) + 1; return x; } node *left_rotate(node *x) { node *y = x->right; node *T2 = y->left; y->left = x; x->right = T2; x->height = max(height(x->left), height(x->right)) + 1; y->height = max(height(y->left), height(y->right)) + 1; return y; } int get_balance(node *n) { if (n == NULL) { return 0; } return height(n->left) - height(n->right); } node *insert(node *n, int data) { if (n == NULL) { return new_node(data); } if (data < n->data) { n->left = insert(n->left, data); } else if (data > n->data) { n->right = insert(n->right, data); } else { return n; } n->height = 1 + max(height(n->left), height(n->right)); int balance = get_balance(n); if (balance > 1 && data < n->left->data) { return right_rotate(n); } if (balance < -1 && data > n->right->data) { return left_rotate(n); } if (balance > 1 && data > n->left->data) { n->left = left_rotate(n->left); return right_rotate(n); } if (balance < -1 && data < n->right->data) { n->right = right_rotate(n->right); return left_rotate(n); } return n; } void pre_order(node *n) { if (n != NULL) { printf("%d ", n->data); pre_order(n->left); pre_order(n->right); } } int main() { node *root = NULL; root = insert(root, 10); root = insert(root, 20); root = insert(root, 30); root = insert(root, 40); root = insert(root, 50); root = insert(root, 25); printf("Preorder traversal of the constructed AVL tree is: \n"); pre_order(root); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值