My implementation of AVL tree

时间:2021-07-14 08:06:30

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 ;
}