用类封装AVL的几个属性,应该没有问题,如果有错请告知我,谢谢
其中打印树参考http://www.cnblogs.com/skywang12345/p/3577479.html
头文件
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void testavl();
主函数
#include "Header.h"
int main()
{
testavl();
return 0;
}
AVL类实现
#include "Header.h"
struct node
{
node* leftchild;
node* rightchild;
node* parents;
int key;
node(){};
node(int a){ leftchild = NULL; rightchild = NULL; parents = NULL; key = a; };
};
class AVL
{
public:
AVL();
node* root_node;
int Height(node* nd);
void Insert(node* nd);
void LeftRotate(node* nd);
void RightRotate(node* nd);
node* MinChild(node* nd);
void Transplant(node *u, node *v);
void Delete(node* nd);
void Delete(int value);
void InsertFixup(node* nd);
void DeleteFixup(node* nd);
};
AVL::AVL()
{
root_node = NULL;//用哨兵编程会简单一点,写的时候没有用哨兵
}
inline int max(int x, int y)
{
return x > y ? x : y;
}
node* AVL::MinChild(node* nd) //返回以nd为根节点的子树的关键词最小节点
{
node* min;
min = NULL;
while (nd)
{
min = nd;
nd = nd->leftchild;
}
return min;
}
void AVL::Delete(node* nd)
{
if (nd->leftchild == NULL && nd->rightchild == NULL)
{
if (nd->parents == NULL)
{
root_node == NULL;
}
else
{
if (nd == nd->parents->leftchild)
{
nd->parents->leftchild = NULL;
}
else
{
nd->parents->rightchild = NULL;
}
DeleteFixup(nd->parents);
}
}
else
{
if (nd->leftchild == NULL)
{
if (nd->rightchild)
{
Transplant(nd, nd->rightchild);
}
DeleteFixup(nd->parents);
}
else if (nd->rightchild == NULL)
{
if (nd->leftchild)
{
Transplant(nd, nd->leftchild);
}
DeleteFixup(nd->parents);
}
else
{
node* z;
z = MinChild(nd->rightchild);
node* x;
x = z->parents;
if (z->parents != nd)
{
Transplant(z, z->rightchild);
z->rightchild = nd->rightchild;
z->rightchild->parents = z;
}
else
{
x = z;
}
Transplant(nd, z);
z->leftchild = nd->leftchild;
z->leftchild->parents = z;
DeleteFixup(x);
}
}
}
int AVL::Height(node* nd)
{
if (nd == NULL)
{
return 0;
}
else
{
return max(Height(nd->leftchild), Height(nd->rightchild)) + 1;
}
}
void AVL::Transplant(node *u, node *v)
{
if (/*root_node == u*/u->parents == NULL)
{
root_node = v;
}
else if (u == u->parents->leftchild)
{
u->parents->leftchild = v;
}
else
{
u->parents->rightchild = v;
}
if (v)
{
v->parents = u->parents;
}
}
void AVL::Insert(node* nd)
{
node* x = new node;
x = root_node;
node* y = NULL;
while (x)
{
y = x;
if (nd->key > x->key)
{
x = x->rightchild;
}
else
{
x = x->leftchild;
}
}
if (y == NULL)
{
root_node = nd;
}
else
{
nd->parents = y;
if (nd->key > y->key)
{
y->rightchild = nd;
}
else
{
y->leftchild = nd;
}
}
if (nd->parents != NULL)
{
InsertFixup(nd->parents);
}
}
void AVL::InsertFixup(node* nd)
{
node* r;// record_location;
node* r_p;// parents of r;
r = nd;
while (r->parents != NULL)
{
r_p = r->parents;
int r_bf = Height(r->rightchild) - Height(r->leftchild);//可以写成节点的一个属性
//int rp_bf = Height(r_p->rightchild) - Height(r_p->leftchild);///////
int rp_bf = 0;
int rr = Height(r_p->rightchild);
int rl = Height(r_p->leftchild);
rp_bf = rr - rl;
if (rp_bf == 2)//
{
if (r_bf == 1)
{
LeftRotate(r_p);
}
else
{
RightRotate(r);
LeftRotate(r->parents->parents);
//RLRotate
}
}
else if (rp_bf == -2)
{
if (r_bf == 1)
{
LeftRotate(r);
RightRotate(r->parents->parents);
}
else
{
RightRotate(r_p);
}
}
r = r->parents;
if (r == NULL)
{
break;
}
}
}
void AVL::DeleteFixup(node* nd)
{
while (nd != NULL)
{
if (Height(nd->rightchild) - Height(nd->leftchild) == -2)
{
if (Height(nd->leftchild->rightchild) - Height(nd->leftchild->leftchild) == -1)
{
RightRotate(nd);
}
else
{
LeftRotate(nd->leftchild);
RightRotate(nd);
}
}
else if (Height(nd->rightchild) - Height(nd->leftchild) == 2)
{
if (Height(nd->rightchild->rightchild) - Height(nd->rightchild->leftchild) == -1)
{
RightRotate(nd->rightchild);
LeftRotate(nd);
}
}
nd = nd->parents;
}
}
void AVL::LeftRotate(node* nd)
{
node* y;
y = nd->rightchild;
nd->rightchild = y->leftchild;
if (y->leftchild != NULL)
{
y->leftchild->parents = nd;
}
y->parents = nd->parents;
if (nd->parents == NULL)
{
root_node = y;
}
else if (nd == nd->parents->leftchild)
{
nd->parents->leftchild = y;
}
else
{
nd->parents->rightchild = y;
}
y->leftchild = nd;
nd->parents = y;
}
void AVL::RightRotate(node* nd)
{
node* y;
y = nd->leftchild;
nd->leftchild = y->rightchild;
if (y->rightchild != NULL)
{
y->rightchild->parents = nd;
}
y->parents = nd->parents;
if (nd->parents == NULL)
{
root_node = y;
}
else if (nd == nd->parents->leftchild)
{
nd->parents->leftchild = y;
}
else
{
nd->parents->rightchild = y;
}
y->rightchild = nd;
nd->parents = y;
}
void PrintTree(node* nd, int nodekey, int direction) //打印以nd为根的子树的所有元素
{
if (nd != NULL)
{
if (direction == 0)
{
cout << setw(2) << nd->key << " is root" << endl;
}
else
{
string s;
s = direction == 1 ? "right" : "left";
cout <<setw(2) << nd->key << " is " << setw(2) << nodekey << "'s ";
cout <<setw(6) << s;
cout << " child" << endl;
}
PrintTree(nd->leftchild, nd->key, -1);
PrintTree(nd->rightchild, nd->key, 1);
}
}
void testavl()
{
AVL test_tree;
int nodekey[16] = { 3, 2, 1, 4, 5, 6, 7, 16, 15, 14, 13, 12, 11, 10, 8, 9 };
node* test_nodes = new node[16];
for (int i = 0; i < 16; i++)
{
test_nodes[i].key = nodekey[i];
test_nodes[i].leftchild = test_nodes[i].rightchild = test_nodes[i].parents = NULL;
test_tree.Insert(&test_nodes[i]);
cout << i << endl;
}
cout << "生成的AVL为:" << endl;
PrintTree(test_tree.root_node, test_tree.root_node->key, 0);
test_tree.Delete(test_tree.root_node);
cout << "删除根节点之后的AVL为:" << endl;
PrintTree(test_tree.root_node, test_tree.root_node->key, 0);
}