Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include<iostream>
- #include<malloc.h>
- using namespace std;
- struct Localitate
- {
- int id;
- char* denumire;
- };
- struct Nod
- {
- Localitate info;
- Nod* left;
- Nod* right;
- };
- Localitate creareLocalitate(int id, char* denumire)
- {
- Localitate l;
- l.id = id;
- l.denumire = (char*)malloc(sizeof(char)*(strlen(denumire) + 1));
- strcpy(l.denumire, denumire);
- return l;
- }
- void afisareLocalitate(Localitate l)
- {
- printf("Localitatea cu id %d, are denumirea %s.\n", l.id, l.denumire);
- }
- Nod* creareNod(Localitate info, Nod* left, Nod* right)
- {
- Nod* nou = (Nod*)malloc(sizeof(Nod));
- nou->info = creareLocalitate(info.id, info.denumire);
- nou->left = left;
- nou->right = right;
- return nou;
- }
- Nod* inserareArbore(Nod* root, Localitate info)
- {
- if (root)
- {
- if (info.id < root->info.id)
- {
- root->left = inserareArbore(root->left, info);
- }
- else if (info.id > root->info.id)
- {
- root->right = inserareArbore(root->right, info);
- }
- return root;
- }
- else
- {
- Nod* nou = creareNod(info, NULL, NULL);
- return nou;
- }
- }
- void afiarePreordine(Nod* root)
- {
- if (root)
- {
- afisareLocalitate(root->info);
- afiarePreordine(root->left);
- afiarePreordine(root->right);
- }
- }
- void afiareInordine(Nod* root)
- {
- if (root)
- {
- afiareInordine(root->left);
- afisareLocalitate(root->info);
- afiareInordine(root->right);
- }
- }
- void afiarePostordine(Nod* root)
- {
- if (root)
- {
- afiareInordine(root->left);
- afiareInordine(root->right);
- afisareLocalitate(root->info);
- }
- }
- int max(int a, int b)
- {
- if (a > b)
- return a;
- else
- return b;
- }
- int inaltimeArbore(Nod* root)
- {
- if (root)
- {
- return 1 + max(inaltimeArbore(root->left), inaltimeArbore(root->right));
- }
- else
- {
- return 0;
- }
- }
- void afisareNivelArbore(Nod* root, int nivelCautat, int nivelCurent)
- {
- if (root)
- {
- if (nivelCurent == nivelCautat)
- {
- afisareLocalitate(root->info);
- }
- else
- {
- (nivelCurent)++;
- afisareNivelArbore(root->left, nivelCautat, nivelCurent);
- (nivelCurent)--;
- afisareNivelArbore(root->right, nivelCautat, nivelCurent);
- }
- }
- }
- Nod* cautaNodArbore(Nod* radacina, int cod)
- {
- if (radacina)
- {
- if (radacina->info.id == cod)
- {
- return radacina;
- }
- else
- {
- if (radacina->info.id > cod)
- {
- return cautaNodArbore(radacina->left, cod);
- }
- else
- {
- return cautaNodArbore(radacina->right, cod);
- }
- }
- }
- else
- {
- return NULL;
- }
- }
- void stergereArbore(Nod** root) {
- if (*root) {
- stergereArbore(&(*root)->left);
- stergereArbore(&(*root)->right);
- free((*root)->info.denumire);
- free(*root);
- *root = NULL;
- }
- }
- void main()
- {
- //Localitate l = creareLocalitate(15, "Bucuresti");
- //afisareLocalitate(l);
- Localitate l1 = creareLocalitate(15, "Bucuresti");
- Localitate l2 = creareLocalitate(10, "Bucuresti");
- Localitate l3 = creareLocalitate(20, "Bucuresti");
- Localitate l4 = creareLocalitate(8, "Bucuresti");
- Localitate l5 = creareLocalitate(12, "Bucuresti");
- Localitate l6 = creareLocalitate(17, "Bucuresti");
- Localitate l7 = creareLocalitate(25, "Bucuresti");
- //afisareLocalitate(l1);
- //afisareLocalitate(l2);
- //afisareLocalitate(l3);
- //afisareLocalitate(l4);
- //afisareLocalitate(l5);
- //afisareLocalitate(l6);
- //afisareLocalitate(l7);
- Nod* arbore = NULL;
- arbore=inserareArbore(arbore, l1);
- arbore = inserareArbore(arbore, l2);
- arbore = inserareArbore(arbore, l3);
- arbore = inserareArbore(arbore, l4);
- arbore = inserareArbore(arbore, l5);
- arbore = inserareArbore(arbore, l6);
- arbore = inserareArbore(arbore, l7);
- cout << "\n==============AFISARE ARBORE PREORDINE================\n\n";
- afiarePreordine(arbore);
- cout << "\n==============AFISARE ARBORE PREORDINE================\n\n";
- afiareInordine(arbore);
- cout << "\n==============AFISARE ARBORE PREORDINE================\n\n";
- afiarePostordine(arbore);
- int inaltime =inaltimeArbore(arbore);
- printf("\nArborele nostru are %d nivele.", inaltime);
- afisareNivelArbore(arbore, 3, 0);
- if (cautaNodArbore(arbore, 20))
- {
- printf("\nNodul exista!\n");
- }
- else
- {
- printf("\nNodul nu exista!\n");
- }
- stergereArbore(&arbore);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement