AVL旋转图及其代码的实现
来源:互联网 发布:十大网络主播评选结果 编辑:程序博客网 时间:2024/06/16 09:18
1) 左单旋转
未插入前:
插入后:
旋转图:
旋转后:
下面是左单旋代码的实现:
void _Rotatel(Node* parent) //左单旋 { Node* pSubR=parent->_pright; Node* pSubRL=pSubR->_pleft; Node * pPParent=parent->_pParent; parent->_pright=pSubRL; if(pSubRL) pSubRL->_pParent=parent; pSubR->_pleft =parent; parent->_pParent=pSubR; pSubR->_pParent=pPParent; if(NULL==pPParent) { _pRoot=pSubR ; pSubR->_pParent = NULL; } else { if(pPParent->_pleft==parent) pPParent->_pleft=pSubR; else pPParent->_pright=pSubR; //pSubR->_pParent = pPParent; } parent ->_bf=pSubR->_bf==0; //parent = pSubR; }
2) 右单旋转
未插入前:
插入后:
旋转图:
旋转后:
下面是右单旋代码的实现:
void _RotateR(Node* parent)//右单旋 { Node *pSubL = parent->_pleft; Node *pSubLR = pSubL->_pright; Node *ppParent = parent->_pParent; parent->_pleft = pSubLR; if (pSubLR) pSubLR->_pParent = parent; pSubL->_pright = parent; parent->_pParent = pSubL; if (NULL == ppParent) { _pRoot = pSubL; pSubL->_pParent = NULL; } else { if (parent == ppParent->_pleft) ppParent->_pleft = pSubL; else ppParent->_pright = pSubL; pSubL->_pParent = ppParent; } pSubL->_bf = parent->_bf = 0; parent = pSubL; }
3) 先左后右双旋
未插入前:
插入后:
左旋转图:
左旋转后:
右旋转图:
右旋转后:
下面是先左后右双旋代码的实现:
void _RotateLR(Node* pParent) { Node *pSubL = pParent->_pleft; Node *pSubLR = pSubL->_pright; _Rotatel(pParent->_pleft); _RotateR(pParent); if (-1 == pSubLR->_bf) pParent->_bf = 1; else if (1 == pSubLR->_bf) pSubL->_bf = -1; }
4) 先右后左双旋转
未插入前:
插入后:
右旋转图:
右旋转后:
左旋转图:
左旋转后:
下面是先右后左双旋转代码的实现:
void _RotateRL(Node* pParent) { Node *pSubR = pParent->_pright; Node *pSubRL = pSubR->_pleft; _RotateR(pParent ->_pright); _Rotatel(pParent); if (1 == pSubRL->_bf) pParent->_bf = -1; else if (-1 == pSubRL->_bf) pSubR->_bf = 1; }
阅读全文
1 0
- AVL旋转图及其代码的实现
- AVL平衡二叉树中旋转操作的本质及其实现
- AVL树的旋转分析与实现
- AVL树的实现代码
- AVL树及其实现
- AVL树的旋转
- AVL树的旋转
- AVL树的旋转
- AVL树的旋转
- AVL树的旋转
- AVL树的旋转
- AVL树的旋转
- AVL树的旋转
- AVL树的旋转
- AVL树的旋转
- AVL树的旋转
- AVL树的旋转
- AVL树的旋转
- 编程学习知识网站汇总(不断完善中)
- 常用的Mysql数据库操作语句大全
- 深度学习Caffe实战笔记(7)Caffe平台下,如何调整卷积神经网络结构
- c primer plus 阅读共享 -1
- java语言基础(57)——Arrays工具类的概述和使用
- AVL旋转图及其代码的实现
- Palindrome Partitioning
- Python爬虫股票评论,snowNLP简单分析股民用户情绪(草稿)
- 环境变量
- 【Tensorflow】报错:Cannot interpret feed_dict key as Tensor: The name 'x' refers to an operation, # > no
- Java多线程-4 Lock Objects to Speed up multi-thread code
- 洛谷P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀--dp
- CSRF
- 数据库磁盘