数据结构---二叉平衡排序树的删除

数据结构—二叉平衡排序树的删除

原理:参考趣学数据结构

代码:

#include<stdio.h>
#include<stdlib.h>
typedef struct avlTree {int data;struct avlTree *lchild, *rchild;int height;
}avlTree;int height(avlTree* AVLTree);//声明avl树的深度
void updateHeight(avlTree* &AVLTree);//声明更新avl树每个结点的高度avlTree* LL(avlTree* &AVLTree) {//单向右旋avlTree* temp = AVLTree->lchild;AVLTree->lchild = temp->rchild;temp->rchild = AVLTree;updateHeight(temp);//更新avl树的高度updateHeight(AVLTree);return temp;
}
avlTree* RR(avlTree* &AVLTree) {//单向左旋avlTree* temp = AVLTree->rchild;AVLTree->rchild=temp->lchild;temp->lchild = AVLTree;updateHeight(temp);//更新avl树的高度updateHeight(AVLTree);//调整顺序的两个节点都要更新高度return temp;
}
avlTree* LR(avlTree* &AVLTree) {//先右后左双向旋转AVLTree->lchild=RR(AVLTree->lchild);return LL(AVLTree);
}
avlTree* RL(avlTree* &AVLTree) {//先左后右双向旋转AVLTree->rchild = LL(AVLTree->rchild);return RR(AVLTree);
}
int height(avlTree* AVLTree) {//计算树的高度int m, n;if (!AVLTree) {return 0;}else {m = height(AVLTree->lchild);//左子树深度n = height(AVLTree->rchild);//右子树深度if (m > n) {return m + 1;}else {return n + 1;}}
}
void updateHeight(avlTree* &AVLTree) {//更新每个结点的高度值if (AVLTree) {AVLTree->height = height(AVLTree);updateHeight(AVLTree->lchild);updateHeight(AVLTree->rchild);}
}
avlTree* insertAVLTree(avlTree* &AVLTree,int e) {//插入一个结点if (!AVLTree) {//如果为空树,生成一个新的结点AVLTree = (avlTree*)malloc(sizeof(avlTree));AVLTree->data = e;AVLTree->lchild = AVLTree->rchild = NULL;//不能写成二个都是左指针AVLTree->height = 1;return AVLTree;}else {//查找元素要插入的位置if (AVLTree->data == e) {printf("已经存在元素%d,不需要插入\n", e);return AVLTree;//结束}if (e < AVLTree->data) {AVLTree->lchild = insertAVLTree(AVLTree->lchild, e);//插入左边if (height(AVLTree->lchild) - height(AVLTree->rchild) == 2) {if (e > AVLTree->lchild->data) {AVLTree =LR(AVLTree);//一定要有返回值覆盖}else {AVLTree =LL(AVLTree);}}}else {AVLTree->rchild = insertAVLTree(AVLTree->rchild, e);//插入右边if (height(AVLTree->rchild) - height(AVLTree->lchild) == 2) {if (AVLTree->rchild->data > e) {AVLTree =RL(AVLTree);}else {AVLTree =RR(AVLTree);}}}}updateHeight(AVLTree);//更新AVL每个结点的高度return AVLTree;
}
avlTree * adjustAVLTree(avlTree * &AVLTree) {//调整删除结点后的树if (!AVLTree) {return NULL;}if (height(AVLTree->lchild) - height(AVLTree->rchild) == 2) {if (height(AVLTree->lchild->lchild) >= height(AVLTree->lchild->rchild)) {AVLTree = LL(AVLTree);//单向右旋}else {AVLTree = LR(AVLTree);//先左后右双向旋转}}else if (height(AVLTree->rchild) - height(AVLTree->lchild) == 2) {if (height(AVLTree->rchild->rchild) >= height(AVLTree->rchild->lchild)) {AVLTree = RR(AVLTree);//单向左旋}else {AVLTree = RL(AVLTree);//先右后左双向旋转}}updateHeight(AVLTree);//更新avl树中每个结点的高度return AVLTree;
}
avlTree * deleteElemAVL(avlTree * &AVLTree,int data) {//删除avl的元素if (AVLTree == NULL) {return AVLTree;}if (AVLTree->data == data) {//查找到了该元素if (!AVLTree->rchild) {//如果右子树为空avlTree* temp = AVLTree;AVLTree = AVLTree->lchild;delete temp;}else {//右子树不为空avlTree* p = AVLTree->rchild;while (p->rchild) {p = p->lchild;}AVLTree->data = p->data;//右孩子的右孩子,一直下去AVLTree->rchild = deleteElemAVL(AVLTree->rchild, AVLTree->data);updateHeight(AVLTree);//更新avl树中每个结点的高度}return AVLTree;//这个结束后返回}else if (data < AVLTree->data) {//左子树查找AVLTree->lchild = deleteElemAVL(AVLTree->lchild, data);}else {//右子树查找AVLTree->rchild = deleteElemAVL(AVLTree->rchild, data);}if (AVLTree->lchild) {//先调左右子树,最后调整根节点AVLTree->lchild = adjustAVLTree(AVLTree->lchild);}if (AVLTree->rchild) {AVLTree->lchild = adjustAVLTree(AVLTree->rchild);}if (AVLTree) {AVLTree = adjustAVLTree(AVLTree);}return AVLTree;
}
void prePrint(avlTree*  T) {//前序遍历AVLif (T) {printf("%d ", T->data);prePrint(T->lchild);prePrint(T->rchild);}
}
int main() {avlTree* T = NULL;//一定要初始化为空树int count, data;printf("开始构造avl:\n输入avl结点的数目:");scanf_s("%d", &count);int counts = 0;while (count--) {//构造avlcounts += 1;printf("输入avl的第%d个结点:", counts);scanf_s("%d", &data);insertAVLTree(T, data);}printf("前序遍历avl\n");prePrint(T);//前序遍历avlprintf("\n");printf("删除avl的结点:");scanf_s("%d", &count);deleteElemAVL(T, count);printf("删除avl的结点后的前序遍历\n");prePrint(T);//前序遍历avlprintf("\n");system("pause");return 0;
}

测试截图:

请添加图片描述

时间复杂度:O(logn),空间复杂度O(logn)

如果存在什么问题,欢迎批评指正!谢谢!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/311865.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Magicodes.IE 2.0发布

Magicodes.IE是我们维护的开源的导入导出通用库&#xff0c;去年年底已加入NCC开源组织。Github地址&#xff1a;https://github.com/xin-lai/Magicodes.IEMagicodes.IE不是一蹴而就&#xff0c;而是根据实际需求不断迭代出来的&#xff0c;而且历经多次重构。这一次&#xff0…

python百分号用法_python百分号%—%s、%d、%f

百分号%表示占位符&#xff0c;在后续通过%传入真实的值 %s 拼接字符串&#xff0c;实际可以接受任何类型的值 %d 只能拼接整数数字 %.nf 四舍五入拼接浮点数&#xff0c;n表示保留到小数点后n位&#xff0c;不加.n默认保留6位小数 %% 在有%拼接的的字符串里&#xff0c;如果要…

《ASP.NET Core 微服务实战》-- 读书笔记(第11章)

第 11 章 开发实时应用和服务在本章&#xff0c;我们将讨论“实时”的准确含义&#xff0c;以及在大部分消费者看来应该属于这一范畴的应用类型接着&#xff0c;我们将探讨 WebSocket&#xff0c;并分析为什么传统的 WebSocket 与云环境完全不相适应&#xff0c;最后我们将构建…

word List 14

word List 14 如果存在什么问题&#xff0c;欢迎批评指正&#xff01;谢谢&#xff01;

python编程模式是什么_python 开发的三种运行模式详细介绍

Python 三种运行模式 Python作为一门脚本语言&#xff0c;使用的范围很广。有的同学用来算法开发&#xff0c;有的用来验证逻辑&#xff0c;还有的作为胶水语言&#xff0c;用它来粘合整个系统的流程。不管怎么说&#xff0c;怎么使用python既取决于你自己的业务场景&#xff0…

递归实现排列型枚举

把 1∼n 这 n 个整数排成一行后随机打乱顺序&#xff0c;输出所有可能的次序。 输入格式 一个整数 n。 输出格式 按照从小到大的顺序输出所有方案&#xff0c;每行 1 个。 首先&#xff0c;同一行相邻两个数用一个空格隔开。 其次&#xff0c;对于两个不同的行&#xff0c;…

【在路上5】实时计算助力派件管控

签收率系统试运营以来&#xff0c;每天算出高额罚款&#xff0c;虽然没有真正执行&#xff0c;但也挺吓人的。并且&#xff0c;完完全全的把全网人员积极性提升起来了。总部网络管理中心每天给出各省区前一天的签收率报表&#xff0c;来了个排名和点名&#xff1b;各省区管理中…

数据结构---B-(B)、B+的总结

数据结构—B-&#xff08;B&#xff09;、B的总结 原理&#xff1a;参考趣学数据结构 m阶B-树规则(有序的&#xff08;左子树的元素值<根节点的元素值<右子树的元素值&#xff09;、平衡的&#xff08;每个结点的左右子树的高度差<1&#xff09;、多路的&#xff08…

dfs巩固训练

按顺序从上往下刷即可&#xff01;&#xff01;&#xff01; 知识点: 关于环形的数组&#xff0c;前移动和后移动可能会溢出下标。解决方法是&#xff0c;转移后的坐标公式为 &#xff08;原坐标改变量数组长度&#xff09;%数组长度 易错点: 写搜索时候&#xff0c;有时候会…

python适用范围_Python应用范围总结概览

Python就是万金油&#xff01; Python&#xff08;派森&#xff09;&#xff0c;它是一个简单的、解释型的、交互式的、可移植的、面向对象的超高级语言。这就是对Python语言的最简单的描述。 Python有一个交互式的开发环境&#xff0c;因为Python是解释运行&#xff0c;这大大…

重磅!K8S 1.18版本将内置支持SideCar容器。

作者&#xff1a;justmine头条号&#xff1a;大数据与云原生微信公众号&#xff1a;大数据与云原生创作不易&#xff0c;在满足创作共用版权协议的基础上可以转载&#xff0c;但请以超链接形式注明出处。为了方便阅读&#xff0c;微信公众号已按分类排版&#xff0c;后续的文章…

广义表的学习(原理和代码)

广义表的学习&#xff08;原理和代码&#xff09; 参考链接&#xff1a; https://blog.csdn.net/it_is_me_a/article/details/99870530

bfs巩固训练

按顺序从上往下刷即可&#xff01;&#xff01;&#xff01; 知识点: 关于环形的数组&#xff0c;前移动和后移动可能会溢出下标。解决方法是&#xff0c;转移后的坐标公式为 &#xff08;原坐标改变量数组长度&#xff09;%数组长度 易错点: 写搜索时候&#xff0c;有时候会…

python输出文本 去掉引号_Python可以在文本文件中读取时从字符串中删除双引号吗?...

我有一些这样的文本文件&#xff0c;有50​​00行&#xff1a; 5.6 4.5 6.8 "6.5" (new line) 5.4 8.3 1.2 "9.3" (new line) 所以最后一个术语是双引号之间的数字。 我想做的是使用Python(如果可能的话)将四列分配给双变量。但主要的问题是最后一个术语&a…

word List 15

word List 15 如果存在什么问题&#xff0c;欢迎批评指正&#xff01;谢谢&#xff01;

.NET Core 如何验证信用卡卡号

_点击上方蓝字关注“汪宇杰博客”导语最近在家闲的蛋疼需要写点文章。正好我本人在金融科技公司工作&#xff0c;对信用卡业务略有了解。我们看看如何在 .NET Core 里验证一个信用卡的卡号是否合法。信用卡卡号组成首先&#xff0c;信用卡的卡号一般为16位&#xff0c;也有少许…

python统计文件中每个单词出现的次数_python统计文本中每个单词出现的次数

.python统计文本中每个单词出现的次数&#xff1a; #codingutf-8 __author__ ‘zcg‘ import collections import os with open(‘abc.txt‘) as file1:#打开文本文件 str1file1.read().split(‘ ‘)#将文章按照空格划分开 print "原文本:\n %s"% str1 print "…

AcWing 1101. 献给阿尔吉侬的花束

阿尔吉侬是一只聪明又慵懒的小白鼠&#xff0c;它最擅长的就是走各种各样的迷宫。 今天它要挑战一个非常大的迷宫&#xff0c;研究员们为了鼓励阿尔吉侬尽快到达终点&#xff0c;就在终点放了一块阿尔吉侬最喜欢的奶酪。 现在研究员们想知道&#xff0c;如果阿尔吉侬足够聪明…

数据结构---关键路径

数据结构—关键路径 原理&#xff1a;参考趣学数据结构 代码&#xff1a; #include<stdio.h> #include<stdlib.h> #include "stack.h" #define typeNode int //每个头结点的标识数据类型 #define N 100 //最大结点数 int degree[N];//结点入度数,通过…

Kubernetes AIOps解决方案商 Carbon Relay获6300万美元A轮融资

每10家企业中就有4家报告说他们正在使用Kubernetes&#xff0c;Kubernetes是开放源代码容器编排框架&#xff0c;用于在生产环境中自动化应用程序的部署&#xff0c;扩展和管理。但是&#xff0c;诸如管理复杂性&#xff0c;部署时间和无法预料的错误之类的挑战可能会降低生产率…