
[Algoritmica] Masiv de date generalizat
Fie vectorul a de tip data T, sa se declare a astfel incat acesta sa aibe n dimensiuni oricare n numar natural (citit sau nu).In acest sens ne vom gandi la o structura arborescenta, unde fiecare nod a[x1][x2]…[xk] va avea x(k+1) subnoduri (copii). Radacina arborelui va avea x1 copii.
Numarul de elemente din masiv va avea: x1*x2*…*xk*….*xn elemente.
Ne vom folosi de urmatoarele variabile si tipuri de data inainte de a crea algoritmul:
Code:
struct nod
{
T v;
nod **child;
};
nod *l,*last;
Pentru a crea un astfel de algoritm in primul rand va trebui sa determinam modul de generare al arborelui. Vom crea o functie recursiva care sa faca acest lucru. l reprezinta radacina arborelui. De aici vom porni.
Code:
void back(int i,nod *u)
{
if(i<n){
u->child=new nod*[dim[i]];//alocam dim[i] copii nodului tata u.
for(int j=0;j<dim[i];j++)
{
u->child[j]=new nod;//alocam spatiu pentru fiecare copil al lui u
back(i+1,u->child[j]);//apel recursiv
}
}
}
Apelam functia astfel: back(0,l).
Pentru a atribui valori si pentru a prelua valori ne vom folosi de doua functii pushElement si getElement ambele avand unul din parametrii pozitia din masiv.
IMPLEMENTAREgendata.h
Code:
/*
Name:
GeneralData Class
Description:
GeneralData is a class with the help of which you can declare arrays of various
dimensions dynamically (given an array of the size of each dimension and the number
of the dimensions).
Author:
Paunoiu Alexandru (dranaxum)
*/
#include<string>
using namespace std;
template <class T>
class GeneralData
{
private:
int *dim;//the dimensions array
int len;//the number of dimensions
struct nod//the main structure for the tree
{
T v;
nod **l;
};
nod *l,*last;
void back(int i,nod *u);
void destroy(int i,nod *u);
public:
GeneralData(int *dim_v,int len_v);
~GeneralData();
T getElement(int *dims);
void pushElement(int *dims, T v);
};
/*
Description: it initializes the nodes within the tree
back:
@param1: i represents the current position in the dimensions array
@param2: u represents the current node in the tree
*/
template<class T>
void GeneralData<T>::back(int i,nod *u)
{
if(i<len){
u->l=new nod*[dim[i]];
for(int j=0;j<dim[i];j++)
{
u->l[j]=new nod;
back(i+1,u->l[j]);
}
}
}
/*
Description: it destroys the whole tree. (used by the destructor)
destroy:
@param1: i represents the current position in the dimensions array
@param2: u represents the current node in the tree
*/
template<class T>
void GeneralData<T>::destroy(int i,nod *u)
{
if(i<len){
for(int j=0;j<dim[i];j++)
{
back(i+1,u->l[j]);
delete u->l[j];
}
}
}
/*
Description: the constructor for the GeneralData class. Contains the main initialization phases
GeneralData
@param1: dim_v represents the inputed dimensions array
@param2: len_v represents the inputed size of dim_v
*/
template<class T>
GeneralData<T>::GeneralData(int *dim_v,int len_v)
{
dim=new int[len_v];
memcpy(dim,dim_v,sizeof(dim_v)+1);
len=len_v;
l=new nod;
last=l;
back(0,last);
}
/*
Description: the destructor for the GeneralData class. It frees the memory
*/
template<class T>
GeneralData<T>::~GeneralData()
{
destroy(0,l);
delete dim;
delete l;
delete last;
}
/*
Description: it gets the element at the inputed position
GeneralData
@param1: dims represents the array which contains the positions in the tree
*/
template<class T>
T GeneralData<T>::getElement(int *dims)
{
last=l;
nod *k=last->l[dims[0]-1];
int i;
for(i=0;i<len-1;i++)
k=k->l[dims[i+1]-1];
return k->v;
}
/*
Description: attributes a value to an inputed element
GeneralData
@param1: dims represents the array which contains the positions in the tree
@param2: v represents the value which will be attributed
*/
template<class T>
void GeneralData<T>::pushElement(int *dims, T v)
{
last=l;
nod *k=last->l[dims[0]-1];
int i;
for(i=0;i<len-1;i++)
k=k->l[dims[i+1]-1];
k->v=v;
}
EXEMPLUPentru exemplu vom folosi declararea unei matrice folosind GeneralData cu elemente de tip int. Vom atribui si afisa valorile!
Code:
#include<conio.h>
#include<iostream>
#include "gendata.h"
using namespace std;
int main()
{
int dim[]={2,2};
GeneralData<int> *p=new GeneralData<int>(dim,2);
int i,j;
for(i=1;i<=dim[0];i++)
{
for(j=1;j<=dim[1];j++)
{
int poz[]={i,j};
p->pushElement(poz,i+j);//atribuim la pozitia (i,j) in matrice valoarea i+j
}
}
for(i=1;i<=dim[0];i++)
{
for(j=1;j<=dim[1];j++)
{
int poz[]={i,j};
cout<<p->getElement(poz)<<" ";//afisam valoarea de la pozitia (i,j)
}
cout<<"\n";
}
getch();
return 0;
}
Cod compilat cu Dev C++ 4.9.9.2.