C++实现的avl平衡树

 #include <stdlib.h>
#include <time.h>
#include <string.h>
#include <vector>
#include <stdio.h> using namespace std; class AvlNode
{
public :
int data;
AvlNode *parent;
AvlNode *left;
AvlNode *right;
int height;
int cx;
int cy;
AvlNode()
{
left = right = parent = NULL;
height = ;
cx = cy = data = ;
} }; class AvlTree
{
public :
AvlNode *root;
void rotateR(AvlNode *node);
void rotateL(AvlNode *node);
int getChildHeight(AvlNode *node);
int getNodeHeight(AvlNode *node);
int getBal(AvlNode *node);
void recallBalancing(AvlNode *node);
void insertValue(const int value);
void preTravel(const AvlNode *node,vector<int> &vec);
void postTravel(const AvlNode *node, vector<int> &vec);
void midTravel(const AvlNode *node, vector<int> &vec);
AvlTree() : root(NULL) {} ;
}; int AvlTree::getNodeHeight(AvlNode *node)
{
if (node == NULL) return ;
return getNodeHeight(node->left) - getNodeHeight(node->right);
} static inline int Max(const int a, const int b)
{
return (a>b)?a:b;
} int AvlTree::getBal(AvlNode *node)
{
if (node == NULL) return ;
return getNodeHeight(node->left) - getNodeHeight(node->right);
} void AvlTree::preTravel(const AvlNode *node, vector<int> &vec)
{
if (node == NULL) return;
vec.push_back(node->data);
preTravel(node->left, vec);
preTravel(node->right, vec);
} void AvlTree::midTravel(const AvlNode *node, vector<int> &vec)
{
if (node == NULL) return;
midTravel(node->left, vec);
vec.push_back(node->data);
midTravel(node->right, vec);
} void AvlTree::postTravel(const AvlNode *node, vector<int> &vec)
{
if (node == NULL) return;
postTravel(node->left, vec);
postTravel(node->right, vec);
vec.push_back(node->data);
} void AvlTree::insertValue(const int value)
{
AvlNode *cursor = root;
if (cursor == NULL) {
AvlNode *node = new AvlNode;
node->data = value;
root = node;
return;
} int ret;
//printf("begin while\n");
while (cursor != NULL) {
ret = cursor->data;
// printf("%d %d\n",value,ret);
if (value < ret) {
if (cursor->left == NULL) {
AvlNode *node = new AvlNode;
node->data = value;
node->parent = cursor;
cursor->left = node;
break;
}
cursor = cursor->left;
}
else if (value > ret) {
if (cursor->right == NULL) {
AvlNode *node = new AvlNode;
node->data = value;
node->parent = cursor;
cursor->right = node;
break;
}
cursor = cursor->right;
}
else {
return;
}
} // while
recallBalancing(cursor);
} int AvlTree::getChildHeight(AvlNode *node)
{
return Max(getNodeHeight(node->left), getNodeHeight(node->right));
} void AvlTree::recallBalancing(AvlNode *node)
{
AvlNode *cursor = node;
int bal;
// printf("Begin balancing\n");
while (cursor != NULL)
{
bal = getBal(cursor);
if (bal == ) {
return;
}
else if (bal == - || bal == ) {
cursor->height = getChildHeight(cursor) + ;
cursor = cursor->parent;
}
else if (bal > ) {
bal = getBal(cursor->left);
if (bal < )
rotateL(cursor->left);
rotateR(cursor);
return;
}
else {
bal = getBal(cursor->right);
if (bal > )
rotateR(cursor->right);
rotateL(cursor);
return;
}
}
// printf("End balance\n");
} void AvlTree::rotateL(AvlNode *node)
{
AvlNode*pivot = node->right;
pivot->height = node->height;
node->height--;
AvlNode*parent = node->parent; node->right = pivot->left;
if (pivot->left)
pivot->left->parent = node;
pivot->left = node;
node->parent = pivot; pivot->parent = parent;
if (parent)
{
if (parent->left == node)
parent->left = pivot;
else parent->right = pivot;
}
else
{
// parent == NULL retur new root
root = pivot;
}
} void AvlTree::rotateR(AvlNode *node)
{
AvlNode*pivot = node->left;
pivot->height = node->height;
node->height--;
AvlNode*parent = node->parent; node->left = pivot->right;
if (pivot->right)
pivot->right->parent = node;
pivot->right = node;
node->parent = pivot; pivot->parent = parent;
if (parent)
{
if (parent->left == node)
parent->left = pivot;
else parent->right = pivot;
}
else
{
// parent == NULL retur new root
root = pivot;
}
} int main(int argc,char **argv)
{
AvlTree avltree;
vector<int> vec; srand(time(NULL));
int i;
for (i=; i < ; i++)
avltree.insertValue(rand()%);
avltree.midTravel(avltree.root, vec);
for (auto elem : vec) {
printf("%d ", elem);
} return ;
}

My implementation of AVL tree的更多相关文章

  1. 04-树5 Root of AVL Tree

    平衡二叉树 LL RR LR RL 注意画图理解法 An AVL tree is a self-balancing binary search tree. In an AVL tree, the he ...

  2. 1066. Root of AVL Tree (25)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  3. 1066. Root of AVL Tree

    An AVL tree is a self-balancing binary search tree.  In an AVL tree, the heights of the two child su ...

  4. 树的平衡 AVL Tree

    本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...

  5. AVL Tree Insertion

    Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...

  6. 1123. Is It a Complete AVL Tree (30)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  7. A1123. Is It a Complete AVL Tree

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  8. A1066. Root of AVL Tree

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  9. PAT A1123 Is It a Complete AVL Tree (30 分)——AVL平衡二叉树,完全二叉树

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

随机推荐

  1. SpringMVC学习总结(三)——Controller接口详解(1)

    4.12.ParameterizableViewController 参数化视图控制器,不进行功能处理(即静态视图),根据参数的逻辑视图名直接选择需要展示的视图. <bean name=&quo ...

  2. C语言中时间调用处理的相关函数介绍

    asctime(将时间和日期以字符串格式表示) 相关函数:time,ctime,gmtime,localtime 表头文件:#include<time.h> 定义函数:char * asc ...

  3. iOS开发 -- 发送JSON数据给服务器

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.URL NSURL *url = [NSURL URLW ...

  4. NPOI读取Excel,导入数据到Excel练习01

    NPOI 2.2.0.0,初级读取导入Excel 1.读取Excel,将数据绑定到dgv上 private void button1_Click(object sender, EventArgs e) ...

  5. PHP命名空间概念解析

    1. PHP中的命名空间是什么? 什么是命名空间?“从广义上来说,命名空间是一种封装事物的方法.在很多地方都可以见到这种抽象概念.例如,在操作系统中目录用来将相关文件分组,对于目录中的文件来说,它就扮 ...

  6. java web 下实现文件下载

    来自:http://blog.csdn.net/longshengguoji/article/details/39433307 需求:实现一个具有文件下载功能的网页,主要下载压缩包和图片 两种实现方法 ...

  7. tengine lua 开源一 调用内部接口高效发送文件

    tengine  lua 开源一 调用内部接口高效发送文件 开源自己封装的sendfile 模块,可以高效的通过lua发送文件 源码地址:https://github.com/weinyzhou/Lu ...

  8. 打开一个已经写好的Android studio工程的方法

  9. bzoj1832: [AHOI2008]聚会

    写过的题... #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...

  10. Lost connection to MySQL server at 'reading initial communication packet' 错误解决

    Lost connection to MySQL server at 'reading initial communication packet' 错误解决 上次解决了这个问题,今天又碰到,突然失忆, ...