如何构建一棵二叉平衡AVL树

简介: 如何构建一棵二叉平衡AVL树
#include <stdio.h>
#include "avl.h"
/**
a) Left Left Case
关注公众号:你不知道的东东
与博主联系
T1, T2, T3 and T4 are subtrees.
         z                                      y
        / \                                   /   \
       y   T4      Right Rotate (z)          x      z
      / \          - - - - - - - - ->      /  \    /  \
     x   T3                               T1  T2  T3  T4
    / \
  T1   T2
b) Left Right Case
     z                               z                           x
    / \                            /   \                        /  \
   y   T4  Left Rotate (y)        x    T4  Right Rotate(z)    y      z
  / \      - - - - - - - - ->    /  \      - - - - - - - ->  / \    / \
T1   x                          y    T3                    T1  T2 T3  T4
    / \                        / \
  T2   T3                    T1   T2
c) Right Right Case
  z                                y
 /  \                            /   \
T1   y     Left Rotate(z)       z      x
    /  \   - - - - - - - ->    / \    / \
   T2   x                     T1  T2 T3  T4
       / \
     T3  T4
d) Right Left Case
   z                            z                            x
  / \                          / \                          /  \
T1   y   Right Rotate (y)    T1   x      Left Rotate(z)   z      y
    / \  - - - - - - - - ->     /  \   - - - - - - - ->  / \    / \
   x   T4                      T2   y                  T1  T2  T3  T4
  / \                              /  \
T2   T3                           T3   T4
*/
int main() {
    AVLTree tree=NULL;
    tree =insert(tree,1);
    pre_order(tree);
    printf("\n");
    tree =insert(tree,2);
    pre_order(tree);
    printf("\n");
    tree =insert(tree,3);
    pre_order(tree);
    printf("\n");
    tree =insert(tree,4);
    pre_order(tree);
    printf("\n");
    tree =insert(tree,5);
    pre_order(tree);
    printf("\n");
    tree =insert(tree,6);
    pre_order(tree);
    printf("\n");
    tree =insert(tree,7);
    pre_order(tree);
    printf("\n");
    tree =insert(tree,8);
    pre_order(tree);
    printf("\n");
    tree =insert(tree,9);
    pre_order(tree);
    printf("\n");
    tree =insert(tree,10);
    pre_order(tree);
    printf("\n");
    tree =insert(tree,11);
    pre_order(tree);
    printf("\n");
    return 0;
}

第二步建立:avl.c和avl.h文件,建立和main的联系

//
// Created by SishanWang on 3/10/2022.
//
关注公众号:你不知道的东东
与博主联系
#include "avl.h"
#include"malloc.h"
#include "stdlib.h"
#include "stdio.h"
/**
a) Left Left Case
T1, T2, T3 and T4 are subtrees.
         z                                      y
        / \                                   /   \
       y   T4      Right Rotate (z)          x      z
      / \          - - - - - - - - ->      /  \    /  \
     x   T3                               T1  T2  T3  T4
    / \
  T1   T2
b) Left Right Case
     z                               z                           x
    / \                            /   \                        /  \
   y   T4  Left Rotate (y)        x    T4  Right Rotate(z)    y      z
  / \      - - - - - - - - ->    /  \      - - - - - - - ->  / \    / \
T1   x                          y    T3                    T1  T2 T3  T4
    / \                        / \
  T2   T3                    T1   T2
c) Right Right Case
  z                                y
 /  \                            /   \
T1   y     Left Rotate(z)       z      x
    /  \   - - - - - - - ->    / \    / \
   T2   x                     T1  T2 T3  T4
       / \
     T3  T4
d) Right Left Case
   z                            z                            x
  / \                          / \                          /  \
T1   y   Right Rotate (y)    T1   x      Left Rotate(z)   z      y
    / \  - - - - - - - - ->     /  \   - - - - - - - ->  / \    / \
   x   T4                      T2   y                  T1  T2  T3  T4
  / \                              /  \
T2   T3                           T3   T4
*/
AVLTree create_node(AVLElem data){
    AVLTree p;
    p=(AVLTree)malloc(sizeof( AVLNode));                 // void *  空指针为万能指针
    p->data=data;
    p->rchild=NULL;
    p->lchild=NULL;
    p->height=1;
    return p;                                    //不能返回数组名,因为数组的内存不在堆里,函数结束后内存地址被释放
}
static int max(int x,int y){                    //三目运算符: return x>y?x:y;  1、static定义的函数只能在此文件用,保护机制
    return x>y?x:y;                      //static int cnt;      1、作用于于全局变量,不被引用,2,作用于局部变量,被多次引用但只被初始化赋值一次
}
static int get_height(AVLTree root){
    if(root != NULL)                   //空指针没有hight
        return root->height;
    else{
        return 0;
    }
}
static int get_balance(AVLTree root){
    if(root!=NULL){
        return get_height(root->lchild)- get_height(root->rchild);      //左孩子高度-右孩子高度
    }
}
/*T1, T2, T3 and T4 are subtrees.
         z                                      y
        / \                                   /   \
       y   T4      Right Rotate (z)          x      z
      / \          - - - - - - - - ->      /  \    /  \
     x   T3                               T1  T2  T3  T4
    / \
  T1   T2
*/
static AVLTree right_rotation(AVLTree root){
    AVLTree z=root;
    AVLTree y=z->lchild;
    AVLTree T3=y->lchild;
    y->rchild=z;
    z->lchild=T3;
    z->height=max(get_height(z->lchild), get_height(z->rchild))+1;     //孩子的高度加一即为父子树的高度
    y->height=max(get_height(y->lchild), get_height(y->rchild))+1;
    return y;
}
/*
  z                                y
 /  \                            /   \
T1   y     Left Rotate(z)       z      x
    /  \   - - - - - - - ->    / \    / \
   T2   x                     T1  T2 T3  T4
       / \
      T3  T4
*/
static AVLTree left_rotation(AVLTree root){
    AVLTree z=root;
    AVLTree y=z->rchild;
    AVLTree T2=y->lchild;
    y->lchild=z;
    z->rchild=T2;                                                       // 将本来z->rchlid覆盖
    z->height=max(get_height(z->lchild), get_height(z->rchild))+1;     //孩子的高度加一即为父子树的高度
    y->height=max(get_height(y->lchild), get_height(y->rchild))+1;
    return y;
}
AVLTree insert(AVLTree root,AVLElem x) {
    if (root == NULL) {
        return create_node(x);
    }
    if (x < root->data) {
        root->lchild = insert(root->lchild, x);
    } else {
        root->rchild = insert(root->rchild, x);
    }
    root->height = max(get_height(root->lchild), get_height(root->rchild)) + 1;
    int balance = get_balance(root);
    if (balance > 1 && x < root->lchild->data) {
        root = right_rotation(root);
    } else if (balance < -1 && x > root->rchild->data) {                         //左孩子高度-右孩子高度
        root = left_rotation(root);
    } else if (balance > 1 && x > root->lchild->data) {                                  //先左旋在右旋
        root->lchild = left_rotation(root->lchild);
        root = right_rotation(root);
    } else if (balance < -1 && x < root->rchild->data) {                                 //先右旋在左旋
        root->rchild = right_rotation(root->rchild);
        root = left_rotation(root);
    }
    return root;
}
    void pre_order(AVLTree root) {
        if (root != NULL) {
            printf("%d", root->data);
            pre_order(root->lchild);
            pre_order(root->rchild);
        }
    }
//
// Created by SishanWang on 3/10/2022.
//
#ifndef AVL_AVL_H
#define AVL_AVL_H
typedef int AVLElem;
typedef struct avl_node{
    AVLElem data;
    struct avl_node * rchild;
    struct avl_node * lchild;
    int height;
}AVLNode,*AVLTree;
AVLTree create_node(AVLElem data);
AVLTree insert(AVLTree root,AVLElem x);
void pre_order(AVLTree root);
#endif //AVL_AVL_H

第三步:运行

6f40beba67444734ae2a3973436255e3.png


相关文章
|
关系型数据库 Serverless 分布式数据库
【公测】PolarDB PostgreSQL版Serverless功能免费使用​!
【公测】PolarDB PostgreSQL版Serverless功能免费使用​,公测于2024年3月28日开始,持续三个月,公测期间可以免费使用!
|
Java Maven Nacos
Maven - Maven 核心概念一网打尽:轻松掌握项目构建与管理技巧
Maven - Maven 核心概念一网打尽:轻松掌握项目构建与管理技巧
272 0
|
移动开发 前端开发
基于Jeecg-boot的flowable流程支持拒绝同意流程操作
基于Jeecg-boot的flowable流程支持拒绝同意流程操作
382 0
|
11月前
|
机器学习/深度学习 人工智能 自然语言处理
什么是深度学习
【10月更文挑战第23天】什么是深度学习
|
缓存 JavaScript 前端开发
js和html代码一定要分离吗
JavaScript(JS)和HTML代码的分离虽非绝对必要,但通常被推荐
|
Python
告别阻塞,拥抱未来!Python 异步编程 asyncio 库实战指南!
高效处理并发任务对提升程序性能至关重要,Python 的 `asyncio` 库提供了强大的异步编程支持。通过 `async/await` 关键字,可以在等待操作完成时不阻塞程序执行,显著提高效率和响应性。`asyncio` 支持定义异步函数、创建任务、等待多个任务完成等功能,并能结合第三方库如 `aiohttp` 实现异步网络请求。此外,它还支持异常处理,确保异步代码的健壮性。借助 `asyncio`,您可以轻松构建高性能、响应迅速的应用程序。
417 0
|
数据安全/隐私保护
BurpSuite2021 -- Intruder模块
BurpSuite2021 -- Intruder模块
402 2
|
机器学习/深度学习 分布式计算 算法
掌握XGBoost:分布式计算与大规模数据处理
掌握XGBoost:分布式计算与大规模数据处理
608 3