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. python logging 学习笔记

    logging.basicConfig函数各参数: filename: 指定日志文件名 filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a' format: 指定输出的格 ...

  2. netty是什么?

    Netty是什么? 相对于Tomcat这种Web Server(顾名思义主要是提供Web协议相关的服务的),Netty是一个Network Server,是处于Web Server更下层的网络框架,也 ...

  3. React使用rAF动画介绍

    一. <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF ...

  4. Android 时间轴TimeLine

    代码:这里

  5. 如何禁用 radio ,设置为只读,不能选定

    如何禁用 radio ,设置为只读,不能选定 禁用 radio ,设置为只读,不能选定: <input name="gender" type="radio" ...

  6. 转Unity 异常操作

    摘要 使用 unity 处理异常的方法可能会与你的直觉不符.本文将给出正确的处理方法,并简单剖析Unity这部分源代码. 处理异常 打算用Unity的AOP截获未处理的异常,然后写个日志什么的,于是我 ...

  7. javascript grunt安装和使用

    grunt是javascript世界的构建工具. 为何要用构建工具? 一句话:自动化.对于需要反复重复的任务,例如压缩(minification).编译.单元测试.linting等.自动化工具可以减轻 ...

  8. 特殊的css样式

    在一定范围大小变化的div .div { width:auto; height:auto; min-height:100px; min-width:100px; max-height:200px; m ...

  9. 在Windows下通过命令行或者.bat文件统计一个目录中文件数量

    在Windows下面怎样通过命令行统计一个目录中文件的数量,或者说,如果在一个.bat文件中,统计一个目录中的文件数量? 我原来以为是不可能的,要编一个vbs程序什么的,后来到网上找了下,发现还真是可 ...

  10. Struts2配置

    1.      设定server a)      window– preferences – myeclipse – servers – tomcat – 6.x b)      选择tomcat h ...