My implementation of AVL tree
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 树的平衡 AVL Tree
本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...
- AVL Tree Insertion
Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- HTML5 编辑 API 之 Range 对象(二)
1.Range.cloneContents()The Range.cloneContents() returns a DocumentFragment copying the objects of t ...
- BCB常用文件与字符串函数
VCL库函数简介 一.BORLAND C++ BUILDER VCL的内存管理函数 1. AllocMem 在队中分配指定字节的内存块,并将分配的每一个字节初始化为 0.函数原型如下: void * ...
- java c# 加密与解密对照
原文 java c# 加密与解密对照 最近一直烦恼,java , c# 加密的不同,然后整理了一下,留个备份的轮子: 其中在 java.c#加密转换时,最重要的是 IV 的确定,我常常用如下方法使得j ...
- ubuntu下显卡管理
1 Ubuntu下卸载ATI显卡驱动并还原开源驱动[转] 首先卸载已经安装的ATI显卡驱动:cd /usr/share/ati/sudo ./fglrx-uninstall.sh 接着执行下面的代码: ...
- RESTful WebService入门
RESTful WebService入门 RESTful WebService是比基于SOAP消息的WebService简单的多的一种轻量级Web服务,RESTful WebService是没有状 ...
- JavaMail如何保证邮件发送成功
使用过JavaMail的api发送邮件的人可能会有这样一个疑惑:我如何知道我调用该api发送的邮件是否成功呢?一般的开放的api给我们调用都会有个返回值或者状态码,来告诉我们执行成功与否.但是Java ...
- Sqlmap基础(一)
(1)选项:-r REQUESTFILE Load HTTP request from a file (2)选项:--current-db Retrieve DBMS curr ...
- Redhat 使用中文安装后更换为英文的设定
vi /etc/sysconfig/i18n将LANG改为LANG=en_US.UTF-8保存退出,重新reboot
- dp,px转换
public static int dip2px(Context context, float dpValue) { final float scale = context.getRes ...
- sql语句记录
清空日志 DUMP TRANSACTION 库名 WITH NO_LOG 截断事务日志 BACKUP LOG 数据库名 WITH NO_LOG 收缩数据库 DBCC SHRINKDATABASE(数据 ...