AVL Tree
#include <iostream>
#include <functional>
using namespace Std;
Class Avl_tree
{
Private
struct tree
{
int data;
Tree* L;
Tree* R;
int height;
Tree (int data_):d ata (Data_), L (0), R (0), height (0) {}
};
tree* Root;
Public
Avl_tree (): root (0) {}
void Insert (int data)
{
Function<void (tree*&, int) > INS = [&] (tree*& R, int d)
{
if (r = = NULL)
{
r = new Tree (d);
}
else if (D < r->data)
{
Ins (r->l, D);
if (height (r->l)-height (r->r) = = 2)
{
if (D < r->l->data)
{
R = LL (r);
}
Else
{
R = LR (r);
}
}
}
else if (d > R->data)
{
Ins (R->r, D);
if (height (r->r)-height (r->l) = = 2)
{
if (d > R->r->data)
{
R = RR (r);
}
Else
{
R = RL (r);
}
}
}
R->height = Compare (height (r->l), height (r->r)) + 1;
};
Ins (root, data);
}
void Delete (int data)
{
You cannot use the two-fork tree to delete the left-right method, so the height difference will be confusing.
Function<void (tree*&, int) > del = [&] (tree*& R, int d)
{
if (r = = NULL)
{
Return
}
if (D < r->data)
{
Del (r->l, D);
if (height (r->r)-height (r->l) = = 2)
{
tree* temp = r->r; Right now it's high.
if (Height (temp->l) > Height (temp->r))//dual rotation required
{
R = RL (r);
}
else//Require single rotation
{
R = RR (r);
}
}
}
else if (d > R->data)
{
Del (r->r, D);
if (height (r->l)-height (r->r) = = 2)
{
tree* temp = r->l;
if (height (temp->l) >= height (temp->r))
{
R = LL (r);
}
Else
{
R = LR (r);
}
}
}
Else
{
if (r->l && r->r)//The Left and right nodes of the node to be deleted are not empty
{
if (Height (r->l) > Height (r->r))//If you want to delete the node's Supi right subtree height, you can put the largest left node on the node you want to delete, you can maintain the balance of the tree
{
tree* temp = r->l;
while (TEMP->R)
{
temp = temp->r;
}
R->data = temp->data;
Del (r->l, temp->data);
}
else//If the right subtree of the node to be deleted is taller or equal than the left subtree, the smallest node on the right can be placed on the node to be deleted, maintaining the balance of the book
{
tree* temp = r->r;
while (TEMP->L)
{
temp = temp->l;
}
R->data = temp->data;
Del (R->r, temp->data);
}
}
else//If the left and right nodes are not all non-empty, this time there is no further discussion of the situation, directly delete the node
{
tree* temp = r;
r = (r->l! = NULL)? r->l:r->r;
Delete temp;
}
}
};
Del (root, data);
}
void Destroy ()
{
Function<void (tree*) > des = [&] (tree* R)
{
if (r = = NULL)
{
Return
}
Des (R->L);
Des (R->r);
Delete R;
};
Des (Root);
root = NULL;
}
int Find_max ()
{
tree* temp = root;
while (TEMP->R)
{
temp = temp->r;
}
Return temp->data;
}
int Find_min ()
{
tree* temp = root;
while (TEMP->L)
{
temp = temp->l;
}
Return temp->data;
}
void Travel_mid ()
{
Function<void (tree*) > tra = [&] (tree* root)
{
if (root = = NULL)
{
Return
}
TRA (root->l);
/*int temp = root->height;
while (temp)
{
cout << ' t ';
temp--;
}*/
cout << root->data << Endl;
TRA (root->r);
};
TRA (root);
}
tree* LL (tree* t)
{
tree* temp = t->l;
T->l = temp->r;
Temp->r = t;
T->height = Compare (height (t->l), height (t->r)) + 1;
Temp->height = Compare (height (t->l), height (t->r)) + 1;
return temp;
}
tree* RR (tree* t)
{
tree* temp = t->r;
T->r = temp->l;
Temp->l = t;
T->height = Compare (height (t->l), height (t->r)) + 1;
Temp->height = Compare (height (t->l), height (t->r)) + 1;
return temp;
}
tree* LR (tree* t)
{
T->l = RR (t->l);
return LL (t);
}
tree* RL (tree* t)
{
T->r = LL (t->r);
return RR (t);
}
int Height (tree* t)
{
if (t = = NULL)
{
return-1;
}
Return t->height;
}
int Compare (int one, int)
{
Return one >? One:two;
}
void Travel_tree ()
{
Function<void (tree*, int) > tra = [&] (tree* R, Int j)
{
if (r = = NULL)
{
Return
}
for (int i = 0; i < J; i++)
{
cout << "";
}
cout << r->data << Endl;
Tra (r->l, j+1);
Tra (r->r, j+1);
};
int judge = 0;
TRA (root, judge);
}
};
int main ()
{
Avl_tree T;
for (int i = 0; i <; i++)
{
T.insert (i);
}
T.travel_tree ();
T.delete (5);
T.travel_tree ();
Cin.get ();
return 0;
}
AVL Tree C + + implementation