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. LR_问题_脚本运行时提示没有参数化

    问题描述 将loadrunner中的参数删除,并且删除脚本目录下对应的bak,执行脚本,出现下面的错误: “错误: 表“XX\phonenumbser.dat”不存在.         [MsgId: ...

  2. posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序

    posix 线程(一):线程模型.pthread 系列函数 和 简单多线程服务器端程序 一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属 ...

  3. 如何实现Windows Phone代码与Unity相互通信(直接调用)

    我之前用了两篇文章写了WP与Unity相互通信.调用的办法,一个是事件,一个是插件. 这次来说个更简单的,我觉得这应该是Unity发布到WP或者Win Store上得天独厚的优势.毕竟都是C#. 懒得 ...

  4. 256. Paint House

    题目: There are a row of n houses, each house can be painted with one of the three colors: red, blue o ...

  5. netty 解决TCP粘包与拆包问题(三)

    今天使用netty的固定长度进行解码 固定长度解码的原理就是按照指定消息的长度对消息自动解码. 在netty实现中,只需要采用FiexedLengthFrameDecoder解码器即可... 以下是服 ...

  6. jumplist和changlist

    用jumplist可以在不同的访问过的位置之间跳转 C-O到上一个 C-I到下一个位置 :jumps列出跳转列表 changlist列出最近的改动点 g;到上一个,g,到下一个 :changes列出相 ...

  7. 1227. Rally Championship

    1227 题意木看懂 是可以停在路上 任何地方 水题一枚 以下条件之一满足就可以 有环(并查集判) 重边 自己到自己的边 最长边大于s(用flod改写下) #include <iostream& ...

  8. 关于Hibernate中的Configuration

    Hibernate中,关于从 Configuration中建立一个SessionFactory常用的可以有两种方法,一种是为Configuration提供hibernate.cfg.xml配置文件,还 ...

  9. rsync不存在用户处理CPU消耗拒绝服务漏洞

    受影响产品: rsync 3.1.0 漏洞描述: CVE ID:CVE-2014-2855 rsync是一款文件同步管理软件. rsync处理不存在用户时存在安全漏洞,可消耗大量CPU资源,造成拒绝服 ...

  10. Ajax、Comet与Websocket

    从 http 协议说起 1996年IETF  HTTP工作组发布了HTTP协议的1.0版本 ,到现在普遍使用的版本1.1,HTTP协议经历了17 年的发展.这种分布式.无状态.基于TCP的请求/响应式 ...