Sunteți pe pagina 1din 12

Lucrare de laborator

la disciplina
Structuri de Date și Algoritmi
Lucrare de laborator nr. 4:

Tema:
Implementarea tipului de date abstract “Arbore binar de căutare” în limbajul C.

Scopul lucrării
Scopul lucrării este de a familiariza studentul cu mecanismul de creare a arborelui binar de
căutare și operații elementare asupra acestuia, utilizînd pentru aceasta limbajul C.
Problema

1. Să se elaboreze un program ce va aloca dinamic un arbore de căutare binar de structuri


(unde structurile vor fi cu mai multe cîmpuri, minim 4 cîmpuri, dintre care un cîmp
pentru cheie numit key) și va realiza următoarele funcții, unde funcțiile date reprezintă
opțiuni organizate într-un meniu în cadrul programului:
- citirea de la tastatură a elementelor arborelui;
- afișarea la consolă a elementelor arborelui;
- căutarea unui nod în baza cîmpului cheie și afișarea cîmpurilor nodului găsit;
- parcurgerea arborelui în inordine;
- parcurgerea arborelui în preordine;
- parcurgerea arborelui în postordine;
- parcurgerea arborelui în adîncime( DFS);
- parcurgerea arborelui în lărgime ( BFS);
- balansarea arborelui (în imaginea de mai jos este redat un arbore balansat și unul nebalansat );

- oglindirea arborelui (orice nod copil drept, devine un nod copil stîng și analog orice nod copil
stîng devine un nod copil drept), ține cont că după oglindirea arborelui binar de căutare
proprietatea între nod și copii se va schimba, prin urmare și căutarea deja se va face în altă
ordine;
- curățirea elementelor arborelui;
- eliberarea memoriei arborelui.
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define Delay 15

typedef struct BinaryTreeNode


{
char *Name;
char *University;
int age;
char *sex;
int key;
struct BinaryTreeNode * left;
struct BinaryTreeNode * right;

}BinaryTreeNode;

typedef struct BinaryTree


{
struct BinaryTreeNode * root;
}BinaryTree;

struct BinaryTree * create_tree()


{
BinaryTree *my_binary = malloc(sizeof(struct BinaryTree));
my_binary->root = NULL;
return my_binary;
}

void NewwNode(BinaryTreeNode * current_node , BinaryTreeNode * new_node)


{
if(current_node)
{
if(current_node->key > new_node->key)
{
if(current_node->left)
{
NewwNode(current_node->left,new_node);
} else
{
current_node->left = new_node;
}
} else
{
if(current_node->right)
{
NewwNode(current_node->right,new_node);
} else
{
current_node->right = new_node;
}
}
}

BinaryTreeNode * nBTNode(char *Name,char *University, int a, char *s, int key)


{
BinaryTreeNode * new_node = calloc(sizeof(BinaryTreeNode),1);
new_node->Name = calloc(sizeof(char),15);
strcpy(new_node->Name,Name);
new_node->University = calloc(sizeof(char),15);
strcpy(new_node->University, University);
new_node->age = a;
new_node->sex = calloc(sizeof(char),1);
strcpy(new_node->sex, s);
new_node->key = key;
return new_node;
}

void AddNNodeTree(BinaryTree * myBinaryTree, BinaryTreeNode ** storage, int


count)
{
char *Name,*University,*s;
int a, k;

printf("Name: \n");
Name = calloc(sizeof(char),15);
scanf("%s",Name);

printf("University: \n");
University = calloc(sizeof(char),15);
scanf("%s",University);

printf("Age: \n");
scanf("%d",&a);

printf("Sex : \n");
s = calloc(sizeof(char),1);
scanf("%s",s);

printf("key: \n");
scanf("%d",&k);
BinaryTreeNode * new_node = nBTNode(Name,University,a,s,k);
storage[count]= new_node;
if(myBinaryTree->root)
{
NewwNode(myBinaryTree->root, new_node);
}
else
{
myBinaryTree->root = new_node;
}

BinaryTreeNode *Searcher(BinaryTreeNode *node, int new_key, int choice) {


if (choice == 1) {
if (node == NULL) {
return NULL;
}
if (node->key == new_key) {
printf("Key -> %d\n\n", new_key);
printf("Name -> %s\n", node->Name);
printf("University -> %s\n", node->University);
printf("Age -> %d\n", node->age);
printf("Sex -> %s\n", node->sex);
return node;
}
if (node && new_key < node->key) {
Searcher(node->right, new_key, choice);
} else {
Searcher(node->left, new_key, choice);
}
} else if (choice == 0) {

if (node == NULL) {
return NULL;
}
if (node->key == new_key) {
printf("Key -> %d\n\n", new_key);
printf("Name -> %s\n", node->Name);
printf("University -> %s\n", node->University);
printf("Age -> %d\n", node->age);
printf("Sex -> %s\n", node->sex);
return node;
}
if (node && new_key < node->key) {
Searcher(node->left, new_key, choice);
} else {
Searcher(node->right, new_key, choice);
}
}
}

void DeleteTreeNode(BinaryTreeNode *node)


{
if(node)
{
DeleteTreeNode(node->left);
DeleteTreeNode(node->right);
free(node->Name);
free(node->University);
free(node->sex);
free(node);
}
}
void ClearTree(BinaryTree * Tree)
{
if(Tree)
{
if(Tree->root)
{
DeleteTreeNode(Tree->root);
Tree->root = NULL;
}
}
}

void DeleteTree(BinaryTree *Tree)


{
if(Tree)
{
ClearTree(Tree);
free(Tree);
printf("Binary shaft deleted successfully\n");
}
}

int menu()
{
int error_input_found = 0;
menu:
system("cls");
printf("\n Lab -4- \n");
printf("\n 1)Add elements");
printf("\n 2)Binary tree display");
printf("\n 3)Search Node");
printf("\n 4)Go through the tree in order");
printf("\n 5)Go through the tree in preorder");
printf("\n 6)Go through the tree in postordine");
printf("\n 7)Going through the tree in depth (DFS)");
printf("\n 8)Width of the tree (BFS)");
printf("\n 9)Shaft swing(Balancer)");
printf("\n 10)Clean the shaft elements");
printf("\n 11)Releasing the memory of the tree");
printf("\n 12)Exit");
int command = 12;
if(error_input_found)
{
printf("\n\nSelect a option.\n");
}
printf("\n\nYour Option : ");
scanf("%d",&command);
while(command>12 || command<0)
{
printf("Option not exist .\n");
error_input_found = 1;
scanf("%*[^\n]");
goto menu;
}
return command;
}

void ShowTree(BinaryTreeNode *my_node, int space)


{
if (my_node == NULL)
return;
space += Delay;

ShowTree(my_node->right, space);

printf("\n");
for (int i = Delay; i < space; i++)
printf(" ");
printf("%d(%s)\n", my_node->key,my_node->Name);
ShowTree(my_node->left, space);
}

void Show(BinaryTreeNode *my_node)


{
ShowTree(my_node, 0);
}

void SVD(BinaryTreeNode * current_node) //Inordine


{
if (current_node == NULL)
{
return;
}
else
{
SVD(current_node->left);
printf("Key -> %d\n\n", current_node->key);
printf("Name -> %s\n",current_node->Name);
printf("University -> %s\n",current_node->University);
printf("Age -> %d\n",current_node->age);
printf("Sex -> %s\n\n",current_node->sex);
printf("-------------\n\n");
SVD(current_node->right);
}
}

void VSD(BinaryTreeNode * current_node) //preordine


{
if (current_node == NULL)
{
return;
}
printf("Key -> %d\n\n", current_node->key);
printf("Name -> %s\n",current_node->Name);
printf("University -> %s\n",current_node->University);
printf("Age -> %d\n",current_node->age);
printf("Sex -> %s\n\n",current_node->sex);
printf("-------------\n\n");
VSD(current_node->left);
VSD(current_node->right);
}

void SDV(BinaryTreeNode * current_node)


{
if (current_node == NULL)
{
return;
}
SDV(current_node->left);
SDV(current_node->right);
printf("Key -> %d\n\n", current_node->key);
printf("Name -> %s\n",current_node->Name);
printf("University -> %s\n",current_node->University);
printf("Age -> %d\n",current_node->age);
printf("Sex -> %s\n\n",current_node->sex);
printf("-------------\n\n");

int TreeDepth(BinaryTreeNode * node)


{
if (node == NULL)
return 0;
else {

int max_Left = TreeDepth(node->left);


int max_Right = TreeDepth(node->right);

if (max_Left>max_Right)
{
return (max_Left+1);
}
else
{
return (max_Right+1);
}
}
}
void bfsLevel(BinaryTreeNode* node, int level , int key_value)
{
if (node == NULL)
{
return;
}
if (level == 1 && (node->key == key_value))
{
printf("Key -> %d\n\n", node->key);
printf("Name -> %s\n",node->Name);
printf("University -> %s\n",node->University);
printf("Age -> %d\n",node->age);
printf("Sex -> %s\n\n",node->sex);
printf("-------------\n\n");
}
else if (level > 1) {
bfsLevel(node->left, level - 1,key_value);
bfsLevel(node->right, level - 1, key_value);
}
}
void BFS(BinaryTreeNode* node)
{
int key;
printf("Key : ");
scanf("%d", &key);
int depth = TreeDepth(node);
int i;
for (i = 0; i <=depth; i++)
bfsLevel(node, i, key);
}

void DFS(BinaryTreeNode * node, int key_value) //Inordine


{
if (node == NULL)
{
return;
}
if(node->key == key_value)
{
printf("Key -> %d\n\n", node->key);
printf("Name -> %s\n",node->Name);
printf("University -> %s\n",node->University);
printf("Age -> %d\n",node->age);
printf("Sex -> %s\n\n",node->sex);
printf("-------------\n\n");
}
else
{
DFS(node->left,key_value);
DFS(node->right,key_value );
}
}
void sortArray(BinaryTreeNode ** arr, int size)
{
BinaryTreeNode * temp;

for(int i=0;i<size;i++)
{
arr[i]->left = NULL;
arr[i]->right = NULL;
}
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(arr[i]->key>arr[j]->key)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}

}
}
}

void printArray(BinaryTreeNode ** arr, int size)


{
for(int i=0;i<size;i++)
{
printf("%d - ",arr[i]->key);
}
}

BinaryTreeNode * balanceTreeHelper(BinaryTreeNode ** storage, int start, int


end, int choice)
{

if(start<end){
int mid = (start + end)/2;
BinaryTreeNode * node = storage[mid];
if(choice!=1)
{
node->left = balanceTreeHelper(storage, start, mid, choice);
node->right = balanceTreeHelper(storage, mid+1, end, choice);
}
else
{
node->right = balanceTreeHelper(storage, start, mid, choice);
node->left = balanceTreeHelper(storage, mid+1, end, choice);
}
return node;
} else
{
return NULL;
}
}

void balanceTree(BinaryTree * tree, BinaryTreeNode ** storage, int n, int


choice)
{
int start = 0, end = n, mid = (start+end)/2;
tree->root = balanceTreeHelper(storage, start, end, choice);

int main()
{
BinaryTreeNode ** storage = NULL;

int choice = 0;
int key;
int pos, size;
int command = 12;
BinaryTree *my_binary = NULL;
while(command)
{
command = menu();
if(command == 1)
{
if(my_binary != NULL)
{
DeleteTree(my_binary);
}
my_binary = create_tree();
printf("How many will you add Element : ");
scanf("%d",&size);
storage = calloc(size,sizeof(BinaryTreeNode*));
for (int i=0;i<size;i++)
{
printf("Number #%d\n",i);
AddNNodeTree(my_binary,storage,i);
printf("\n");
}
} else if(my_binary != NULL)
{
switch(command)
{
case 2:
{
Show(my_binary->root);
break;
}
case 3:
{
printf("Key : ");
scanf("%d",&key);
Searcher(my_binary->root,key,choice);
break;
}

case 4:
{
SVD(my_binary->root);
break;
}
case 5:
{
VSD(my_binary->root);
break;
}
case 6:
{
SDV(my_binary->root);
break;
}
case 7:
{
printf("Key : ");
scanf("%d",&key);
DFS(my_binary->root,key);
break;
}
case 8:
{

BFS(my_binary->root);

}
case 9:
{
sortArray(storage,size);
printArray(storage,size);
balanceTree(my_binary,storage,size,choice);

break;
}

case 10:
{
ClearTree(my_binary);
break;
}
case 11:
{
DeleteTree(my_binary);
break;
}
case 12:
{
return 0;
}
}
} else
{
printf("\nCreate a Tree \n");
}
printf("\n\nPress any Key to continue!\n");
_getch();
}

return 0;
}
Rezultatul
Rezultaul lucrării se va plasa într-un raport, unde va fi inclusă foaia de titlu și codul sursă a
programului cu o poză de execuție a acestuia.