AVLTree(C++实现)没有统一旋转操作
AVLTree源码实现
/*
* BinarySearchTree.h
* 1. 添加元素时需自己做判断元素是否合法
* 2. 除层序遍历外,本源代码均采用递归遍历,若要减少栈的消耗,应该实现迭代遍历
* 3. 本代码实现的AVL树没有统一旋转操作,采用分情况讨论LL,LR,RR,RL来进行树的平衡
* Created on: 2020年1月29日
* Author: LuYonglei
*/ #ifndef SRC_BINARYSEARCHTREE_H_
#define SRC_BINARYSEARCHTREE_H_
#include <queue> template<typename Element>
class BinarySearchTree {
public: BinarySearchTree(int (*cmp)(Element e1, Element e2)); //比较函数指针 virtual ~BinarySearchTree(); int size(); //元素的数量 bool isEmpty(); //是否为空 void clear() {
//清空所有元素
NODE *node = root_;
root_ = nullptr;
using namespace std;
queue<NODE*> q;
q.push(node);
while (!q.empty()) {
NODE *tmp = q.front();
if (tmp->left != nullptr)
q.push(tmp->left);
if (tmp->right != nullptr)
q.push(tmp->right);
delete tmp;
q.pop();
}
} void add(Element e) {
//添加元素
add(e, cmp_);
} void remove(Element e) {
//删除元素
remove(Node(e, cmp_));
} bool contains(Element e) {
//是否包含某元素
return Node(e, cmp_) != nullptr;
} void preorderTraversal(bool (*visitor)(Element e)) {
//前序遍历
if (visitor == nullptr)
return;
bool stop = false; //停止标志,若stop为true,则停止遍历
preorderTraversal(root_, stop, visitor);
} void inorderTraversal(bool (*visitor)(Element e)) {
//中序遍历
if (visitor == nullptr)
return;
bool stop = false; //停止标志,若stop为true,则停止遍历
inorderTraversal(root_, stop, visitor);
} void postorderTraversal(bool (*visitor)(Element e)) {
//后序遍历
if (visitor == nullptr)
return;
bool stop = false; //停止标志,若stop为true,则停止遍历
postorderTraversal(root_, stop, visitor);
} void levelOrderTraversal(bool (*visitor)(Element e)) {
//层序遍历,迭代实现
if (visitor == nullptr)
return;
levelOrderTraversal(root_, visitor);
} int height() {
//树的高度
return height(root_);
} bool isComplete() {
//判断是否是完全二叉树
return isComplete(root_);
} private: int size_; typedef struct _Node {
Element e;
_Node *parent;
_Node *left;
_Node *right;
int height; //节点的高度
_Node(Element e_, _Node *parent_) :
e(e_), parent(parent_), left(nullptr), right(nullptr), height() {
//节点构造函数
} inline bool isLeaf() {
return (left == nullptr && right == nullptr);
} inline bool hasTwoChildren() {
return (left != nullptr && right != nullptr);
} inline int balanceFactor() {
//获得节点的平衡因子
int leftHeight = left == nullptr ? : left->height; //获得左子树的高度
int rightHeight = right == nullptr ? : right->height; //获得右子树的高度
return leftHeight - rightHeight;
} inline bool isBalanced() {
//判断node是否平衡
int balanceFactor_ = balanceFactor();
return balanceFactor_ >= - && balanceFactor_ <= ; //平衡因子为-1,0,1则返回true
} inline void updateHeight() {
//更新节点的高度
int leftHeight = left == nullptr ? : left->height; //获得左子树的高度
int rightHeight = right == nullptr ? : right->height; //获得右子树的高度
height = + (leftHeight > rightHeight ? leftHeight : rightHeight); //把节点高度更新为左右子树最大的高度+1
} inline bool isLeftChild() {
//判断节点是否是父亲节点的左子结点
return parent != nullptr && parent->left == this;
} inline bool isRightChild() {
//判断节点是否是父亲节点的右子结点
return parent != nullptr && parent->right == this;
} inline _Node* tallerChild() {
//获得高度更高的子树
int leftHeight = left == nullptr ? : left->height; //获得左子树的高度
int rightHeight = right == nullptr ? : right->height; //获得右子树的高度
if (leftHeight > rightHeight)
return left;
if (leftHeight < rightHeight)
return right;
return isLeftChild() ? left : right;
} } NODE; NODE *root_; int (*cmp_)(Element e1, Element e2); //为实现树的排序的个性化配置,私有成员保存一个比较函数指针 NODE* Node(Element e, int (*cmp_)(Element e1, Element e2)) {
//返回e元素所在的节点
NODE *node = root_;
while (node != nullptr) {
int cmp = cmp_(e, node->e);
if (cmp == ) //找到了元素
return node;
if (cmp > ) { //待寻找元素大于节点存储的元素
node = node->right;
} else { //待寻找元素小于节点存储的元素
node = node->left;
}
}
return nullptr;
} NODE* predecessor(NODE *node) {
//返回node的前驱节点
if (node == nullptr)
return nullptr;
//前驱节点在左子树
NODE *tmp = node->left;
if (tmp != nullptr) {
while (tmp->right != nullptr)
tmp = tmp->right;
return tmp;
}
//从父节点,祖父节点中寻找前驱节点
while (node->parent != nullptr && node == node->parent->left) {
node = node->parent;
}
return node->parent;
} NODE* successor(NODE *node) {
//返回node的后继节点
if (node == nullptr)
return nullptr;
//后继节点在右子树
NODE *tmp = node->right;
if (tmp != nullptr) {
while (tmp->left != nullptr)
tmp = tmp->left;
return tmp;
}
//从父节点,祖父节点中寻找后继节点
while (node->parent != nullptr && node == node->parent->right) {
node = node->parent;
}
return node->parent;
} void afterRotate(NODE *gNode, NODE *pNode, NODE *child) {
//在左旋转与右旋转中统一调用
pNode->parent = gNode->parent;
if (gNode->isLeftChild())
gNode->parent->left = pNode;
else if (gNode->isRightChild())
gNode->parent->right = pNode;
else
//此时gNode->parent 为nullptr,gNode为root节点
root_ = pNode;
if (child != nullptr)
child->parent = gNode;
gNode->parent = pNode;
//左右子树发生变化,所以要更新高度
gNode->updateHeight();
pNode->updateHeight();
} void rotateLeft(NODE *gNode) {
//对gNode进行左旋转
NODE *pNode = gNode->right;
NODE *child = pNode->left;
gNode->right = child;
pNode->left = gNode;
afterRotate(gNode, pNode, child); } void rotateRight(NODE *gNode) {
//对gNode进行右旋转
NODE *pNode = gNode->left;
NODE *child = pNode->right;
gNode->left = child;
pNode->right = gNode;
afterRotate(gNode, pNode, child); } void rebalance(NODE *gNode) {
//恢复平衡,grand为高度最低的不平衡节点
NODE *pNode = gNode->tallerChild();
NODE *nNode = pNode->tallerChild();
if (pNode->isLeftChild()) {
if (nNode->isLeftChild()) {
//LL
/*
* gNode
* / 对gNode右旋
* pNode ====> pNode
* / / \
* nNode nNode gNode
*/
rotateRight(gNode);
} else {
//LR
/*
* gNode gNode
* / 对pNode左旋 / 对gNode右旋
* pNode ====> nNode ====> nNode
* \ / / \
* nNode pNode pNode gNode
*/
rotateLeft(pNode);
rotateRight(gNode);
}
} else {
if (nNode->isLeftChild()) {
//RL
/*
* gNode gNode
* \ 对pNode右旋 \ 对gNode左旋
* pNode ====> nNode ====> nNode
* / \ / \
* nNode pNode gNode pNode
*/
rotateRight(pNode);
rotateLeft(gNode);
} else {
//RR
/*
* gNode
* \ 对gNode左旋
* pNode ====> pNode
* \ / \
* nNode gNode nNode
*/
rotateLeft(gNode);
}
}
} void afterAdd(NODE *node) {
//添加node之后的调整
if (node == nullptr)
return;
node = node->parent;
while (node != nullptr) {
if (node->isBalanced()) {
//如果节点平衡,则对其更新高度
node->updateHeight();
} else {
//此时对第一个不平衡节点操作,使其平衡
rebalance(node);
//整棵树恢复平衡后,跳出循环
break;
}
node = node->parent;
}
} void add(Element e, int (*cmp_)(Element e1, Element e2)) {
//当树为空时,添加的节点作为树的根节点
if (root_ == nullptr) {
root_ = new NODE(e, nullptr);
size_++;
//插入一个根节点之后进行调整
afterAdd(root_);
return;
}
//当添加的节点不是第一个节点
NODE *parent = root_;
NODE *node = root_;
int cmp = ; //比较结果
while (node != nullptr) {
parent = node; //保存父节点
cmp = cmp_(e, node->e); //由函数指针来比较
if (cmp > ) {
node = node->right; //添加的元素大于节点中的元素
} else if (cmp < ) {
node = node->left; //添加的元素小于节点中的元素
} else {
node->e = e; //相等时就覆盖
return; //添加的元素等于节点中的元素,直接返回
}
}
//判断要插入父节点的哪个位置
NODE *newNode = new NODE(e, parent); //为新元素创建节点
if (cmp > ) {
parent->right = newNode; //添加的元素大于节点中的元素
} else {
parent->left = newNode; //添加的元素小于节点中的元素
}
size_++;
//添加一个新节点之后进行调整
afterAdd(newNode);
} void afterRemove(NODE *node) {
//删除节点之后的调整,node是要被删除的节点,或是替代的节点
if (node == nullptr)
return;
node = node->parent;
while (node != nullptr) {
if (node->isBalanced()) {
//如果节点平衡,则对其更新高度
node->updateHeight();
} else {
//此时对不平衡节点操作,使其平衡
rebalance(node);
}
node = node->parent;
}
} void remove(NODE *node_) {
//删除某一节点
if (node_ == nullptr)
return;
size_--;
//优先删除度为2的节点
if (node_->hasTwoChildren()) {
NODE *pre = successor(node_); //找到node_的后继节点
node_->e = pre->e; //用后继节点的值覆盖度为2的节点的值
//删除后继节点(后继节点的度只能为1或0)
node_ = pre;
}
//此时node_的度必然为0或1
NODE *replacement = node_->left != nullptr ? node_->left : node_->right;
if (replacement != nullptr) { //node_的度为1
replacement->parent = node_->parent;
if (node_->parent == nullptr) //度为1的根节点
root_ = replacement;
else if (node_->parent->left == node_)
node_->parent->left = replacement;
else
node_->parent->right = replacement;
//所有删除操作准备完成,准备释放节点内存前进行平衡操作
afterRemove(replacement);
delete node_;
} else if (node_->parent == nullptr) { //node_是叶子节点,也是根节点
root_ = nullptr;
delete node_;
} else { //node_是叶子节点,但不是根节点
if (node_->parent->left == node_)
node_->parent->left = nullptr;
else
node_->parent->right = nullptr;
//所有删除操作准备完成,准备释放节点内存前进行平衡操作
afterRemove(node_);
delete node_;
}
} void preorderTraversal(NODE *node, bool &stop, bool (*visitor)(Element e)) {
//递归实现前序遍历
if (node == nullptr || stop == true)
return;
stop = visitor(node->e);
preorderTraversal(node->left, stop, visitor);
preorderTraversal(node->right, stop, visitor);
} void inorderTraversal(NODE *node, bool &stop, bool (*visitor)(Element e)) {
//递归实现中序遍历
if (node == nullptr || stop == true)
return;
inorderTraversal(node->left, stop, visitor);
if (stop == true)
return;
stop = visitor(node->e);
inorderTraversal(node->right, stop, visitor);
} void postorderTraversal(NODE *node, bool &stop,
bool (*visitor)(Element e)) {
//递归实现后序遍历
if (node == nullptr || stop == true)
return;
postorderTraversal(node->left, stop, visitor);
postorderTraversal(node->right, stop, visitor);
if (stop == true)
return;
stop = visitor(node->e);
} void levelOrderTraversal(NODE *node, bool (*visitor)(Element e)) {
if (node == nullptr)
return;
using namespace std;
queue<NODE*> q;
q.push(node);
while (!q.empty()) {
NODE *node = q.front();
if (visitor(node->e) == true)
return;
if (node->left != nullptr)
q.push(node->left);
if (node->right != nullptr)
q.push(node->right);
q.pop();
}
} int height(NODE *node) {
//某一节点的高度
return node->height;
} bool isComplete(NODE *node) {
if (node == nullptr)
return false;
using namespace std;
queue<NODE*> q;
q.push(node);
bool leaf = false; //判断接下来的节点是否为叶子节点
while (!q.empty()) {
NODE *node = q.front();
if (leaf && !node->isLeaf()) //判断叶子节点
return false;
if (node->left != nullptr) {
q.push(node->left);
} else if (node->right != nullptr) { //node->left == nullptr && node->right != nullptr
return false;
}
if (node->right != nullptr) {
q.push(node->right);
} else { //node->right==nullptr
leaf = true;
}
q.pop();
}
return true;
} }; template<typename Element>
BinarySearchTree<Element>::BinarySearchTree(int (*cmp)(Element e1, Element e2)) :
size_(), root_(nullptr), cmp_(cmp) {
//树的构造函数
} template<typename Element>
BinarySearchTree<Element>::~BinarySearchTree() {
// 析构函数
clear();
} template<typename Element>
inline int BinarySearchTree<Element>::size() {
//返回元素个数
return size_;
} template<typename Element>
inline bool BinarySearchTree<Element>::isEmpty() {
//判断是否为空树
return size_ == ;
} #endif /* SRC_BINARYSEARCHTREE_H_ */
main测试例程
/*
* main.cpp
*
* Created on: 2020年1月29日
* Author: LuYonglei
*/ #include "BinarySearchTree.h"
#include <iostream>
#include <time.h> using namespace std; template<typename Element>
int compare(Element e1, Element e2) {
//比较函数,相同返回0,e1<e2返回-1,e1>e2返回1
return e1 == e2 ? : (e1 < e2 ? - : );
} template<typename Elemnet>
bool visitor(Elemnet e) {
cout << e << " ";
// cout << endl;
return false; //若返回true,则在遍历时会退出
} int main(int argc, char **argv) {
BinarySearchTree<double> a(compare);
a.add();
a.add();
a.add();
a.add();
a.add();
// a.add(99);
// a.add(95);
// a.add(2);
// a.add(1);
// a.add(70);
// a.add(44);
// a.add(58);
// a.add(11);
// a.add(21);
// a.add(14);
// a.add(93);
// a.add(57);
// a.add(4);
// a.add(56);
// a.remove(99);
// a.remove(85);
// a.remove(95);
// clock_t start = clock();
// for (int i = 0; i < 1000000; i++) {
// a.add(i);
// }
// for (int i = 0; i < 1000000; i++) {
// a.remove(i);
// }
// a.inorderTraversal(visitor);
// clock_t end = clock();
// cout << end - start << endl;
// cout <<a.height()<< endl;
// cout << a.isComplete() << endl;
// a.remove(7);
// a.clear();
a.levelOrderTraversal(visitor);
cout << endl;
a.preorderTraversal(visitor);
cout << endl;
a.inorderTraversal(visitor);
cout << endl;
a.postorderTraversal(visitor);
cout << endl;
// cout << endl;
// cout<<a.contains(0)<<endl;
}
AVLTree(C++实现)没有统一旋转操作的更多相关文章
- AVLtree(C++实现)有统一的旋转操作
在学习完AVLtree之后,我发现,左旋,右旋均可以采用统一的旋转方式来实现,所以把代码贴在下面 代码是完整的AVLTree实现 C++标准为C++11 在ubuntu 18.04下通过编译和调试 / ...
- AVLTree的实现以及左右旋转维持自平衡
AVL(Adelson-Velskii and Landis)树是带有平衡条件的二叉查找树.这个平衡条件必须要容易保持,而且它保证树的深度须是o(logN).最简单的想法是要求左右子树具有相同的高度, ...
- 【微软100题】定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部。 如把字符串abcdef左旋转2位得到字符串cdefab。请实现字符串左旋转的函数。
package test; /** * 定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部. 如把字符串abcdef左旋转2位得到字符串cdefab. 请实现字符串左旋转的函数. * ...
- Splay_Tree 模板(区间修改,旋转操作)
1.旋转操作 #define MAXN 100100 bool Add[MAXN];//延迟标记 struct Splay_Tree { int cnt, rt;//cnt为节点数,rt == roo ...
- splay tree旋转操作 hdu 1890
很神奇的旋转操作. 目前没看到其他数据结构能实现这个功能.平衡树不好处理区间操作,线段树很难旋转.splay tree搞这个就很简单了. 下面用的这个模板跑了700ms,好慢,估计是删除操作太费时了, ...
- hdu 1890 Robotic SortI(splay区间旋转操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题解:splay又一高级的功能,区间旋转这个是用线段树这些实现不了的,这题可以学习splay的旋 ...
- media静态文件统一管理 操作内存的流 - StringIO | BytesIO PIL:python图片操作库 前端解析二进制流图片(了解) Admin自动化数据管理界面
一.media ''' 1. 将用户上传的所有静态文件统一管理 -- settings.py -- MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 2. 服务 ...
- AVL树的旋转操作详解
[0]README 0.0) 本文部分idea 转自:http://blog.csdn.net/collonn/article/details/20128205 0.1) 本文仅针对性地分析AVL树的 ...
- AVL树的单双旋转操作
把必须重新平衡的节点称为å.对于二叉树,å的两棵子树的高度最多相差2,这种不平衡可能有四种情况: 对å的左儿子的左子树进行插入节点(左-左) 对å的左儿子的右子树进行插入节点(左-右) 对å的右儿子的 ...
随机推荐
- 使用SuperWebSocket实现Web消息推送
在大部分Web系统中,我们可能遇到需要向客户端推送消息的需求.SuperWebSocket第三方库能让我们轻松的完成任务.SuperWebSocket第三方库可以从网上下载,不过通过Visual St ...
- JavaScript DOM事件对象的两个小练习 | 学习内容分享
Event 对象 Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态. 事件通常与函数结合使用,函数不会在事件发生前被执行! 本文用于记录个人学习过程 ...
- mysql中的表操作
------------恢复内容开始------------ 创建数据库 create database 数据库名 切换数据库 use 数据库名 建表: create table 表名 ( 字段名1, ...
- 【译】PEP 318--函数和方法的装饰器
PEP原文 : https://www.python.org/dev/peps/pep-0318 PEP标题: Decorators for Functions and Methods PEP作者: ...
- Pycharm学生版安装教程(2019-12月更新)
以下方法全部是官方渠道正版激活,可选择学生版(免费) 或企业版(付费) 我的机器学习教程「美团」算法工程师带你入门机器学习 以及 「三分钟系列」数据结构与算法 已经开始更新了,欢迎大家订阅~这篇专 ...
- 十三、springboot 优雅集成spring-boot-admin 实现程序监控
前言 我们知道项目的监控是尤为重要的,但是我们如果用jdk 自带的jconsole 和jvisualvm 的话会非常繁琐,且界面不是很友好.之前我们使用了spring boot 项目,但是都没有对项目 ...
- 使用git pull拉取代码的时候,无法拉取最新代码,报"unable to update local ref"错误。
使用git pull拉取代码的时候,无法拉取最新代码,报"unable to update local ref"错误. 除了重新clone一份代码外,还可以使用如下解决方案: .切 ...
- centos 7.3 服务器环境搭建——MySQL 安装和配置
centos 7.3 服务器环境搭建——MySQL 安装和配置服务器信息如下:服务器:阿里云系统 centos 7.3 (阿里云该版本最新系统)mysql版本:5.7.18 (当前时间最新版本)连接服 ...
- Linux学习之路--常用配置
1.修改IP地址 $vi /etc/sysconfig/network-scripts/ifcfg-eth0 BOOTPROTO=staticONBOOT=yesIPADDR=192.168.2.12 ...
- Ant Design框架中不同的组件访问不同的models中的数据
Ant Design框架中不同的组件访问不同的models中的数据 本文记录了我在使用该框架的时候踩过的坑,方便以后查阅. 一.models绑定 在某个组件(控件或是页面),要想从某个models中获 ...