cpp Copy code
#include <iostream>
using namespace std;
struct Node
{
int key, count;
Node *left, *right;
int balance;
};
Node* search(int x, Node* p, short h) {
Node* p1, * p2;
if (p == NULL) {
p = new Node;
p->key = x;
p->count = 1;
p->balance = 0;
p->left = NULL;
p->right = NULL;
}
else if (x < p->key) {
p->left = search(x, p->left, h);
if (h != 0) {
switch (p->balance) {
case 1:
p->balance = 0;
h = 0;
break;
case 0:
p->balance = -1;
break;
case -1:
p1 = p->left;
if (p1->balance == -1) {
p->left = p1->right;
p1->right = p;
p->balance = 0;
p = p1;
}
else {
p2 = p1->right;
p1->right = p2->left;
p2->left = p1;
p->left = p2->right;
p2->right = p;
if (p2->balance == -1)
p->balance = 1;
else
p->balance = 0;
if (p2->balance == 1)
p1->balance = -1;
else
p1->balance = 0;
p = p2;
}
p->balance = 0;
h = 0;
break;
}
}
}
else if (x > p->key) {
p->right = search(x, p->right, h);
if (h != 0) {
switch (p->balance) {
case -1:
p->balance = 0;
h = false;
break;
case 0:
p->balance = 1;
break;
case 1:
p1 = p->right;
if (p1->balance == 1) {
p->right = p1->left;
p1->left = p;
p->balance = 0;
p = p1;
}
else {
p2 = p1->left;
p1->left = p2->right;
p2->right = p1;
p->right = p2->left;
p2->left = p;
if (p2->balance == 1)
p->balance = -1;
else
p->balance = 0;
if (p2->balance == -1)
p1->balance = 1;
else
p1->balance = 0;
p = p2;
}
p->balance = 0;
h = 0;
break;
}
}
}
else {
p->count += 1;
h = 0;
}
return p;
}
void balance1(Node* p, short h) {
Node* p1, * p2;
short b1, b2;
switch (p->balance) {
case -1:
p->balance = 0;
break;
case 0:
p->balance = 1;
h = 0;
break;
case 1:
p1 = p->right;
b1 = p1->balance;
if (b1 >= 0) {
p->right = p1->left;
p1->left = p;
if (b1 == 0) {
p->balance = 1;
p1->balance = -1;
h = 0;
}
else {
p->balance = 0;
p1->balance = 0;
}
p = p1;
}
else {
p2 = p1->left;
b2 = p2->balance;
p1->left = p2->right;
p2->right = p1;
p->right = p2->left;
p2->left = p;
if (b2 == 1)
p->balance = -1;
else
p->balance = 0;
if (b2 == -1)
p1->balance = 1;
else
p1->balance = 0;
p = p2;
p2->balance = 0;
}
}
}
void balance2(Node* p, short h) {
Node* p1, * p2;
short b1, b2;
switch (p->balance) {
case 1:
p->balance = 0;
break;
case 0:
p->balance = 0;
h = 0;
break;
case -1:
p1 = p->left;
b1 = p1->balance;
if (b1 <= 0) {
p->left = p1->right;
p1->right = p;
if (b1 == 0) {
p->balance = -1;
p1->balance = 1;
h = 0;
}
else {
p->balance = 0;
p1->balance = 0;
}
p = p1;
}
else {
p2 = p1->right;
b2 = p2->balance;
p1->right = p2->left;
p2->left = p1;
p->left = p2->right;
p2->right = p;
if (b2 == -1)
p->balance = 1;
else
p->balance = 0;
if (b2 == 1)
p1->balance = -1;
else
p1->balance = 0;
p = p2;
p2->balance = 0;
}
}
}
void del(Node* r, Node* q, short h) {
if (r->right != NULL) {
del(r->right, q, h);
if (h != 0)
balance2(r, h);
}
else {
q->key = r->key;
q->count = r->count;
q->balance = r->balance;
r = r->left;
h = 1;
}
}
Node* delet(int x, Node* p, short h) {
Node* q;
if (p == NULL) {
cout << "Key is not in tree" << endl;
h = 0;
}
else if (x < p->key) {
p->left = delet(x, p->left, h);
if (h != 0)
balance1(p, h);
}
else if (x > p->key) {
p->right = delet(x, p->right, h);
if (h != 0)
balance2(p, h);
}
else {
q = p;
if (q->right == NULL) {
p = q->left;
h = 1;
}
else if (q->left == NULL) {
p = q->right;
h = 1;
}
else {
del(q->left, q, h);
if (h != 0)
balance1(p, h);
}
delete q;
}
return p;
}
Node* tree(int x) {
Node* p;
p = new Node;
p->key = x;
p->count = 1;
p->balance = 0;
p->left = NULL;
p->right = NULL;
return p;
}
void printtree(Node* t, short h) {
if (t != NULL) {
printtree(t->right, h + 1);
for (int i = 1; i <= h; i++) {
cout << ' ';
}
cout << t->key << endl;
printtree(t->left, h + 1);
}
}
int main() {
Node* root = NULL;
short h = 0;
root = search(9, root, h);
root = search(1, root, h);
root = search(2, root, h);
root = search(2, root, h);
root = search(0, root, h);
root = search(9, root, h);
root = search(0, root, h);
root = search(3, root, h);
root = search(0, root, h);
root = search(4, root, h);
cout << "AVL Tree: " << endl;
printtree(root, h);
return 0;
}