数据结构二叉树算法c语言实现,数据结构与算法 :AVL平衡二叉树C语言实现
C语言实现AVL平衡二叉树
1结构体//这里定义一个取较大值的MAX宏
#define MAX(a,b) ((a)>(b)?(a):(b))
typedef struct AVLTreeNode {
int data; //数据域
int height; //数据域
struct AVLTreeNode *left; //左子结点
struct AVLTreeNode *right; //右子结点
} AVLTreeNode;
2.1右单旋/**
* 右单旋
* 8 4
* 4 12 2 8
* 2 6 => 1 6 12
* 1
*/
AVLTreeNode *right_rotation(AVLTreeNode *root)
{
struct AVLTreeNode *new_root;
new_root = root->left;
root->left = new_root->right;
new_root->right = root;
//旋转完重新计算高度
root->height = MAX(Height(root->left) + 1, Height(root->right));
new_root->height = MAX(Height(new_root->left) + 1, Height(new_root->right));
return new_root;
}
2.2左单旋/**
* 左单旋
* 8 12
* 4 12 8 50
* 9 50 => 4 9 70
* 70
*/
AVLTreeNode *left_rotation(AVLTreeNode *root)
{
struct AVLTreeNode *new_root;
new_root = root->right;
root->right = new_root->left;
new_root->left = root;
//旋转完重新计算高度
root->height = MAX(Height(root->right) + 1, Height(root->left));
new_root->height = MAX(Height(new_root->right) + 1, Height(new_root->left));
return new_root;
}
2.3左右旋/**
* 左右旋 先对root->left左旋 再对root右旋
* 8 8 6
* 4 12 6 12 4 8
* 2 6 => 4 => 2 5 12
* 5 2 5
*/
AVLTreeNode *left_right_rotation(AVLTreeNode *root){
root->left = left_rotation(root->left);
return right_rotation(root);
}
2.3右左旋/**
* 右左旋 先对root->right右旋 再对root左旋
* 8 8 10
* 4 12 4 10 8 12
* 10 14 => 9 12 => 4 9 14
* 9 14
*/
AVLTreeNode *right_left_rotation(AVLTreeNode *root){
root->right = right_rotation(root->right);
return left_rotation(root);
}
3 自平衡AVLTreeNode *rebalance(AVLTreeNode *root, int data)
{
if (Height(root->right) - Height(root->left) == 2)
{
if (data > root->right->data)//左单旋的情况
{
printf("left_rotation \n");
root = left_rotation(root);
}else{
printf("right_left_rotation \n");//右左旋的情况
root = right_left_rotation(root);
}
}
if (Height(root->left) - Height(root->right) == 2)
{
if (data < root->left->data)//右单旋的情况
{
printf("right_rotation \n");
root = right_rotation(root);
}else{
printf("left_right_rotation \n");//左右旋的情况
root = left_right_rotation(root);
}
}
return root;
}
4 插入AVLTreeNode *insert(AVLTreeNode *root, int data)
{
if (NULL == root)
{
struct AVLTreeNode *node;
node = (struct AVLTreeNode *)malloc(sizeof(struct AVLTreeNode));
if (node == NULL)
{
printf("malloc error \n");
return NULL;
}
node->data = data;
node->right = NULL;
node->left = NULL;
return node;
}
if (data >= root->data)
{
root->right = insert(root->right, data);
root->height = MAX(Height(root->left), Height(root->right) + 1);//相比于二叉搜索树多了高度判断
}else{
root->left = insert(root->left, data);
root->height = MAX(Height(root->left) + 1, Height(root->right));
}
root = rebalance(root, data);
return root;
}
5.1 查找最大/小节点AVLTreeNode *FindMin(AVLTreeNode *root)
{
if (NULL == root)
{
return NULL;
}
if (root->left == NULL)
{
return root;
}else{
return FindMin(root->left);
}
}
AVLTreeNode *FindMax(AVLTreeNode *root)
{
if (NULL == root)
{
return NULL;
}
if (root->right == NULL)
{
return root;
}else{
return FindMax(root->right);
}
}
5.2删除AVLTreeNode *Delete(AVLTreeNode *root, int target)
{
if (NULL == root)
{
return NULL;
}
if (target > root->data)
{
root->right = Delete(root->right, target);
if (Height(root->left) - Height(root->right) == 2)
{
AVLTreeNode *l = root->left;
if (Height(l->right) > Height(l->left))
root = left_right_rotation(root);
else
root = left_rotation(root);
}
}else if(target < root->data){
root->left = Delete(root->left, target);
if (Height(root->right) - Height(root->left) == 2)
{
AVLTreeNode *r = root->right;
if (Height(r->left) > Height(r->right))
root = left_right_rotation(root);
else
root = right_rotation(root);
}
}else{
//相等的情况
if (root->left && root->right)
{
if (Height(root->left) > Height(root->right))
{
//左子树比右子树高
AVLTreeNode *max = FindMax(root->left);
root->data = max->data;
root->left = Delete(root->left, max->data);
}else{
AVLTreeNode *min = FindMin(root->right);
root->data = min->data;
root->right = Delete(root->right, min->data);
}
}else{
struct AVLTreeNode *tmp;
tmp = root;
if (root->left == NULL)
{
root = root->right;
}else if(root->right == NULL){
root = root->left;
}else{
root = NULL;//删除自身
}
}
}
return root;
}
测试:int main()
{
struct AVLTreeNode *node;
node = NULL;
node = insert(node, 8);
node = insert(node, 4);
node = insert(node, 12);
node = insert(node, 10);
node = insert(node, 14);
node = insert(node, 9);
printf("***前序***: \n");
preOrderTraverse(node);
node = Delete(node, 10);
printf("***前序***: \n");
preOrderTraverse(node);
}
数据结构二叉树算法c语言实现,数据结构与算法 :AVL平衡二叉树C语言实现相关推荐
- 13:Scala语言的数据结构和算法
第十九章 Scala语言的数据结构和算法 19.1 数据结构(算法)的介绍 数据结构的介绍 1.数据结构是一门研究算法的学科,只从有了编程语言也就有了数据结构.学好数据结构可以编写出更加漂亮.更加 ...
- 大数据技术之_16_Scala学习_13_Scala语言的数据结构和算法_Scala学习之旅收官之作
大数据技术之_16_Scala学习_13 第十九章 Scala语言的数据结构和算法 19.1 数据结构(算法)的介绍 19.2 看几个实际编程中遇到的问题 19.2.1 一个五子棋程序 19.2.2 ...
- 数据结构 - 二叉树 - 面试中常见的二叉树算法题
数据结构 - 二叉树 - 面试中常见的二叉树算法题 数据结构是面试中必定考查的知识点,面试者需要掌握几种经典的数据结构:线性表(数组.链表).栈与队列.树(二叉树.二叉查找树.平衡二叉树.红黑树).图 ...
- c语言通用数据结构和常用算法库,基于C语言的通用数据结构和算法库
本人最近在学习数据结构的课程,在过程中发现用C语言来实现各种数据结构类型的时候很难做到真正意义上的通用的数据结构,于是在网上搜罗了一些所谓的C语言通用数据结构库,在此也将这些数据结构库一一罗列,方便大 ...
- 数据结构-二叉树入门Go语言实现
数据结构-二叉树入门Go语言实现 之前我们一直在谈的是一对一的线性结构,可现实中,还有很多一对多的情况需要处理,所以我们需要研究这种一对多的数据结构--"树",考虑它的各种特性,来 ...
- c语言紧凑算法的内存分配,C语言(第二章.1)算法与数据结构A.ppt
C语言课件(第二章.1)算法与数据结构A资料 课程设置目的 本课程是非计算机专业学生的技术基础课,通过本课程的学习,应使学生掌握传统的结构化程序设计的一般方法,以C为语言基础,培养学生严谨的程序设计思 ...
- c语言算法集,【二级C语言】数据结构算法集---C 语言实现
蜡烛马区新固动工八拐怪状小城,小城炮兵会晒茶麸领子.冒算心头画行启脾沙拉,壮志骗人淡漠流生宣布木锯南政新车?猛料妙绝长亭浓味寺塔马兜放领理光.普特民师初生情境民寿流传灭绝!勤恳心曲修整拭除转子苛评. ...
- 算法java语言描述_java语言描述数据结构与算法崔笑颜的博客
java语言描述数据结构与算法崔笑颜的博客 冒泡排序 插入排序 选择排序 希尔排序 快速排序 归并排序 二分查找package com.demo.test; import java.util.Arra ...
- C语言基本数据结构之三(图的广度及深度遍历,求单源最短路径的Dijkstra算法)
上一篇主要讲了二叉树的先序,中序,后序遍历算法以及深度和节点的算法,这篇就讲一讲图的基本算法. 一.图的基本概念 1.1有向图G1: 有向图G是由两个集合V(G)和E(G)组成的,其中:V(G)是顶点 ...
- 【经典回放】多种语言系列数据结构算法:栈(JavaScript版)
本文采用JavaScript语言实现栈结构算法并举例应用. JavaScript的栈是数组对象中自动提供的,这点如同C#,实际也不需要自己编写什么栈的程序,如果你用: var s=new Array( ...
最新文章
- Android被逼学习小例子1
- 先出报表还是先计提所得税_大征期来啦!5分钟熟练掌握企业所得税申报
- RHEL 5基础篇—了解系统的引导过程
- Rxjava+Retrofit的使用实例(基于retrofit2.1.0)
- HTML5 CSS3初学者指南(3) – HTML5新特性
- 应用交付老兵眼中的Envoy, 云原生时代下的思考
- LeetCode LCS 01. 下载插件
- Java Calendar getDisplayName()方法与示例
- Git仓库只拷贝代码-不拷贝提交记录-不拷贝其他分支
- CodeMirror动态加载和表单提交
- 2022年计算机软件水平考试网络工程师(中级)练习题及答案
- 腾讯云CDN加速COS图床
- 通达信自带指标 均线多头排列(DTPL)
- 微信支付跨平台软件架构
- 通过编写游戏程序的视角去编写字符驱动--设备树-百问imx6ull-pro
- 牛客小白月赛58 B(暴力)C(思维)D(dp滚动数组优化)
- 【TensorFlow报错】AttributeError: module 'tensorflow' has no attribute 'merge_all_summaries'
- Chapter 5 (Eigenvalues and Eigenvectors): The characteristic equation (特征方程)
- 「小白学Python」Windows安装Python
- RxSwift之路01-----简单的RxSwift使用