当前位置: 首页 > news >正文

平衡二叉树c语言版

一、定义二叉树结点结构体
/*** 定义平衡二叉树结点
*/
struct avlbinarytree
{    //数据域NodeData*  data;///树高int  h;struct avlbinarytree*  left;struct avlbinarytree*  right;
};
typedef struct avlbinarytree AVLNode;
二、声明函数的操作
/*** 创建结点
*/
AVLNode* create_avlbinarytree_node(NodeData* data);/**** 计算树高度
*/
int   get_avltree_h(AVLNode* childRoot);/*** 添加结点
*/
void  insert_avlbinarytree_node(AVLNode** tree,NodeData*  data);/*** 平衡操作
*/
void  do_avltree_roate(AVLNode** root);/*** 前序遍历
*/
void  per_order_avlbinarytree(AVLNode* root);/*** LL型,左孩子的左子树添加删除引起的失衡*         5                                       3*       .   .           平衡操作                .     .*      3     6      -------------->            2     5 *     .  .                                    .    .    .*    2    4                                  1     4     6*   .*  1* * 平衡操作:左子树整体右旋,将根节点左孩子提到根节点,原根结点变成新根节点的右孩子,新根结点的原右孩子变成原根节点的左孩子*
*/
void  ll_roate_avl(AVLNode** root);/*** RR型右孩子的右子树添加删除引起的失衡*          2                               4*       .     .                         .     .*       1     4      ------->           2     6  *          .    .                     .   .  .    *          3    6                     1   3  5*             .  *             5*
*/void  rr_roate_avl(AVLNode** root);
/*** LR型,左孩子的右子树添加删除引起的失衡*         5                    5                           3*       .   .               .     .                     .     .*      2     6              3     6                     2     5 *     .  .       ------> .   .          ------->      .     .    .*         .            .*          4          1*平衡操作:左子树先做一次RR左旋,再做一次LL右旋
*/
void  lr_roate_avl(AVLNode** root);/*** RL型右孩子的左子树添加删除引起的失衡*          2                        2                                4*       .     .                   .    .                           .     .*       1     5    ------->      1     4       ---->               2       5*          .    .                    .    .                      .    .      .*          4    6                   3      5                    1      3      6*         .                                  .*        3                                     6*        *平衡操作: 先将右子树做一次LL右旋,在做一次RR左旋
*/
void  rl_roate_avl(AVLNode** root);
三、平衡二叉树操作函数定义

/*** 创建结点*/
AVLNode *create_avlbinarytree_node(NodeData *data)
{AVLNode *node = malloc(sizeof(AVLNode *));if (node == NULL){perror("创建AVLNode结点失败");return NULL;}node->data = malloc(sizeof(NodeData *));if (node->data == NULL){perror("AVLNode数据域分配内存空间失败");return NULL;}*(node->data) = *data;node->h = 1;node->left = NULL;node->right = NULL;return node;
}
/*** 获取树高*/
int get_avltree_h(AVLNode *childRoot)
{if (childRoot == NULL){return 0;}int l = get_avltree_h(childRoot->left) + 1;int r = get_avltree_h(childRoot->right) + 1;return l > r ? l : r;
}void insert_avlbinarytree_node(AVLNode **tree, NodeData *data)
{if (*tree == NULL){AVLNode *newNode = create_avlbinarytree_node(data);*tree = newNode;return;}/// 左子树if (*data < *((*tree)->data)){insert_avlbinarytree_node(&((*tree)->left), data);}//右子树if (*data > *((*tree)->data)){insert_avlbinarytree_node(&((*tree)->right), data);}
}void  do_avltree_roate(AVLNode** root)
{int lh = get_avltree_h((*root)->left);int rh = get_avltree_h((*root)->right);/// 左子树高于右子树造成的不平衡if(lh-rh>1){int llh =  get_avltree_h((*root)->left->left);int lrh =  get_avltree_h((*root)->left->right);bool isLL = llh > lrh ;if(isLL)ll_roate_avl(root);elselr_roate_avl(root);}/// 右子树高于左子树造成的不平衡if(rh-lh>1){int rlh =  get_avltree_h((*root)->right->left);int rrh =  get_avltree_h((*root)->right->right);bool isRR = rrh > rlh ;if(isRR)rr_roate_avl(root);elserl_roate_avl(root);      }
}void per_order_avlbinarytree(AVLNode *root)
{if (root == NULL){return;}printf("d-avlbinarytree:  %d\n", *(root->data));per_order_avlbinarytree(root->left);per_order_avlbinarytree(root->right);
}void ll_roate_avl(AVLNode **root)
{printf("lr_roate_avl----LL型\n");//  (*root)->left = temp->right;//  temp->right = (*root);//  *root = temp; AVLNode *temp =(*root)->left->right;(*root)->left->right = *root;*root =(*root)->left;(*root)->right->left= temp;
}void rr_roate_avl(AVLNode **root)
{printf("lr_roate_avl----RR型\n");AVLNode *temp = (*root)->right->left;(*root)->right->left = *root;*root = (*root)->right;(*root)->left->right = temp;
}void lr_roate_avl(AVLNode **root)
{printf("lr_roate_avl----LR型\n");AVLNode *leftTree = (*root)->left;rr_roate_avl(&leftTree);(*root)->left = leftTree;ll_roate_avl(root);
}void rl_roate_avl(AVLNode **root)
{printf("lr_roate_avl----RL型\n");AVLNode *rightRoot = (*root)->right;ll_roate_avl(&rightRoot);(*root)->right = rightRoot;rr_roate_avl(root);
}
四、平衡二叉树测试
void   test_avlbinarytree()
{//RR型  {1,2,3,4,5,6};//LL型  {7,6,5,4,3,2,1};//LR型  {5,2,6,1,3,4};//RL型  {4,3,8,6,5,10};NodeData  arr[] = {4,3,8,6,5,10};int len = sizeof(arr)/sizeof(arr[0]);int i;AVLNode* root = NULL;for(i=0;i<len;i++){insert_avlbinarytree_node(&root,&arr[i]);do_avltree_roate(&root);}printf("start 中序遍历---\n");per_order_avlbinarytree(root);int lh = get_avltree_h(root->left);int rh = get_avltree_h(root->right);printf("树高度lh= %d, rh= %d\n",lh,rh);}

http://www.lryc.cn/news/237356.html

相关文章:

  • 初始环境配置
  • 记GitLab服务器迁移后SSH访问无法生效的问题解决过程
  • 【NGINX--2】高性能负载均衡
  • Android studio run 手机或者模拟器安装失败,但是生成了debug.apk
  • 【面试经典150 | 数学】加一
  • Rust unix domain socket
  • 初识分布式键值对存储etcd
  • docker swarm集群部署
  • MySQL进阶_9.事务基础知识
  • IDEA调用接口超时,但Postman可成功调用接口
  • TableUtilCache:针对CSV表格进行的缓存
  • java源码-工程讲解
  • K8S基础笔记
  • 十一、统一网关GateWay(搭建网关、过滤器、跨越解决)
  • C语言--每日五道选择题--Day20
  • Fourier分析导论——第6章——R^d 上的Fourier变换(E.M. Stein R. Shakarchi)
  • 音视频技术在手机上的应用与挑战
  • 三十分钟学会SCALA
  • leetcode做题笔记242. 有效的字母异位词
  • 沸点 | Ultipa 图数据库金融应用场景优秀案例首批入选,金融街论坛年会发布
  • GaussDB SQL基础语法示例-GOTO语句
  • ClickHouse 物化视图
  • 原理Redis-ZipList
  • 小迪安全笔记——Web架构篇语言中间件数据库系统源码获取
  • Linux从 全栈开发 centOS 7 到 运维
  • Harmony Ble 蓝牙App (一)扫描
  • 录制第一个jmeter性能测试脚本2(http协议)——webtour
  • 时间序列与 Statsmodels:预测所需的基本概念(1)
  • 计算机网络(持续更新…)
  • BetterDisplay Pro for Mac(显示器校准软件)