It is currently Tue Feb 09, 2010 1:34 pm




Post new topic Reply to topic  [ 7 posts ] 
[Algoritmica] Masiv de date generalizat 
Author Message
Braincode Team Leader
User avatar

Joined: Mon Sep 10, 2007 11:40 pm
Posts: 2720
Location: Galati
Post [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.

IMPLEMENTARE

gendata.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;
}


EXEMPLU
Pentru 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.


Sun Feb 01, 2009 12:30 am
Profile
Braincode Programmer
User avatar

Joined: Wed Aug 01, 2007 5:40 pm
Posts: 1283
Location: Botosani
Post Re: [Algoritmica] Masiv de date generalizat
frumos Alex,asta a fost cu dedicatie pentru mine nu?:))


Mon Feb 02, 2009 8:41 pm
Profile
Braincode Team Leader
User avatar

Joined: Mon Sep 10, 2007 11:40 pm
Posts: 2720
Location: Galati
Post Re: [Algoritmica] Masiv de date generalizat
Intr-adevar cu dedicatie, dar nu pentru tine (desi stiu de ce a-i fi inclinat sa crezi asa ceva :rolfz: ). E o poveste mai lunga. :)


Mon Feb 02, 2009 8:57 pm
Profile
Grand Member

Joined: Sun Aug 12, 2007 4:54 pm
Posts: 329
Post Re: [Algoritmica] Masiv de date generalizat
in mod dubios, in destructor nu distrugi tot arborele. :P


Fri Feb 06, 2009 11:36 am
Profile
Braincode Team Leader
User avatar

Joined: Mon Sep 10, 2007 11:40 pm
Posts: 2720
Location: Galati
Post Re: [Algoritmica] Masiv de date generalizat
M-am gandit sa il distrug pe tot, dar stii tu cum e... :D Daca tot s-a observat asta, o sa trag de mine si o sa editez postul. Promit! :D


Fri Feb 06, 2009 12:58 pm
Profile
Grand Member

Joined: Sun Aug 12, 2007 4:54 pm
Posts: 329
Post Re: [Algoritmica] Masiv de date generalizat
haha, nu stii cat de carcotas sunt eu? :P puteai sa lasi si un byte nesters si tot il gaseam :D. glumesc, dar am vazut destructorul si ma gandeam cat de dubios trebuie sa fie sa eliberezi spatiul dupa modul in care ai construit arborele... cand am inceput sa citesc am vazut surpriza >:P dranaxum sterge radacina si inca 2-3 chestii si gata. :)


Fri Feb 06, 2009 1:10 pm
Profile
Braincode Team Leader
User avatar

Joined: Mon Sep 10, 2007 11:40 pm
Posts: 2720
Location: Galati
Post Re: [Algoritmica] Masiv de date generalizat
Pai lumea ar muri daca nu ar fi carcotasi :)).
Anyway thanks, post edited.


Fri Feb 06, 2009 1:15 pm
Profile
 
Post new topic Reply to topic  [ 7 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © phpBB Group.
Designed by boogiesbc and Vjacheslav Trushkin