0% found this document useful (0 votes)
38 views9 pages

SD Lab 10

The document contains code for implementing an AVL tree data structure in C++. It includes functions for inserting and deleting nodes from the tree, balancing operations to maintain the AVL property, and functions for traversing and printing the tree. The main function demonstrates inserting values into an empty tree, printing the inorder traversal, and freeing the memory.

Uploaded by

X
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views9 pages

SD Lab 10

The document contains code for implementing an AVL tree data structure in C++. It includes functions for inserting and deleting nodes from the tree, balancing operations to maintain the AVL property, and functions for traversing and printing the tree. The main function demonstrates inserting values into an empty tree, printing the inorder traversal, and freeing the memory.

Uploaded by

X
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

AVL

Main.cpp
#include "Header.h"
int Eq_required;

int main() {
Nod* rad = 0;
int val;

cout << "9 7 5 3 6 8 10 12 14 15 11 0\n";

cin >> val;


while (val) {
cout << "\nInserare " << val << ": ";
Inserare(rad, val);
cout << endl;
IndentedListing(rad, 0);
cout << endl;
cin >> val;
}

cout << endl;


cout << "Afisare inordine: ";
Inorder(rad);
cout << endl;
IndentedListing(rad, 0);
cout << endl;

/*for (int i = 0; i < 7; ++i) {


cin >> val;
Delete(rad, val);
cout << endl;
IndentedListing(rad, 0);
cout << endl;
}*/

FreeMemory(rad);

return 0;
}

Functii.cpp
#include "Header.h"
extern int Eq_required;

Nod* MakeNod(int val) {


Nod* p = new Nod;
p->data = val;
p->stg = 0;
p->drt = 0;
p->bf = 0;

return p;
}

void RSS(Nod*& a) {
Nod* b;
b = a->drt;
a->drt = b->stg;
b->stg = a;
a->bf = b->bf = 0;
a = b;
}

void RSD(Nod*& a) {
Nod* b;
b = a->stg;
a->stg = b->drt;
b->drt = a;
a->bf = b->bf = 0;
a = b;
}

void RDS(Nod*& a) {
Nod* b, * c;
b = a->drt;
c = b->stg;

switch (c->bf) {
case 0:
a->bf = 0;
b->bf = 0;
break;
case -1:
a->bf = 1;
b->bf = 0;
break;
case 1:
a->bf = 0;
b->bf = -1;
}

a->drt = c->stg;
b->stg = c->drt;
c->bf = 0;
c->stg = a;
c->drt = b;
a = c;
}

void RDD(Nod*& a) {
Nod* b, * c;
b = a->stg;
c = b->drt;

switch (c->bf) {
case 0:
a->bf = 0;
b->bf = 0;
break;
case -1:
a->bf = 0;
b->bf = 1;
break;
case 1:
a->bf = -1;
b->bf = 0;
}

a->stg = c->drt;
b->drt = c->stg;
c->bf = 0;
c->drt = a;
c->stg = b;
a = c;
}

void Echilibrare(Nod*& a, int val, bool stg) {


if (Eq_required) {
if (stg == true) {
switch (a->bf) {
case 1:
Eq_required = false;
val < a->stg->data ? RSD(a) : RDD(a);
val < a->stg->data ? cout << "rotatie simpla dreapta" : cout << "rotatie dubla
dreapta";
break;
case 0:
a->bf = 1;
break;
case -1:
a->bf = 0;
Eq_required = false;
}
}
else {
switch (a->bf) {
case -1:
Eq_required = false;
val > a->drt->data ? RSS(a) : RDS(a);
val < a->stg->data ? cout << "rotatie simpla stanga" : cout << "rotatie dubla
stanga";
break;
case 0:
a->bf = -1;
break;
case 1:
a->bf = 0;
Eq_required = false;
}
}
}
else
Eq_required = false;
}

void Inserare(Nod*& rad, int val) {


if (!rad) {
rad = MakeNod(val);
Eq_required = true;
}
else {
if (val < rad->data) {
Inserare(rad->stg, val);
Echilibrare(rad, val, true);
}
else if (val > rad->data) {
Inserare(rad->drt, val);
Echilibrare(rad, val, false);
}
}
}

void Inorder(Nod* rad) {


if (rad)
{
Inorder(rad->stg);
cout << rad->data << " ";
Inorder(rad->drt);
}
}

void IndentedListing(Nod* rad, int level) {


int i;
cout << endl;
for (i = 0; i < level; ++i) {
cout << '\t';
}
if (!rad) {
cout << '-';
}
else {
cout << rad->data;
IndentedListing(rad->stg, level + 1);
IndentedListing(rad->drt, level + 1);
}
}

void FreeMemory(Nod*& rad) {


if (rad) {
if (!rad->stg && !rad->drt) {
delete rad;
rad = 0;
}
else {
FreeMemory(rad->stg);
FreeMemory(rad->drt);
delete rad;
rad = 0;
}
}
}

void EchilibrareDelete(Nod*& a, int val, bool stg) {


if (Eq_required) {
if (stg == true) {
switch (a->bf) {
case -1:
Eq_required = false;
val < a->stg->data ? RSS(a) : RDS(a);
val < a->stg->data ? cout << "rotatie simpla stanga" : cout << "rotatie dubla
stanga";
break;
case 0:
a->bf = -1;
break;
case 1:
a->bf = 0;
Eq_required = false;
}
}
else {
switch (a->bf) {
case 1:
Eq_required = false;
val > a->drt->data ? RSD(a) : RDD(a);
val < a->stg->data ? cout << "rotatie simpla dreapta" : cout << "rotatie dubla
dreapta";

break;
case 0:
a->bf = 1;
break;
case -1:
a->bf = 0;
Eq_required = false;
}
}
}
else
Eq_required = false;
}

void Delete(Nod*& rad, int val) {


if (rad) {
if (val < rad->data) {
Delete(rad->stg, val);
EchilibrareDelete(rad, val, true);
}
else if (val > rad->data) {
Delete(rad->drt, val);
EchilibrareDelete(rad, val, false);
}
else if(val == rad->data){
Eq_required = true;
DeleteRoot(rad);
}
}
}

void DeleteRoot(Nod*& rad) {


Nod* p;
if (!rad->stg) {
p = rad->drt;
delete rad;
rad = p;
}
else if (!rad->drt) {
p = rad->stg;
delete rad;
rad = p;

}
else {
p = RemoveGreatest(rad->stg);
p->stg = rad->stg;
p->drt = rad->drt;
delete rad;
rad = p;
}
}

Nod* RemoveGreatest(Nod*& rad) {


Nod* p;
if (!rad->drt) {
p = rad;
rad = rad->stg;
return p;
}
return RemoveGreatest(rad->drt);
}

Header.h
#pragma once
#include <iostream>
using namespace std;

struct Nod {
int data;
int bf;
Nod* stg, * drt;
};

Nod* MakeNod(int val);


void RSS(Nod*& a);
void RSD(Nod*& a);
void RDS(Nod*& a);
void RDD(Nod*& a);
void Echilibrare(Nod*& a, int val, bool stg);
void Inserare(Nod*& rad, int val);

void Inorder(Nod* rad);


void IndentedListing(Nod* rad, int level);
void FreeMemory(Nod*& rad);

void EchilibrareDelete(Nod*& a, int val, bool stg);


void Delete(Nod*& rad, int val);
void DeleteRoot(Nod*& rad);
Nod* RemoveGreatest(Nod*& rad);

You might also like