avl-cvs Mailing List for avl (Page 4)
Brought to you by:
hetfield666,
jah2003
You can subscribe to this list here.
2004 |
Jan
|
Feb
|
Mar
|
Apr
|
May
(2) |
Jun
(15) |
Jul
(16) |
Aug
(5) |
Sep
(65) |
Oct
|
Nov
|
Dec
|
---|
From: Patrizio B. <het...@us...> - 2004-07-18 10:40:23
|
Update of /cvsroot/avl/avl/docs In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26381 Added Files: relazione.tex Log Message: inizio relazione --- NEW FILE: relazione.tex --- \documentclass[12pt]{report} \usepackage{enumerate} \usepackage[latin1]{inputenc} %per usare direttamente da tastiera le vocali %accentate \usepackage[italian]{babel} \usepackage{fancyhdr} \usepackage{graphicx} \usepackage{epsfig} \usepackage{setspace} \pagestyle{fancy} \title{\Huge\textbf{AVL \& BST Trees}\\\ \\\textbf{\LARGE{Relazione}}\\~\\} \author{Patrizio Bassi, Gianlorenzo D'Angelo} \begin{document} \maketitle \tableofcontents \onehalfspacing \chapter{Introduzione}\label{Introduzione} L'evoluzione tecnologica degli ultimi anni sia stata semplicemente travolgente, con una crescita sempre più accentuata della potenza di calcolo a disposizione, non solo di grandi aziende e enti governativi, ma anche per i cosiddetti "end-users", gli utenti domestici. Macchine assemblate con processori con frequenze superiori ai 3 GHz, quantità di RAM superiori al GB e spazio su HardDisk nell'ordine di centinaia di GB sono disponibili a prezzi decisamente accessibili. Tuttavia, è noto che l'hardware a sè stante è inutile. E' (e sarà) sempre necessario del software per comandare tutte le varie periferiche in questione; questa cosa fa capire quanto una macchian di per sè velocissima possa essere inutilizabile se il software che la comanda contiene errori o è lento. Per questo motivo la ricerca nel campo degli algoritmi non è affatto terminata. Negli anni passati trovare un algoritmo ottimo era assolutamente necessario, dato che le risorse di calcolo a disposizione erano limitate, ma ancora oggi esistono tanti problemi che non possono essere affrontati con il semplice approccio "forza bruta", perchè impiegherebbero anche centinaia di anni ad essere risolti. Davvero fervente è stata la ricerca su grafi ed alberi, poichè sono strutture dati che possono plasmare situazioni problematiche che sembrano assai differenti tra loro, risolvendo il problema con l'utilizzo di algoritmi già noti, di cui è nota anche l'efficenza, le prestazioni, gli eventuali problemi. In questo ambito è collocato questo lavoro: lo studio e implementazione degli alberi AVL e BST, per individuare quali sono i vantaggi e svantaggi di entrambi gli algoritmi. Nel prossimo capitolo verrà illustrato in generale in funzionamento e l'idea alla base delle due tecniche, poi verrà esaminata in generale l'implementazione proposta e le conclusioni che si possono trarre dopo aver sfruttato il codice prodotto. \chapter{Breve Panoramica} La premessa di questo capitolo è quella di non essere assolutamente un riferimento esaustivo sugli alberi AVL e BST. Il contesto in cui si colloca questo lavoro è quello dei Dizionari. In ogni problema, di qualsiasi tipo esso sia, si ha a che fare con una certa tipologia e quantità di dati; generalmente si può modellare un problema utilizzando un tipo di dato generico, non ben specificato. Questo è il caso del Dizionario: un tipo di dato astratto (ADT) che rappresenti un insieme di tipi di oggetti al quale vengono applicate operazioni di inserimento, cancellazione, ricerca. Sarebbe inutile infatti conoscere degli oggetti senza avere possibilità di manipolarli o gestirli. La gestione di un dizionario può chiaramente essere effettuata in una infinità di soluzioni, come array (e qui si potrebbe parlare della disposizione degli oggetti nel contenitore array stesso), liste (con tutti i vari tipi di concatenazione) oppure alberi. Quest'ultima soluzione offre numerose alternative, tra queste ci sono proprio i BST, Binary Search Tree, e gli AVL trees, anagramma derivato dal nome dei loro ideatori. In generale gli alberi superano alcune limitazioni delle altre soluzioni, ad esempio non soffrono di limiti di spazio fisso. Generalmente ogni tipo di implementazione ha una doppia faccia, nel senso che risulta ottima e veloce in una certa operazione o in una certa sequenza di dati, mentre ha performance inferiori in altri contesti. In questo lavoro ci si concentrerà sull'operazione di ricerca, restringendo l'area agli alberi binari, ovvero quelli i cui nodi hanno al massimo 2 figli. L'implementazione BST è basata su questi alberi binari, con in più le condizioni: \begin{itemize} \item Ogni nodo v ha associata una chiave, un numero \item Le chiavi nel sotto albero sinistro di v sono tutte < o al più = della chiave di v \item Le chiavi nel sotto albero destro di v sono tutte > o al più = della chiave di v \end{itemize} In poche parole in fase di inserimento di un nodo, si controlla il valore della chiave e si appende il nuovo nodo al giusto sottoalbero. In fase di ricerca questa disposizione permette di scartare interi sottoalberi, un sistema molto simile alla ricerca binaria su liste ordinate, il cui costo è log(n). Il problema fondamentale di questi alberi BST è il bilanciamento. Nel caso di una sequenza di nodi, la cui chiave è sempre maggiore del nodo precedente, l'albero diventa semplicemente una lista concatenata, i cui nodi hanno tutti un unico figlio. In questo modo, se l'albero è composto da n elementi, la ricerca nel caso peggiore costerà O(n), quindi un costo lineare, che in alcune circostanze può essere ritenuto troppo elevato. E' sorto quindi il problema di tenere l'albero bilanciato in modo di avere la ricerca più efficente possibile. Chiaramente, come già detto in precedenza, avere un costo minore nella ricerca costringerà a performance infariori in altre situazioni. Il concetto di bilanciamento può essere riassunto nel mantenere un albero i cui rami hanno tutti la stessa lunghezza (con starti di +/-1), in modo da pagare un costo logaritmico per la ricerca. Va considerato che questo costo è particolarmente importante, poichè è evidente che anche le operazioni di cancellazione e inserimento contengono una ricerca priliminare (per trovare dove inserire e per trovare il nodo da cancellare). \chapter{Applicazione}\label{Applicazione} \chapter{Conclusioni}\label{Conclusioni} \end{document} |
From: Patrizio B. <het...@us...> - 2004-07-17 19:48:37
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15811 Modified Files: avl.cpp bst.cpp test.cpp test.h Log Message: very nice output with colors Index: test.h =================================================================== RCS file: /cvsroot/avl/avl/src/test.h,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** test.h 17 Jul 2004 17:05:54 -0000 1.6 --- test.h 17 Jul 2004 19:48:28 -0000 1.7 *************** *** 31,34 **** --- 31,35 ---- double init_timer; double change_timer; + int tree_dimensions; }; Index: bst.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/bst.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** bst.cpp 17 Jul 2004 17:05:54 -0000 1.6 --- bst.cpp 17 Jul 2004 19:48:27 -0000 1.7 *************** *** 19,22 **** --- 19,23 ---- ***************************************************************************/ #include "bst.h" + //#include <string.h> Bst::Bst() *************** *** 326,337 **** } void Bst::print1() { ! printf("\n\n%d\t",value); ! if( getLeft() ) ! printf("sinistro-->%d\t",getLeft()->getValue()); ! if( getRight() ) ! printf("destro-->%d\t",getRight()->getValue()); if( getLeft() ) getLeft()->print1(); --- 327,396 ---- } + int c_print(char *string){ + + // for colors and style. + #define MAGENTA 35 + #define GREEN 32 + #define BLUE 34 + #define GRAY 0 + #define YELLOW 33 + #define NORMAL 0 + + int color=GRAY; + int format= NORMAL; + + for(;*string;string++){ + if(*string=='C'){ //for color selection + string++; + if(*string=='m'){ //magenta + color=MAGENTA; + printf("[%i;%i;40m",format,color); + } + else if(*string=='g'){ //green + color=GREEN; + printf("[%i;%i;40m",format,color); + } + else if(*string=='b'){//BLUE + color=BLUE; + printf("[%i;%i;40m",format,color); + } + else if(*string=='y'){//BLUE + color=YELLOW; + printf("[%i;%i;40m",format,color); + } + + + } + else printf("%c",*string); + } + printf("[%i;%i;40m",NORMAL,GRAY); + } void Bst::print1() { ! char buffer[50]; ! ! printf("\n"); ! sprintf(buffer,"Cy%d",value); ! c_print(buffer); ! ! if ( getLeft() ) { ! printf("\t"); ! sprintf(buffer,"Cgleft value-->%d",getLeft()->getValue()); ! c_print(buffer); ! printf("\n"); ! } ! if( getRight() ) { ! printf("\t"); ! sprintf(buffer,"Cmright value-->%d",getRight()->getValue()); ! c_print(buffer); ! printf("\n"); ! } ! if (isLeaf()) { ! printf("\t"); ! sprintf(buffer,"Cbthis is a leaf"); ! c_print(buffer); ! printf("\n"); ! } if( getLeft() ) getLeft()->print1(); Index: test.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/test.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** test.cpp 17 Jul 2004 17:05:54 -0000 1.4 --- test.cpp 17 Jul 2004 19:48:28 -0000 1.5 *************** *** 33,37 **** start = clock(); for (int i=0;i<base_limit;i++) { ! random=rand()%10000; avlt=avlt->insert(random); // insertion_list.push_back(random); --- 33,37 ---- start = clock(); for (int i=0;i<base_limit;i++) { ! random=rand()%tree_dimensions; avlt=avlt->insert(random); // insertion_list.push_back(random); *************** *** 46,50 **** start = clock(); for (int i=0;i<base_limit;i++) { ! random=rand()%10000; bst->insert(random); // insertion_list.push_back(random); --- 46,50 ---- start = clock(); for (int i=0;i<base_limit;i++) { ! random=rand()%tree_dimensions; bst->insert(random); // insertion_list.push_back(random); *************** *** 75,79 **** start=clock(); for (int i=0;i<upper_limit;i++) { ! random=rand()%10000; decision=rand()%2; if (decision==0) { --- 75,79 ---- start=clock(); for (int i=0;i<upper_limit;i++) { ! random=rand()%tree_dimensions; decision=rand()%2; if (decision==0) { *************** *** 128,132 **** start=clock(); for (int i=0;i<upper_limit;i++) { ! random=rand()%10000; decision=rand()%2; if (decision==0) { --- 128,132 ---- start=clock(); for (int i=0;i<upper_limit;i++) { ! random=rand()%tree_dimensions; decision=rand()%2; if (decision==0) { *************** *** 152,156 **** } } else waste++; - } end=clock(); --- 152,155 ---- *************** *** 165,169 **** void Test::comparation(int base_tree_size, int upperbound ) { ! double temp_timer; bst=new Bst(); --- 164,169 ---- void Test::comparation(int base_tree_size, int upperbound ) { ! double temp_timer,search_timer; ! clock_t start, end; bst=new Bst(); *************** *** 172,176 **** Test_Init(base_tree_size,1); //bst temp_timer=getInitTimer(); ! sleep(1); Test_Init(base_tree_size,0); //avl printf("\nInit Result:\nBST -> %lf, AVL -> %lf\n",temp_timer,getInitTimer()); --- 172,176 ---- Test_Init(base_tree_size,1); //bst temp_timer=getInitTimer(); ! sleep(5); Test_Init(base_tree_size,0); //avl printf("\nInit Result:\nBST -> %lf, AVL -> %lf\n",temp_timer,getInitTimer()); *************** *** 180,192 **** Bst_Test(upperbound); temp_timer=getChangeTimer(); ! sleep(1); Avl_Test(upperbound); if ((temp_timer-getInitTimer())>double(0)) printf("***************************\nAVL faster\n***************************\n"); if ((temp_timer-getInitTimer())<double(0)) printf("***************************\nBST faster\n***************************\n"); if ((temp_timer-getInitTimer())==double(0)) printf("***************************\nNo real differences found\n***************************\n"); } Test::Test(int kind, int upperbound, int base_tree_size) { switch (kind){ case 0: --- 180,211 ---- Bst_Test(upperbound); temp_timer=getChangeTimer(); ! sleep(5); Avl_Test(upperbound); if ((temp_timer-getInitTimer())>double(0)) printf("***************************\nAVL faster\n***************************\n"); if ((temp_timer-getInitTimer())<double(0)) printf("***************************\nBST faster\n***************************\n"); if ((temp_timer-getInitTimer())==double(0)) printf("***************************\nNo real differences found\n***************************\n"); + + int max=bst->max()->getValue(); //caching! + + start=clock(); + bst->search(max); + end=clock(); + search_timer=((double) (end - start)) / CLOCKS_PER_SEC; + + max=avlt->max()->getValue(); //caching! + start=clock(); + avlt->search(max); + end=clock(); + temp_timer=((double) (end - start)) / CLOCKS_PER_SEC; + printf("Bst search time of its max value (%d):%lf\nBst search time of its max value (%d):%lf\n",bst->max()->getValue(),search_timer,max,temp_timer); + if ((temp_timer-search_timer)<double(0)) printf("***************************\nAVL faster\n***************************\n"); + if ((temp_timer-search_timer)>double(0)) printf("***************************\nBST faster\n***************************\n"); + if ((temp_timer-search_timer)==double(0)) printf("***************************\nNo real differences found\n***************************\n"); + } Test::Test(int kind, int upperbound, int base_tree_size) { + tree_dimensions=base_tree_size; switch (kind){ case 0: Index: avl.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/avl.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** avl.cpp 17 Jul 2004 17:05:54 -0000 1.6 --- avl.cpp 17 Jul 2004 19:48:27 -0000 1.7 *************** *** 123,127 **** // printf("%d",b->getValue()); #endif ! // 0= test avl // 1= test bst --- 123,127 ---- // printf("%d",b->getValue()); #endif ! /* // 0= test avl // 1= test bst *************** *** 148,155 **** printf("Comparation on the way, wait\n"); prova_bst = new Test(2,upper_bound,base_tree); ! } //prova->getBst()->print1(); //not a good thing...for testing purpose only //prova->getAvlt()->print1(); return EXIT_SUCCESS; } --- 148,179 ---- printf("Comparation on the way, wait\n"); prova_bst = new Test(2,upper_bound,base_tree); ! }*/ ! //Test *prova; ! //prova = new Test(1,5,1); //prova->getBst()->print1(); //not a good thing...for testing purpose only //prova->getAvlt()->print1(); + + Bst a(1 ); + + a.insert(3); + a.insert(5); + a.insert(4); + a.insert(2); + a.insert(10); + a.insert(-10); + a.insert(-1); + a.insert(20); + a.insert(-20); + a.insert(-25); + a.insert(-15); + a.insert(-6); + a.insert(-200); + a.insert(-300); + a.insert(-400); + a.insert(-500); + a.insert(-600); + a.insert(-700); + a.print1(); return EXIT_SUCCESS; } |
From: Patrizio B. <het...@us...> - 2004-07-17 17:06:19
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26597/src Modified Files: avl.cpp bst.cpp test.cpp test.h Log Message: Test class completed Index: test.h =================================================================== RCS file: /cvsroot/avl/avl/src/test.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** test.h 17 Jul 2004 14:12:59 -0000 1.5 --- test.h 17 Jul 2004 17:05:54 -0000 1.6 *************** *** 5,10 **** #include "bst.h" ! //check the time! will be changed soon ! #define mytime int using namespace std; --- 5,10 ---- #include "bst.h" ! #include <time.h> ! #include <list> using namespace std; *************** *** 16,21 **** Test(); ~Test(); ! Test(int,int,int=150); ! mytime getTimer(); Avlt* getAvlt(); Bst* getBst(); --- 16,20 ---- Test(); ~Test(); ! Test(int,int=1000,int=1500); Avlt* getAvlt(); Bst* getBst(); *************** *** 24,30 **** void Avl_Test(int); void Bst_Test(int); ! void Test_Init(int); Avlt* avlt; Bst* bst; }; --- 23,34 ---- void Avl_Test(int); void Bst_Test(int); ! void comparation(int,int); ! void Test_Init(int,int); ! double getInitTimer(); ! double getChangeTimer(); Avlt* avlt; Bst* bst; + double init_timer; + double change_timer; }; Index: bst.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/bst.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** bst.cpp 15 Jul 2004 12:47:21 -0000 1.5 --- bst.cpp 17 Jul 2004 17:05:54 -0000 1.6 *************** *** 167,171 **** if ( key == aux1->getValue() ) { ! printf("The number -> %d is already in the Binary Search Tree\n",key); return; } --- 167,171 ---- if ( key == aux1->getValue() ) { ! //printf("The number -> %d is already in the Binary Search Tree\n",key); return; } Index: test.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/test.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** test.cpp 17 Jul 2004 14:12:59 -0000 1.3 --- test.cpp 17 Jul 2004 17:05:54 -0000 1.4 *************** *** 1,4 **** #include "test.h" - #include <list> Test::~Test() { --- 1,3 ---- *************** *** 6,14 **** if (avlt) delete avlt; if (bst) delete bst; } Test::Test() { ! Test(0,60,150); } --- 5,14 ---- if (avlt) delete avlt; if (bst) delete bst; + } Test::Test() { ! Test(0,1000,1500); } *************** *** 23,40 **** } ! void Test::Test_Init(int base_limit) { int random; ! srand ( time(NULL) ); ! if (avlt) { ! for (int i=0;i<base_limit;i++) { ! random=rand()%1000; avlt=avlt->insert(random); } ! } ! } --- 23,59 ---- } ! void Test::Test_Init(int base_limit,int kind=0) { int random; ! clock_t start, end; ! srand ( time(NULL) ); ! ! if (avlt && kind==0) { ! start = clock(); for (int i=0;i<base_limit;i++) { ! random=rand()%10000; avlt=avlt->insert(random); + // insertion_list.push_back(random); } ! end = clock(); ! init_timer = ((double) (end - start)) / CLOCKS_PER_SEC; ! printf("AVL Initialization of %d items tree took %lf seconds\n",base_limit,init_timer); ! // getchar(); } ! ! if (bst && kind==1) { ! start = clock(); ! for (int i=0;i<base_limit;i++) { ! random=rand()%10000; ! bst->insert(random); ! // insertion_list.push_back(random); ! } ! end = clock(); ! init_timer = ((double) (end - start)) / CLOCKS_PER_SEC; ! printf("BST Initialization of %d items tree took %lf seconds\n",base_limit,init_timer); ! // getchar(); ! } ! } *************** *** 45,61 **** int decision; int random_erase; ! list<int> insertion_list; list<int>::iterator IT; int x; srand ( time(NULL) ); if (avlt) { ! for (int i=0;i<upper_limit;i++) { ! random=rand()%1000; decision=rand()%2; if (decision==0) { avlt=avlt->insert(random); insertion_list.push_back(random); ! printf("inserted number %d\n",random); } else if (!insertion_list.empty()){ --- 64,85 ---- int decision; int random_erase; ! int erase_counter=0,insert_counter=0; list<int>::iterator IT; + list<int> insertion_list; int x; + clock_t start, end; + int waste=0; srand ( time(NULL) ); + if (avlt) { ! start=clock(); for (int i=0;i<upper_limit;i++) { ! random=rand()%10000; decision=rand()%2; if (decision==0) { avlt=avlt->insert(random); insertion_list.push_back(random); ! //printf("inserted number %d\n",random); ! insert_counter++; } else if (!insertion_list.empty()){ *************** *** 68,80 **** if (x==random_erase) { //printf("x:%d value=%d\n",x,*IT); ! printf("erased number %d\n",*IT); avlt=avlt->erase(*IT); if (IT!=insertion_list.end()) insertion_list.erase(IT); } } ! } } ! } --- 92,110 ---- if (x==random_erase) { //printf("x:%d value=%d\n",x,*IT); ! // printf("erased number %d\n",*IT); avlt=avlt->erase(*IT); if (IT!=insertion_list.end()) insertion_list.erase(IT); + erase_counter++; } } ! } ! else waste++; } ! end=clock(); ! change_timer = ((double) (end - start)) / CLOCKS_PER_SEC; ! printf("AVL TEST RESULTS:\n%d insertions and %d deletions took %lf seconds.\nTotal of %d cycles, wasted:%d\n",insert_counter,erase_counter,change_timer,upper_limit,waste); ! printf("press Return to exit\n"); ! //getchar(); } *************** *** 83,102 **** void Test::Bst_Test(int upper_limit) { } ! Test::Test(int kind, int upperbound, int base_tree_size=150) { switch (kind){ case 0: avlt=new Avlt(); ! Test_Init(base_tree_size); Avl_Test(upperbound); break; case 1: bst=new Bst(); ! //Test_Init(); not ready for both Bst_Test(upperbound); break; default: printf("Invalid test selection, application will abort\n"); --- 113,206 ---- void Test::Bst_Test(int upper_limit) { + int random; + int decision; + int random_erase; + int erase_counter=0,insert_counter=0; + list<int>::iterator IT; + list<int> insertion_list; + int x; + clock_t start, end; + int waste=0; + + srand ( time(NULL) ); + + if (bst) { + start=clock(); + for (int i=0;i<upper_limit;i++) { + random=rand()%10000; + decision=rand()%2; + if (decision==0) { + bst->insert(random); + insertion_list.push_back(random); + //printf("inserted number %d\n",random); + insert_counter++; + } + else if (!insertion_list.empty()){ + + random_erase=rand()%insertion_list.size(); + + for ( IT=insertion_list.begin(), x=0; x<=random_erase && IT!=insertion_list.end(); + IT++, x++) + { + if (x==random_erase) { + //printf("x:%d value=%d\n",x,*IT); + // printf("erased number %d\n",*IT); + bst->erase(*IT); + if (IT!=insertion_list.end()) insertion_list.erase(IT); + erase_counter++; + } + } + } else waste++; + + } + end=clock(); + change_timer = ((double) (end - start)) / CLOCKS_PER_SEC; + printf("BST TEST RESULTS:\n%d insertions and %d deletions took %lf seconds.\nTotal of %d cycles, wasted %d\n",insert_counter,erase_counter,change_timer,upper_limit,waste); + printf("press Return to exit\n"); + //getchar(); + } + } + void Test::comparation(int base_tree_size, int upperbound ) { ! double temp_timer; ! ! bst=new Bst(); ! avlt=new Avlt(); ! ! Test_Init(base_tree_size,1); //bst ! temp_timer=getInitTimer(); ! sleep(1); ! Test_Init(base_tree_size,0); //avl ! printf("\nInit Result:\nBST -> %lf, AVL -> %lf\n",temp_timer,getInitTimer()); ! if ((temp_timer-getInitTimer())>double(0)) printf("***************************\nAVL faster\n***************************\n"); ! if ((temp_timer-getInitTimer())<double(0)) printf("***************************\nBST faster\n***************************\n"); ! if ((temp_timer-getInitTimer())==double(0)) printf("***************************\nNo real differences found\n***************************\n"); ! Bst_Test(upperbound); ! temp_timer=getChangeTimer(); ! sleep(1); ! Avl_Test(upperbound); ! if ((temp_timer-getInitTimer())>double(0)) printf("***************************\nAVL faster\n***************************\n"); ! if ((temp_timer-getInitTimer())<double(0)) printf("***************************\nBST faster\n***************************\n"); ! if ((temp_timer-getInitTimer())==double(0)) printf("***************************\nNo real differences found\n***************************\n"); ! } ! ! Test::Test(int kind, int upperbound, int base_tree_size) { switch (kind){ case 0: avlt=new Avlt(); ! Test_Init(base_tree_size,0); Avl_Test(upperbound); break; case 1: bst=new Bst(); ! Test_Init(base_tree_size,1); Bst_Test(upperbound); break; + case 2: + comparation(base_tree_size,upperbound); + break; default: printf("Invalid test selection, application will abort\n"); *************** *** 105,110 **** } ! mytime Test::getTimer() { } --- 209,220 ---- } ! double Test::getInitTimer() { + return init_timer; + } + + double Test::getChangeTimer() { + + return change_timer; } Index: avl.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/avl.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** avl.cpp 17 Jul 2004 12:34:54 -0000 1.5 --- avl.cpp 17 Jul 2004 17:05:54 -0000 1.6 *************** *** 126,137 **** // 0= test avl // 1= test bst ! ! Test *prova; ! int upper_bound=100; ! ! prova = new Test(0,upper_bound); ! //not a good thing...for testing purpose only ! prova->getAvlt()->print1(); return EXIT_SUCCESS; } --- 126,155 ---- // 0= test avl // 1= test bst ! Test *prova_avl, *prova_bst; ! int upper_bound=1000; ! int base_tree=10000; ! printf("Welcome to AVL/BST test program\nYou will be asked for values, no controls on your imput, so be careful!\n"); ! printf("What do you want to test? AVL=0 BST=1 Comparation=2\n"); ! int choice; ! scanf("%d",&choice); ! printf("Insert the initial number of items\n"); ! scanf("%d",&base_tree); ! printf("Insert the number of changements you want to do on the tree\n"); ! scanf("%d",&upper_bound); ! if (choice==0) { ! printf("Processing AVL, wait\n"); ! prova_avl = new Test(0,upper_bound,base_tree); ! } ! if (choice==1) { ! printf("Processing BST, wait\n"); ! prova_bst = new Test(1,upper_bound,base_tree); ! } ! if (choice==2) { ! printf("Comparation on the way, wait\n"); ! prova_bst = new Test(2,upper_bound,base_tree); ! } ! //prova->getBst()->print1(); //not a good thing...for testing purpose only ! //prova->getAvlt()->print1(); return EXIT_SUCCESS; } |
From: Patrizio B. <het...@us...> - 2004-07-17 17:06:19
|
Update of /cvsroot/avl/avl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26597 Modified Files: ChangeLog TODO Log Message: Test class completed Index: TODO =================================================================== RCS file: /cvsroot/avl/avl/TODO,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** TODO 24 Jun 2004 17:11:08 -0000 1.1 --- TODO 17 Jul 2004 17:05:54 -0000 1.2 *************** *** 1,6 **** -Fare funzione (o classe) che visualizzi decentemente un albero binario (anche qualsiasi non per forza bst o avl) ! -fare classe che stressi il sistemi ovvero che faccia casualmente operazioni di inserimento e cancellazione. In particolare: ! -un metodo che prima inserisce un certo numero casuale di dati, poi casualmente fa un certo numero di inserimenti e cancellazioni. ! -un metodo che fa richiama il metodo precedente n volte. ! -un metodo che restituisce una sorta di benchmark contando il tempo che ci mettono le altre ad essere eseguite (si possono utilizzare le funzioni a basso livello del C). --- 1,3 ---- -Fare funzione (o classe) che visualizzi decentemente un albero binario (anche qualsiasi non per forza bst o avl) ! - Controllare e fixare i punti in cui freeza. dovrebbe essere in bst. non è solo quando l'albero è vuoto. Index: ChangeLog =================================================================== RCS file: /cvsroot/avl/avl/ChangeLog,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ChangeLog 15 Jul 2004 12:47:21 -0000 1.4 --- ChangeLog 17 Jul 2004 17:05:54 -0000 1.5 *************** *** 1,2 **** --- 1,6 ---- + --==17.07.2004==-- + <hetfield666>: + - Test class completed, with bst, avl and comparation + - Added Timing support in Test class --==15.07.2004==-- <hetfield666>: |
From: Patrizio B. <het...@us...> - 2004-07-17 14:13:08
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3937 Modified Files: test.cpp test.h Log Message: initialization, no more freezes (workaround, bug must be fixed) Index: test.h =================================================================== RCS file: /cvsroot/avl/avl/src/test.h,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** test.h 17 Jul 2004 12:38:44 -0000 1.4 --- test.h 17 Jul 2004 14:12:59 -0000 1.5 *************** *** 16,20 **** Test(); ~Test(); ! Test(int,int); mytime getTimer(); Avlt* getAvlt(); --- 16,20 ---- Test(); ~Test(); ! Test(int,int,int=150); mytime getTimer(); Avlt* getAvlt(); *************** *** 24,27 **** --- 24,28 ---- void Avl_Test(int); void Bst_Test(int); + void Test_Init(int); Avlt* avlt; Bst* bst; Index: test.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/test.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** test.cpp 17 Jul 2004 12:34:55 -0000 1.2 --- test.cpp 17 Jul 2004 14:12:59 -0000 1.3 *************** *** 1,7 **** #include "test.h" #include <list> - #include <iostream> - Test::Test() { } --- 1,14 ---- #include "test.h" #include <list> + Test::~Test() { + + if (avlt) delete avlt; + if (bst) delete bst; + } + + + Test::Test() { + Test(0,60,150); } *************** *** 16,19 **** --- 23,43 ---- } + void Test::Test_Init(int base_limit) { + + int random; + + srand ( time(NULL) ); + if (avlt) { + + for (int i=0;i<base_limit;i++) { + random=rand()%1000; + avlt=avlt->insert(random); + } + + } + + } + + void Test::Avl_Test(int upper_limit){ *************** *** 38,45 **** random_erase=rand()%insertion_list.size(); ! //for ( list<int>::iterator IT=insertion_list.begin(), int x=0; x<random_erase; IT++, x++) ! //list<int>::iterator IT = find(insertion_list.begin(),insertion_list.end(),random_erase); ! //insertion_list.erase(IT); ! for ( IT=insertion_list.begin(), x=0; x<=random_erase && IT!=insertion_list.end(); IT++, x++) --- 62,66 ---- random_erase=rand()%insertion_list.size(); ! for ( IT=insertion_list.begin(), x=0; x<=random_erase && IT!=insertion_list.end(); IT++, x++) *************** *** 65,76 **** ! Test::Test(int kind, int upperbound) { switch (kind){ case 0: avlt=new Avlt(); Avl_Test(upperbound); break; case 1: bst=new Bst(); Bst_Test(upperbound); break; --- 86,100 ---- ! Test::Test(int kind, int upperbound, int base_tree_size=150) { ! switch (kind){ case 0: avlt=new Avlt(); + Test_Init(base_tree_size); Avl_Test(upperbound); break; case 1: bst=new Bst(); + //Test_Init(); not ready for both Bst_Test(upperbound); break; |
From: Patrizio B. <het...@us...> - 2004-07-17 12:38:52
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24489 Added Files: test.h Log Message: committing --- NEW FILE: test.h --- #ifndef TEST_H #define TEST_H #include "avlt.h" #include "bst.h" //check the time! will be changed soon #define mytime int using namespace std; class Test { public: Test(); ~Test(); Test(int,int); mytime getTimer(); Avlt* getAvlt(); Bst* getBst(); private: void Avl_Test(int); void Bst_Test(int); Avlt* avlt; Bst* bst; }; #endif |
From: Patrizio B. <het...@us...> - 2004-07-17 12:38:06
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24389 Removed Files: test.h Log Message: cvs bug found, must do this to fix --- test.h DELETED --- |
From: Patrizio B. <het...@us...> - 2004-07-17 12:35:04
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24093/src Modified Files: avl.cpp Makefile test.cpp Log Message: Index: avl.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/avl.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** avl.cpp 25 Jun 2004 16:05:56 -0000 1.4 --- avl.cpp 17 Jul 2004 12:34:54 -0000 1.5 *************** *** 23,27 **** #endif ! #include "avlt.h" #include <iostream> #include <cstdlib> --- 23,28 ---- #endif ! //#include "avlt.h" ! #include "test.h" #include <iostream> #include <cstdlib> *************** *** 31,34 **** --- 32,36 ---- int main(int argc, char *argv[]) { + #if 0 // Bst a(1 ), *b; *************** *** 60,64 **** // a.insert(-3); */ ! a = new Avlt(0,1); for (int i=0;i<567;i++) --- 62,67 ---- // a.insert(-3); */ ! ! Avlt *a = new Avlt(0,1); for (int i=0;i<567;i++) *************** *** 76,80 **** } } ! // a->search(-8)->print1(); /* printf("%d\n",a->nodes()); --- 79,84 ---- } } ! a->print(); ! // a->search(-8)->print1(); /* printf("%d\n",a->nodes()); *************** *** 118,121 **** // b = (a.getLeft())->getLeft()->getRight(); // printf("%d",b->getValue()); ! return EXIT_SUCCESS; } --- 122,137 ---- // b = (a.getLeft())->getLeft()->getRight(); // printf("%d",b->getValue()); ! #endif ! ! // 0= test avl ! // 1= test bst ! ! Test *prova; ! int upper_bound=100; ! ! prova = new Test(0,upper_bound); ! ! //not a good thing...for testing purpose only ! prova->getAvlt()->print1(); ! return EXIT_SUCCESS; } Index: Makefile =================================================================== RCS file: /cvsroot/avl/avl/src/Makefile,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Makefile 15 Jul 2004 12:47:21 -0000 1.3 --- Makefile 17 Jul 2004 12:34:55 -0000 1.4 *************** *** 1,5 **** # AVL Project CC=g++ -O2 ! LIBS=avlt.o bst.o test.o BINARY=avl --- 1,5 ---- # AVL Project CC=g++ -O2 ! LIBS=avlt.o bst.o test.o avl.o BINARY=avl *************** *** 8,19 **** test.o: test.cpp $(CC) -c test.cpp -o test.o avlt.o: avlt.cpp $(CC) -c avlt.cpp -o avlt.o bst.o: bst.cpp $(CC) -c bst.cpp -o bst.o ! $(BINARY): avl.cpp $(LIBS) ! $(CC) avl.cpp -o $(BINARY) $(LIBS) clean: --- 8,23 ---- test.o: test.cpp $(CC) -c test.cpp -o test.o + avlt.o: avlt.cpp $(CC) -c avlt.cpp -o avlt.o + avl.o: avl.cpp + $(CC) -c avl.cpp -o avl.o + bst.o: bst.cpp $(CC) -c bst.cpp -o bst.o ! $(BINARY): $(LIBS) ! $(CC) -o $(BINARY) $(LIBS) clean: Index: test.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/test.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** test.cpp 15 Jul 2004 12:47:21 -0000 1.1 --- test.cpp 17 Jul 2004 12:34:55 -0000 1.2 *************** *** 1,10 **** #include "test.h" ! Test::Test() { } ! Test::Test(int kind, int upperbound) { } --- 1,82 ---- #include "test.h" ! #include <list> ! #include <iostream> Test::Test() { } ! Avlt* Test::getAvlt() { ! ! return avlt; ! } ! ! Bst* Test::getBst() { ! ! return bst; ! } ! ! void Test::Avl_Test(int upper_limit){ ! ! int random; ! int decision; ! int random_erase; ! list<int> insertion_list; ! list<int>::iterator IT; ! int x; ! srand ( time(NULL) ); ! if (avlt) { ! ! for (int i=0;i<upper_limit;i++) { ! random=rand()%1000; ! decision=rand()%2; ! if (decision==0) { ! avlt=avlt->insert(random); ! insertion_list.push_back(random); ! printf("inserted number %d\n",random); ! } ! else if (!insertion_list.empty()){ ! ! random_erase=rand()%insertion_list.size(); ! //for ( list<int>::iterator IT=insertion_list.begin(), int x=0; x<random_erase; IT++, x++) ! //list<int>::iterator IT = find(insertion_list.begin(),insertion_list.end(),random_erase); ! //insertion_list.erase(IT); ! ! for ( IT=insertion_list.begin(), x=0; x<=random_erase && IT!=insertion_list.end(); ! IT++, x++) ! { ! if (x==random_erase) { ! //printf("x:%d value=%d\n",x,*IT); ! printf("erased number %d\n",*IT); ! avlt=avlt->erase(*IT); ! if (IT!=insertion_list.end()) insertion_list.erase(IT); ! } ! } ! } ! ! } ! ! } ! ! } + void Test::Bst_Test(int upper_limit) { + + } + + + Test::Test(int kind, int upperbound) { + switch (kind){ + case 0: + avlt=new Avlt(); + Avl_Test(upperbound); + break; + case 1: + bst=new Bst(); + Bst_Test(upperbound); + break; + default: + printf("Invalid test selection, application will abort\n"); + return; + } } |
From: Gianlorenzo D\\'A. <ja...@us...> - 2004-07-15 13:17:38
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30130 Modified Files: test.h Log Message: Index: test.h =================================================================== RCS file: /cvsroot/avl/avl/src/test.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** test.h 15 Jul 2004 12:47:21 -0000 1.1 --- test.h 15 Jul 2004 13:17:28 -0000 1.2 *************** *** 6,10 **** //check the time! ! #define mytime int using namespace std; --- 6,10 ---- //check the time! ! typedef int mytime; using namespace std; *************** *** 23,26 **** Bst* bst; ! } #endif --- 23,26 ---- Bst* bst; ! }; #endif |
From: Patrizio B. <het...@us...> - 2004-07-15 12:47:31
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25778/src Modified Files: bst.cpp Makefile Added Files: test.cpp test.h Log Message: first test class implementation, doesn't compile! --- NEW FILE: test.h --- #ifndef TEST_H #define TEST_H #include "avlt.h" #include "bst.h" //check the time! #define mytime int using namespace std; class Test { public: Test(); ~Test(); Test(int,int); mytime getTimer(); private: Avlt* avl; Bst* bst; } #endif Index: bst.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/bst.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** bst.cpp 25 Jun 2004 16:05:57 -0000 1.4 --- bst.cpp 15 Jul 2004 12:47:21 -0000 1.5 *************** *** 167,171 **** if ( key == aux1->getValue() ) { ! printf("The %d is already in the Binary Search Tree",key); return; } --- 167,171 ---- if ( key == aux1->getValue() ) { ! printf("The number -> %d is already in the Binary Search Tree\n",key); return; } Index: Makefile =================================================================== RCS file: /cvsroot/avl/avl/src/Makefile,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Makefile 24 Jun 2004 19:29:45 -0000 1.2 --- Makefile 15 Jul 2004 12:47:21 -0000 1.3 *************** *** 1,9 **** # AVL Project CC=g++ -O2 ! LIBS=avlt.o bst.o BINARY=avl all: $(LIBS) $(BINARY) avlt.o: avlt.cpp $(CC) -c avlt.cpp -o avlt.o --- 1,11 ---- # AVL Project CC=g++ -O2 ! LIBS=avlt.o bst.o test.o BINARY=avl all: $(LIBS) $(BINARY) + test.o: test.cpp + $(CC) -c test.cpp -o test.o avlt.o: avlt.cpp $(CC) -c avlt.cpp -o avlt.o --- NEW FILE: test.cpp --- #include "test.h" Test::Test() { } Test::Test(int kind, int upperbound) { } mytime Test::getTimer() { } |
From: Patrizio B. <het...@us...> - 2004-07-15 12:47:31
|
Update of /cvsroot/avl/avl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25778 Modified Files: ChangeLog Log Message: first test class implementation, doesn't compile! Index: ChangeLog =================================================================== RCS file: /cvsroot/avl/avl/ChangeLog,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ChangeLog 24 Jun 2004 17:03:22 -0000 1.3 --- ChangeLog 15 Jul 2004 12:47:21 -0000 1.4 *************** *** 1,2 **** --- 1,8 ---- + --==15.07.2004==-- + <hetfield666>: + - first Test class implementation + --==24.06.2004==-- + <hetfield666>: + - first Makefile implementation --==24.06.2004==-- <jah2003>: |
From: Gianlorenzo D\\'A. <ja...@us...> - 2004-06-25 16:06:18
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26265/src Modified Files: avl.cpp avlt.cpp avlt.h bst.cpp bst.h Log Message: Index: bst.h =================================================================== RCS file: /cvsroot/avl/avl/src/bst.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** bst.h 23 Jun 2004 14:25:05 -0000 1.2 --- bst.h 25 Jun 2004 16:05:57 -0000 1.3 *************** *** 64,68 **** Bst *search(int); //OK void insert(int); //OK ! void erase(int); //No nel caso di nodo con 2 figli in cui il succ è il figlio destro del nodo da eliminare Bst *max(); //OK Bst *min(); //OK --- 64,68 ---- Bst *search(int); //OK void insert(int); //OK ! void erase(int); //Non testato Bst *max(); //OK Bst *min(); //OK Index: bst.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/bst.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** bst.cpp 24 Jun 2004 19:29:45 -0000 1.3 --- bst.cpp 25 Jun 2004 16:05:57 -0000 1.4 *************** *** 196,208 **** aux1 = successor(); ! if( getLeft() ) { ! aux2 = getLeft(); ! setLeft( NULL ); } else { ! aux2 = getRight(); ! setRight( NULL ); } --- 196,208 ---- aux1 = successor(); ! if( aux1->getLeft() ) { ! aux2 = aux1->getLeft(); ! //setLeft( NULL ); } else { ! aux2 = aux1->getRight(); ! //setRight( NULL ); } *************** *** 210,216 **** aux2->setParent( aux1->getParent() ); ! if( !aux1->getParent() ) ! aux2 = root();//Il contrario ! else if( aux1 == aux1->getParent()->getLeft() ) aux1->getParent()->setLeft( aux2 ); --- 210,214 ---- aux2->setParent( aux1->getParent() ); ! if( aux1->getParent() ) if( aux1 == aux1->getParent()->getLeft() ) aux1->getParent()->setLeft( aux2 ); *************** *** 220,223 **** --- 218,222 ---- if( aux1 != this ) setValue( aux1->getValue() ); + delete aux1; //ATT!!!!!!! } Index: avl.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/avl.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** avl.cpp 24 Jun 2004 17:03:23 -0000 1.3 --- avl.cpp 25 Jun 2004 16:05:56 -0000 1.4 *************** *** 65,69 **** { int aaa=(rand()%1000)-500; ! a = a->insert_ric( aaa ); //if(i>=19) { --- 65,69 ---- { int aaa=(rand()%1000)-500; ! a = a->insert( aaa ); //if(i>=19) { *************** *** 78,82 **** // a->search(-8)->print1(); ! printf("%d\n",a->nodes()); a = a->erase(277); a = a->erase(3); --- 78,82 ---- // a->search(-8)->print1(); ! /* printf("%d\n",a->nodes()); a = a->erase(277); a = a->erase(3); *************** *** 90,94 **** printf("\n%d\n",a->nodes()); ! printf("\n%d",a->checkBalance()); //a->print1(); // a->search(3)->print1(); --- 90,94 ---- printf("\n%d\n",a->nodes()); ! printf("\n%d",a->checkBalance());*/ //a->print1(); // a->search(3)->print1(); Index: avlt.h =================================================================== RCS file: /cvsroot/avl/avl/src/avlt.h,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** avlt.h 24 Jun 2004 17:40:18 -0000 1.4 --- avlt.h 25 Jun 2004 16:05:57 -0000 1.5 *************** *** 51,60 **** vector<Avlt *> getWrongs(); Avlt *insert(int); - Avlt *insert_ric(int); Avlt *erase(int); - - }; --- 51,58 ---- vector<Avlt *> getWrongs(); + Avlt *insert_iter(int); Avlt *insert(int); Avlt *erase(int); }; Index: avlt.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/avlt.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** avlt.cpp 24 Jun 2004 17:03:23 -0000 1.3 --- avlt.cpp 25 Jun 2004 16:05:57 -0000 1.4 *************** *** 131,135 **** } ! Avlt *Avlt::insert_ric(int key) { Avlt *aux; --- 131,135 ---- } ! Avlt *Avlt::insert(int key) { Avlt *aux; *************** *** 141,145 **** if( getLeft() ) { ! ((Avlt *)getLeft())->insert_ric(key); setFdb(); if( getFdb() == 2 ) --- 141,145 ---- if( getLeft() ) { ! ((Avlt *)getLeft())->insert(key); setFdb(); if( getFdb() == 2 ) *************** *** 164,168 **** if( getRight() ) { ! ((Avlt *)getRight())->insert_ric(key); setFdb(); if( getFdb() == -2 ) --- 164,168 ---- if( getRight() ) { ! ((Avlt *)getRight())->insert(key); setFdb(); if( getFdb() == -2 ) *************** *** 186,190 **** } ! Avlt *Avlt::insert(int key) { /* --- 186,190 ---- } ! Avlt *Avlt::insert_iter(int key) { /* |
From: Patrizio B. <het...@us...> - 2004-06-24 19:29:54
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25741 Modified Files: Makefile bst.cpp Log Message: added support for shared library compilation Index: bst.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/bst.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** bst.cpp 23 Jun 2004 14:25:05 -0000 1.2 --- bst.cpp 24 Jun 2004 19:29:45 -0000 1.3 *************** *** 166,170 **** --- 166,173 ---- { if ( key == aux1->getValue() ) + { + printf("The %d is already in the Binary Search Tree",key); return; + } aux2 = aux1; if ( key < aux1->getValue() ) Index: Makefile =================================================================== RCS file: /cvsroot/avl/avl/src/Makefile,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Makefile 24 Jun 2004 17:40:18 -0000 1.1 --- Makefile 24 Jun 2004 19:29:45 -0000 1.2 *************** *** 4,16 **** BINARY=avl ! all: $(LIBS) ! $(CC) avl.cpp -o $(BINARY) $(LIBS) ! avlt.o: $(CC) -c avlt.cpp -o avlt.o ! bst.o: $(CC) -c bst.cpp -o bst.o clean: ! rm *.o -f --- 4,27 ---- BINARY=avl ! all: $(LIBS) $(BINARY) ! avlt.o: avlt.cpp $(CC) -c avlt.cpp -o avlt.o ! bst.o: bst.cpp $(CC) -c bst.cpp -o bst.o + $(BINARY): avl.cpp $(LIBS) + $(CC) avl.cpp -o $(BINARY) $(LIBS) + clean: ! rm *.o -f $(BINARY) ! ! shared: ! make library ! $(CC) avl.cpp -o $(BINARY) libavl.a ! ! library: ! $(CC) -fPIC -c bst.cpp -o bst.o ! $(CC) -fPIC -c avlt.cpp -o avlt.o ! $(CC) -shared -Wl,-soname,libavl.a -o libavl.a $(LIBS) -lc |
From: Patrizio B. <het...@us...> - 2004-06-24 17:40:28
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2338/src Modified Files: avlt.h Added Files: Makefile Log Message: added Makefile and fixed a compilation issue (yes..fixable with -I flag too) --- NEW FILE: Makefile --- # AVL Project CC=g++ -O2 LIBS=avlt.o bst.o BINARY=avl all: $(LIBS) $(CC) avl.cpp -o $(BINARY) $(LIBS) avlt.o: $(CC) -c avlt.cpp -o avlt.o bst.o: $(CC) -c bst.cpp -o bst.o clean: rm *.o -f Index: avlt.h =================================================================== RCS file: /cvsroot/avl/avl/src/avlt.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** avlt.h 24 Jun 2004 17:03:23 -0000 1.3 --- avlt.h 24 Jun 2004 17:40:18 -0000 1.4 *************** *** 23,27 **** #include <vector> ! #include <bst.h> using namespace std; --- 23,27 ---- #include <vector> ! #include "bst.h" using namespace std; |
From: Patrizio B. <het...@us...> - 2004-06-24 17:40:28
|
Update of /cvsroot/avl/avl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2338 Removed Files: doc Log Message: added Makefile and fixed a compilation issue (yes..fixable with -I flag too) --- doc DELETED --- |
From: Patrizio B. <het...@us...> - 2004-06-24 17:38:57
|
Update of /cvsroot/avl/avl/docs In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1865/docs Log Message: Directory /cvsroot/avl/avl/docs added to the repository |
From: Gianlorenzo D'A. <gia...@ti...> - 2004-06-24 17:31:53
|
Non lo mettere nel cvs per=F2. mettiamoci solo i sorgenti senno diventa u= n casino. Comunque non =E8 finito niente. Hai letto il TODO? Il gio, 2004-06-24 alle 19:24, Patrizio Bassi ha scritto: > ma cos=EC gli avl li hai davvero implementati tutti. >=20 > ci so rimasto male. molto. >=20 > e cmq manco compilano...mo faccio un makefile e provo a fix=E0 la > compilazione. |
From: Gianlorenzo D\\'A. <ja...@us...> - 2004-06-24 17:11:18
|
Update of /cvsroot/avl/avl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28580 Added Files: TODO Log Message: --- NEW FILE: TODO --- -Fare funzione (o classe) che visualizzi decentemente un albero binario (anche qualsiasi non per forza bst o avl) -fare classe che stressi il sistemi ovvero che faccia casualmente operazioni di inserimento e cancellazione. In particolare: -un metodo che prima inserisce un certo numero casuale di dati, poi casualmente fa un certo numero di inserimenti e cancellazioni. -un metodo che fa richiama il metodo precedente n volte. -un metodo che restituisce una sorta di benchmark contando il tempo che ci mettono le altre ad essere eseguite (si possono utilizzare le funzioni a basso livello del C). |
From: Gianlorenzo D\\'A. <ja...@us...> - 2004-06-24 17:03:32
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27094/src Modified Files: avl.cpp avlt.cpp avlt.h Log Message: avltree's class is almoust complete Index: avlt.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/avlt.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** avlt.cpp 23 Jun 2004 14:25:05 -0000 1.2 --- avlt.cpp 24 Jun 2004 17:03:23 -0000 1.3 *************** *** 286,295 **** setFdb( aux , aux2 ); ! return (Avlt *)root(); } ! void Avlt::erase(int key) { ! Avlt *aux1, *aux2; if(key == getValue()) --- 286,297 ---- setFdb( aux , aux2 ); ! return (Avlt *)root(); //si può fare di meglio } ! Avlt *Avlt::erase(int key) { ! Avlt * aux, *aux1, *aux2; ! ! aux = this; if(key == getValue()) *************** *** 299,329 **** else aux1 = (Avlt *)successor(); ! ! if( getLeft() ) ! { ! aux2 = (Avlt *)getLeft(); ! setLeft( NULL ); ! } else ! { ! aux2 = (Avlt *)getRight(); ! setRight( NULL ); ! } if( aux2 ) aux2->setParent( aux1->getParent() ); ! if( !aux1->getParent() ) ! aux2 = (Avlt *)root();//Il contrario ! else if( aux1 == aux1->getParent()->getLeft() ) aux1->getParent()->setLeft( aux2 ); else aux1->getParent()->setRight( aux2 ); ! if( aux1 != this ) setValue( aux1->getValue() ); setFdb(); ! return; } --- 301,354 ---- else aux1 = (Avlt *)successor(); ! ! if( aux1->getLeft() ) //verificare se non è aux1->getLeft() ! aux2 = (Avlt *)(aux1->getLeft()); else ! aux2 = (Avlt *)(aux1->getRight()); if( aux2 ) aux2->setParent( aux1->getParent() ); ! if( aux1->getParent() ) if( aux1 == aux1->getParent()->getLeft() ) aux1->getParent()->setLeft( aux2 ); else aux1->getParent()->setRight( aux2 ); ! ! aux1->setLeft( NULL ); ! aux1->setRight( NULL ); if( aux1 != this ) + { setValue( aux1->getValue() ); + aux2 = aux1; + do{ + aux2=(Avlt *)(aux2->getParent()); + if(aux2)//verificare se necessario + { + aux2->setFdb(); + if( aux2->getFdb() == -2 ) + if( ((Avlt *)(aux2->getRight()))->getFdb() == +1) + { + ((Avlt *)(aux2->getRight()))->rightRotate(); + aux2->leftRotate(); + } + else + aux2->leftRotate(); + else + if( aux2->getFdb() == 2 ) + if( ((Avlt *)(aux2->getLeft()))->getFdb() == -1) + { + ((Avlt *)(aux2->getLeft()))->leftRotate(); + aux2->rightRotate(); + } + else + aux2->rightRotate(); + } + }while( aux2 != this && aux2 ); + } + setFdb(); ! delete( aux1 ); //ATT!!!!!!! ! return aux; } *************** *** 338,345 **** { ((Avlt *)getRight())->rightRotate(); ! leftRotate(); } else ! leftRotate(); } } --- 363,370 ---- { ((Avlt *)getRight())->rightRotate(); ! aux = leftRotate(); } else ! aux = leftRotate(); } } *************** *** 351,363 **** setFdb(); if( getFdb() == 2 ) ! if( ((Avlt *)getRight())->getFdb() == -1) { ((Avlt *)getLeft())->leftRotate(); ! rightRotate(); } else ! rightRotate(); } } } --- 376,389 ---- setFdb(); if( getFdb() == 2 ) ! if( ((Avlt *)getLeft())->getFdb() == -1) { ((Avlt *)getLeft())->leftRotate(); ! aux = rightRotate(); } else ! aux = rightRotate(); } } + return aux; } Index: avl.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/avl.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** avl.cpp 23 Jun 2004 14:25:05 -0000 1.2 --- avl.cpp 24 Jun 2004 17:03:23 -0000 1.3 *************** *** 62,66 **** a = new Avlt(0,1); ! for (int i=0;i<600;i++) { int aaa=(rand()%1000)-500; --- 62,66 ---- a = new Avlt(0,1); ! for (int i=0;i<567;i++) { int aaa=(rand()%1000)-500; *************** *** 68,72 **** //if(i>=19) { ! /* printf("%d->%d\n",i,aaa); if(!a->checkBalance()) { --- 68,72 ---- //if(i>=19) { ! /* printf("%d->%d\n",i,aaa); if(!a->checkBalance()) { *************** *** 76,81 **** } } ! a->erase(1); ! printf("%d",a->checkBalance()); // a->print(); /*vector<Avlt *> v; --- 76,110 ---- } } ! ! // a->search(-8)->print1(); ! printf("%d\n",a->nodes()); ! a = a->erase(277); ! a = a->erase(3); ! a = a->erase(-3); ! a = a->erase(-8); ! a = a->erase(-165); ! a = a->erase(190); ! a = a->erase(415); ! a = a->erase(a->root()->getValue()); ! // a = a->erase(-7); ! printf("\n%d\n",a->nodes()); ! ! printf("\n%d",a->checkBalance()); ! //a->print1(); ! // a->search(3)->print1(); ! //a->print(); ! /* ! a = a->insert(-98); ! a = a->insert(100); ! a = a->insert(-1000); ! a = a->insert(-10); ! a = a->insert(50); ! a = a->insert(102); ! a = a->insert(1000); ! ! a->print(); ! ! a->erase(50); ! a->print1();*/ // a->print(); /*vector<Avlt *> v; Index: avlt.h =================================================================== RCS file: /cvsroot/avl/avl/src/avlt.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** avlt.h 23 Jun 2004 14:25:05 -0000 1.2 --- avlt.h 24 Jun 2004 17:03:23 -0000 1.3 *************** *** 54,58 **** Avlt *insert_ric(int); ! void erase(int); --- 54,58 ---- Avlt *insert_ric(int); ! Avlt *erase(int); |
From: Gianlorenzo D\\'A. <ja...@us...> - 2004-06-24 17:03:31
|
Update of /cvsroot/avl/avl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27094 Modified Files: ChangeLog Log Message: avltree's class is almoust complete Index: ChangeLog =================================================================== RCS file: /cvsroot/avl/avl/ChangeLog,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ChangeLog 23 Jun 2004 14:25:04 -0000 1.2 --- ChangeLog 24 Jun 2004 17:03:22 -0000 1.3 *************** *** 1,2 **** --- 1,7 ---- + --==24.06.2004==-- + <jah2003>: + -finished erase method (I hope) + -added TODO entries + --==23.06.2004==-- <jah2003>: |
From: Gianlorenzo D\\'A. <ja...@us...> - 2004-06-23 14:25:15
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12112/src Modified Files: avl.cpp avlt.cpp avlt.h bst.cpp bst.h Log Message: Index: bst.h =================================================================== RCS file: /cvsroot/avl/avl/src/bst.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** bst.h 21 Jun 2004 20:14:36 -0000 1.1 --- bst.h 23 Jun 2004 14:25:05 -0000 1.2 *************** *** 64,68 **** Bst *search(int); //OK void insert(int); //OK ! void erase(int); //No nel caso che di nodo con 2 figli in cui il succ è il figlio destro del nodo da eliminare Bst *max(); //OK Bst *min(); //OK --- 64,68 ---- Bst *search(int); //OK void insert(int); //OK ! void erase(int); //No nel caso di nodo con 2 figli in cui il succ è il figlio destro del nodo da eliminare Bst *max(); //OK Bst *min(); //OK *************** *** 71,74 **** --- 71,75 ---- void print(); + void print1(); }; Index: bst.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/bst.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** bst.cpp 21 Jun 2004 20:14:36 -0000 1.1 --- bst.cpp 23 Jun 2004 14:25:05 -0000 1.2 *************** *** 208,212 **** if( !aux1->getParent() ) ! aux2 = root(); else if( aux1 == aux1->getParent()->getLeft() ) --- 208,212 ---- if( !aux1->getParent() ) ! aux2 = root();//Il contrario else if( aux1 == aux1->getParent()->getLeft() ) *************** *** 226,230 **** { aux->erase(); ! // delete aux; } } --- 226,230 ---- { aux->erase(); ! //delete aux; } } *************** *** 324,329 **** } ! /* ! void Bst::print() { printf("\n\n%d\t",value); --- 324,329 ---- } ! ! void Bst::print1() { printf("\n\n%d\t",value); *************** *** 333,339 **** printf("destro-->%d\t",getRight()->getValue()); if( getLeft() ) ! getLeft()->print(); if( getRight() ) ! getRight()->print(); } ! */ --- 333,339 ---- printf("destro-->%d\t",getRight()->getValue()); if( getLeft() ) ! getLeft()->print1(); if( getRight() ) ! getRight()->print1(); } ! Index: avl.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/avl.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** avl.cpp 21 Jun 2004 20:14:36 -0000 1.1 --- avl.cpp 23 Jun 2004 14:25:05 -0000 1.2 *************** *** 62,82 **** a = new Avlt(0,1); ! for (int i=0;i<80;i++) { int aaa=(rand()%1000)-500; ! a= a->insert( aaa ); ! //if(i>=10) { ! printf("%d->%d\n",i,aaa); if(!a->checkBalance()) { a->print(); printf("\n-------------------------------------------------------------\n"); ! } } } ! ! //printf("%d",a->checkBalance()); ! a->print(); /*vector<Avlt *> v; v = a->getWrongs(); --- 62,82 ---- a = new Avlt(0,1); ! for (int i=0;i<600;i++) { int aaa=(rand()%1000)-500; ! a = a->insert_ric( aaa ); ! //if(i>=19) { ! /* printf("%d->%d\n",i,aaa); if(!a->checkBalance()) { a->print(); printf("\n-------------------------------------------------------------\n"); ! }*/ } } ! a->erase(1); ! printf("%d",a->checkBalance()); ! // a->print(); /*vector<Avlt *> v; v = a->getWrongs(); Index: avlt.h =================================================================== RCS file: /cvsroot/avl/avl/src/avlt.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** avlt.h 21 Jun 2004 20:14:36 -0000 1.1 --- avlt.h 23 Jun 2004 14:25:05 -0000 1.2 *************** *** 52,55 **** --- 52,57 ---- Avlt *insert(int); + Avlt *insert_ric(int); + void erase(int); Index: avlt.cpp =================================================================== RCS file: /cvsroot/avl/avl/src/avlt.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** avlt.cpp 21 Jun 2004 20:14:36 -0000 1.1 --- avlt.cpp 23 Jun 2004 14:25:05 -0000 1.2 *************** *** 131,136 **** } ! Avlt *Avlt::insert(int key) //FARE RICORSIVO (forse viene più elegante) { /* --- 131,190 ---- } + Avlt *Avlt::insert_ric(int key) + { + Avlt *aux; + aux = this; + if(key == getValue()) + return this; + if(key < getValue()) + { + if( getLeft() ) + { + ((Avlt *)getLeft())->insert_ric(key); + setFdb(); + if( getFdb() == 2 ) + if( key < getLeft()->getValue() ) + aux = rightRotate(); + else + { + ((Avlt *)(getLeft()))->leftRotate(); + aux = aux->rightRotate(); + } + } + else + { + setLeft(new Avlt); + getLeft()->setParent(this); + getLeft()->setValue(key); + setFdb(); + } + } + else + { + if( getRight() ) + { + ((Avlt *)getRight())->insert_ric(key); + setFdb(); + if( getFdb() == -2 ) + if( key > getRight()->getValue() ) + aux = leftRotate(); + else + { + ((Avlt *)(getRight()))->rightRotate(); + aux = aux->leftRotate(); + } + } + else + { + setRight(new Avlt); + getRight()->setParent(this); + getRight()->setValue(key); + setFdb(); + } + } + return aux; + } ! Avlt *Avlt::insert(int key) { /* *************** *** 155,159 **** aux = this; aux1 = this; ! aux2 = NULL; while( aux1 ) --- 209,213 ---- aux = this; aux1 = this; ! aux2 = NULL;//si può anche levare una variabile while( aux1 ) *************** *** 165,174 **** { aux1 = (Avlt *)aux1->getLeft(); ! if(aux1) { if( ((Avlt *)(aux2))->getFdb() == 1 ) { aux = aux2; ! if( key < aux1->getValue() ) caso = 1; else --- 219,228 ---- { aux1 = (Avlt *)aux1->getLeft(); ! if(aux1)//per levare una var fare lo shift dopo questo if e la condizione va fatta su aux1->getLeft() { if( ((Avlt *)(aux2))->getFdb() == 1 ) { aux = aux2; ! if( key < aux1->getValue() ) //per levare la var anche qui utilizzare aux1->getLeft()->getValue() caso = 1; else *************** *** 205,252 **** } } ! // if(aux->getRight()) ! // printf("fdb:%d;val:%d",((Avlt *)(aux->getRight()))->getFdb(),((Avlt *)(aux->getRight()))->getValue()); ! /* ! aux1 = new Avlt; ! aux1->setParent( aux2 ); ! aux1->setValue( key ); ! ! if ( key < aux2->getValue() ) ! aux2->setLeft( aux1 ); ! else ! aux2->setRight( aux1 ); ! */ aux->setFdb(); if(aux->getFdb()==2 || aux->getFdb()==-2) { ! switch(caso) ! { ! case 1: ! aux = aux->rightRotate(); ! // printf("<<<<<<<caso1"); ! break; ! case 2: ! if( aux->getLeft() ) ! ((Avlt *)(aux->getLeft()))->leftRotate(); ! aux = aux->rightRotate(); ! // printf("<<<<<<<caso2"); ! ! break; ! case 3: ! aux = aux->leftRotate(); ! // printf("<<<<<<<caso3"); ! ! break; ! case 4: ! if( aux->getRight() ) ! ((Avlt *)(aux->getRight()))->rightRotate(); ! aux = aux->leftRotate(); ! // printf("<<<<<<<caso4"); ! ! break; ! default:; ! } } ! setFdb( aux , aux2 ); //aggiorna il fdb nel percorso giusto ( forse si deve spostare prima delle rotazioni e poi controllare se fdb==2 ) return (Avlt *)root(); } --- 259,289 ---- } } ! aux->setFdb(); if(aux->getFdb()==2 || aux->getFdb()==-2) { ! switch(caso) ! { ! case 1: ! aux = aux->rightRotate(); ! break; ! case 2: ! if( aux->getLeft() ) ! ((Avlt *)(aux->getLeft()))->leftRotate(); ! aux = aux->rightRotate(); ! break; ! case 3: ! aux = aux->leftRotate(); ! break; ! case 4: ! if( aux->getRight() ) ! ((Avlt *)(aux->getRight()))->rightRotate(); ! aux = aux->leftRotate(); ! break; ! default:; ! } } ! setFdb( aux , aux2 ); ! return (Avlt *)root(); } *************** *** 254,257 **** --- 291,363 ---- void Avlt::erase(int key) { + Avlt *aux1, *aux2; + + if(key == getValue()) + { + if ( !getLeft() || !getRight() ) + aux1 = this; + else + aux1 = (Avlt *)successor(); + + if( getLeft() ) + { + aux2 = (Avlt *)getLeft(); + setLeft( NULL ); + } + else + { + aux2 = (Avlt *)getRight(); + setRight( NULL ); + } + + if( aux2 ) + aux2->setParent( aux1->getParent() ); + + if( !aux1->getParent() ) + aux2 = (Avlt *)root();//Il contrario + else + if( aux1 == aux1->getParent()->getLeft() ) + aux1->getParent()->setLeft( aux2 ); + else + aux1->getParent()->setRight( aux2 ); + + if( aux1 != this ) + setValue( aux1->getValue() ); + setFdb(); + return; + } + + if(key < getValue()) + { + if( getLeft() ) + { + ((Avlt *)getLeft())->erase( key ); + setFdb(); + if( getFdb() == -2 ) + if( ((Avlt *)getRight())->getFdb() == +1) + { + ((Avlt *)getRight())->rightRotate(); + leftRotate(); + } + else + leftRotate(); + } + } + else + { + if( getRight() ) + { + ((Avlt *)getRight())->erase( key ); + setFdb(); + if( getFdb() == 2 ) + if( ((Avlt *)getRight())->getFdb() == -1) + { + ((Avlt *)getLeft())->leftRotate(); + rightRotate(); + } + else + rightRotate(); + } + } } *************** *** 262,265 **** --- 368,372 ---- bool l, r; l = r = true; + setFdb(); if( getLeft() ) l = ((Avlt *)getLeft())->checkBalance(); |
From: Gianlorenzo D\\'A. <ja...@us...> - 2004-06-23 14:25:14
|
Update of /cvsroot/avl/avl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12112 Modified Files: ChangeLog Log Message: Index: ChangeLog =================================================================== RCS file: /cvsroot/avl/avl/ChangeLog,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ChangeLog 21 Jun 2004 20:13:01 -0000 1.1 --- ChangeLog 23 Jun 2004 14:25:04 -0000 1.2 *************** *** 1,6 **** --==21.06.2004==-- <jah2003>: -First commit (finally) (the code is about one month old) -Full implementation of BST ! -Partial implementation of Avl trees (all except the erase ! function) --- 1,11 ---- + --==23.06.2004==-- + <jah2003>: + -added recursive implementation of insert function + -added partial implementation of erase function (don't work) + --==21.06.2004==-- <jah2003>: -First commit (finally) (the code is about one month old) -Full implementation of BST ! -Partial implementation of Avl trees (all except the erase function) ! |
From: Gianlorenzo D'A. <gia...@ti...> - 2004-06-21 20:25:48
|
Ho fatto il primo commit a avl. Non =E8 che ho scritto codice l'ho messo solo per sicurezza (non vorrei che succedesse qualcosa all'hd). |
From: Gianlorenzo D\\'A. <ja...@us...> - 2004-06-21 20:14:46
|
Update of /cvsroot/avl/avl/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26739/src Added Files: avl.cpp avlt.cpp avlt.h bst.cpp bst.h Log Message: --- NEW FILE: bst.h --- /*************************************************************************** * Copyright (C) 2004 by Gianlorenzo D'Angelo * * jah@notebook0 * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifndef BST_H #define BST_H #include "stdio.h" /** @author Gianlorenzo D'Angelo */ class Bst { private: int value; Bst *left; Bst *right; Bst *parent; void erase(); void print_ric( int, int ); public: Bst(); Bst(int); ~Bst(); Bst *getLeft(); Bst *getRight(); Bst *getParent(); int getValue(); void setLeft(Bst *); void setRight(Bst *); void setParent(Bst *); void setValue(int); //operazioni sugli alberi Bst *root(); bool isLeaf(); //OK bool isRoot(); //OK int level(); //OK int nodes(); //OK int leaves(); int height(); //operazioni tipiche dei BST Bst *search(int); //OK void insert(int); //OK void erase(int); //No nel caso che di nodo con 2 figli in cui il succ è il figlio destro del nodo da eliminare Bst *max(); //OK Bst *min(); //OK Bst *predecessor(); //OK Bst *successor(); //OK void print(); }; #endif --- NEW FILE: bst.cpp --- /*************************************************************************** * Copyright (C) 2004 by Gianlorenzo D'Angelo * * jah@notebook0 * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "bst.h" Bst::Bst() { Bst(0); } Bst::Bst(int val) { value = val; left = right = parent = NULL; } Bst::~Bst() { } Bst *Bst::getLeft() { return left; } Bst *Bst::getRight() { return right; } Bst *Bst::getParent() { return parent; } int Bst::getValue() { return value; } void Bst::setLeft(Bst * l) { left = l; } void Bst::setRight(Bst * r) { right = r; } void Bst::setParent(Bst * p) { parent = p; } void Bst::setValue(int val) { value = val; } Bst *Bst::root() { Bst *aux; for( aux=this; aux->getParent(); aux=aux->getParent() ); return aux; } bool Bst::isLeaf() { return !(getLeft()||getRight()); } bool Bst::isRoot() { return !getParent(); } int Bst::level() { Bst *aux; int count; for( count=0,aux=this; aux->getParent(); aux=aux->getParent(),count++ ); return count; } int Bst::nodes() { if( isLeaf() ) return 1; int node = 1; if( getLeft() ) node += getLeft()->nodes(); if( getRight() ) node += getRight()->nodes(); return node; } int Bst::leaves() { if( isLeaf() ) return 1; int node = 0; if( getLeft() ) node = getLeft()->leaves(); if( getRight() ) node += getRight()->leaves(); return node; } int Bst::height() { if( isLeaf() ) return 0; int l, r; l = r = 0; if( getLeft() ) l = getLeft()->height(); if( getRight() ) r = getRight()->height(); return 1 + ( l>r ? l : r ); } Bst *Bst::search(int key) { Bst *aux; aux = this; while( aux ) { if( aux->getValue() == key ) return aux; if( key < aux->getValue() ) aux = aux->getLeft(); else aux = aux->getRight(); } return NULL; } void Bst::insert(int key) { Bst *aux1, *aux2; aux1 = this; aux2 = NULL; while( aux1 ) { if ( key == aux1->getValue() ) return; aux2 = aux1; if ( key < aux1->getValue() ) aux1 = aux1->getLeft(); else aux1 = aux1->getRight(); } aux1 = new Bst; aux1->setParent( aux2 ); aux1->setValue( key ); if ( key < aux2->getValue() ) aux2->setLeft( aux1 ); else aux2->setRight( aux1 ); } void Bst::erase() { Bst *aux1, *aux2; if ( !getLeft() || !getRight() ) aux1 = this; else aux1 = successor(); if( getLeft() ) { aux2 = getLeft(); setLeft( NULL ); } else { aux2 = getRight(); setRight( NULL ); } if( aux2 ) aux2->setParent( aux1->getParent() ); if( !aux1->getParent() ) aux2 = root(); else if( aux1 == aux1->getParent()->getLeft() ) aux1->getParent()->setLeft( aux2 ); else aux1->getParent()->setRight( aux2 ); if( aux1 != this ) setValue( aux1->getValue() ); } void Bst::erase(int val) { Bst *aux; aux = search( val ); if( aux ) { aux->erase(); // delete aux; } } Bst *Bst::max() { Bst *aux; for( aux=this; aux->getRight(); aux=aux->getRight() ); return aux; } Bst *Bst::min() { Bst *aux; for( aux=this; aux->getLeft(); aux=aux->getLeft() ); return aux; } Bst *Bst::predecessor() { if ( getLeft() ) return getLeft()->max(); Bst *aux1, *aux2; aux1 = getParent(); aux2 = this; while( aux1 && aux2==aux1->getLeft() ) { aux2 = aux1; aux1 = aux1->getParent(); } return aux1; } Bst *Bst::successor() { if ( getRight() ) return getRight()->min(); Bst *aux1, *aux2; aux1 = getParent(); aux2 = this; while( aux1 && aux2==aux1->getRight() ) { aux2 = aux1; aux1 = aux1->getParent(); } return aux1; } /*void Bst::print_ric(Bst* root, int level) { int i; if(root == NULL) return; print_ric(root->left, level+1); printf("%d[%d],", root->getValue(),level); print_ric(root->right, level+1); } void Bst::print() { print_ric(this, 0); }*/ void Bst::print_ric(int lev, int tab) { if(level() == lev) { printf("%d ",getValue()); } else { if( getLeft() ) getLeft()->print_ric( lev , tab ); if( getRight() ) getRight()->print_ric( lev , tab ); } } void Bst::print() { int lev, alt; Bst *aux; aux = this; alt = this->height(); lev = 0; while( lev != alt +1 ) { print_ric(lev, alt - lev); printf("\n"); lev++; } } /* void Bst::print() { printf("\n\n%d\t",value); if( getLeft() ) printf("sinistro-->%d\t",getLeft()->getValue()); if( getRight() ) printf("destro-->%d\t",getRight()->getValue()); if( getLeft() ) getLeft()->print(); if( getRight() ) getRight()->print(); } */ --- NEW FILE: avl.cpp --- /*************************************************************************** * Copyright (C) 2004 by Gianlorenzo D'Angelo * * jah@notebook0 * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include "avlt.h" #include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) { // Bst a(1 ), *b; Avlt *a, *b; /* a.insert(3); a.insert(5); a.insert(4); a.insert(2); a.insert(10); a.insert(-10); a.insert(-1); a.insert(20); a.insert(-20); a.insert(-25); a.insert(-15); a.insert(-6); a.insert(-200); a.insert(-300); a.insert(-400); a.insert(-500); a.insert(-600); a.insert(-700); */ /* a.insert(6); a.insert(3); // a.insert(-3); */ a = new Avlt(0,1); for (int i=0;i<80;i++) { int aaa=(rand()%1000)-500; a= a->insert( aaa ); //if(i>=10) { printf("%d->%d\n",i,aaa); if(!a->checkBalance()) { a->print(); printf("\n-------------------------------------------------------------\n"); } } } //printf("%d",a->checkBalance()); a->print(); /*vector<Avlt *> v; v = a->getWrongs(); for (int i=0; i<v.size(); i++) cout << (v[i])->getValue() << endl; */ // printf("%d",a->getFdb()); // a->print(); // b = (a.getLeft())->getLeft()->getRight(); // printf("%d",b->getValue()); return EXIT_SUCCESS; } --- NEW FILE: avlt.h --- /*************************************************************************** * Copyright (C) 2004 by Gianlorenzo D'Angelo * * jah@notebook0 * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifndef AVLT_H #define AVLT_H #include <vector> #include <bst.h> using namespace std; /** @author Gianlorenzo D'Angelo */ class Avlt : public Bst { private: int fdb; Avlt *rightRotate(); Avlt *leftRotate(); public: Avlt(); Avlt( int, int ); ~Avlt(); void setFdb( int ); void setFdb(); void setFdb(Avlt *, Avlt *); int getFdb(); bool checkBalance(); vector<Avlt *> getWrongs(); Avlt *insert(int); void erase(int); }; #endif --- NEW FILE: avlt.cpp --- /*************************************************************************** * Copyright (C) 2004 by Gianlorenzo D'Angelo * * jah@notebook0 * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "avlt.h" Avlt::Avlt() : Bst() { Avlt( 0,0 ); } Avlt::Avlt(int s,int val) : Bst(val) { setFdb( s ); } void Avlt::setFdb( int s ) { fdb = s; } void Avlt::setFdb() { int l, r; l = r = 0; if( getLeft() ) l = getLeft()->height() + 1; if( getRight() ) r = getRight()->height() + 1; setFdb( l-r ); } void Avlt::setFdb( Avlt * start , Avlt * end) { while( end != start ) { end->setFdb(); //forse si può migliorare end = (Avlt *)end->getParent(); } if( end ) end->setFdb(); } int Avlt::getFdb() { return fdb; } Avlt *Avlt::leftRotate() //dd { Avlt *aux; aux = (Avlt *)getRight(); if( aux ) { setRight( getRight()->getLeft() ); if( getRight() ) getRight()->setParent( this ); aux->setLeft( this ); aux->setParent( getParent() ); if( getParent() ) if( this == getParent()->getLeft() ) getParent()->setLeft( aux ); else getParent()->setRight( aux ); setParent( aux ); ((Avlt *)(aux->getLeft()))->setFdb(); return aux; } else return this; } Avlt *Avlt::rightRotate() //ss { Avlt *aux; aux = (Avlt *)getLeft(); if( aux ) { setLeft( getLeft()->getRight() ); if( getLeft() ) getLeft()->setParent( this ); aux->setRight( this ); aux->setParent( getParent() ); if( getParent() ) if( this == getParent()->getLeft() ) getParent()->setLeft( aux ); else getParent()->setRight( aux ); setParent( aux ); ((Avlt *)(aux->getRight()))->setFdb(); return aux; } else return this; } Avlt *Avlt::insert(int key) //FARE RICORSIVO (forse viene più elegante) { /* 1. si cerca il posto dove inserire l'elemento (come in bst) e intanto si memorizza il più basso +1 o -1 (aux) 2. si inserisce 3. si aggiornano i fdb del cammino tra aux e il nodo inserito (stanno tutti a 0 tranne aux) 4. se il fdb di aux è +1 [o -1] e l'inserimento viene fatto a destra [o sinistra] di aux l'albero si ribilancia da solo altrimenti viene fatta una rotazione: 4 casi: fdb==1 si inserisce in ll => rightrotation si inserisce in lr => leftrotation e poi rightrotation fdb==-1 si inserisce in rr => leftrotation si inserisce in rl => rightrotation e poi leftrotation */ Avlt *aux1, *aux2, *aux; int caso=0; aux = this; aux1 = this; aux2 = NULL; while( aux1 ) { if ( key == aux1->getValue() ) return this; aux2 = aux1; if ( key < aux1->getValue() ) { aux1 = (Avlt *)aux1->getLeft(); if(aux1) { if( ((Avlt *)(aux2))->getFdb() == 1 ) { aux = aux2; if( key < aux1->getValue() ) caso = 1; else caso = 2; } } else { aux2->setLeft(new Avlt); aux2->getLeft()->setParent(aux2); aux2->getLeft()->setValue(key); } } else { aux1 = (Avlt *)aux1->getRight(); if(aux1) { if( ((Avlt *)(aux2))->getFdb() == -1 ) { aux = aux2; if( key < aux1->getValue() ) caso = 4; else caso = 3; } } else { aux2->setRight(new Avlt); aux2->getRight()->setParent(aux2); aux2->getRight()->setValue(key); } } } // if(aux->getRight()) // printf("fdb:%d;val:%d",((Avlt *)(aux->getRight()))->getFdb(),((Avlt *)(aux->getRight()))->getValue()); /* aux1 = new Avlt; aux1->setParent( aux2 ); aux1->setValue( key ); if ( key < aux2->getValue() ) aux2->setLeft( aux1 ); else aux2->setRight( aux1 ); */ aux->setFdb(); if(aux->getFdb()==2 || aux->getFdb()==-2) { switch(caso) { case 1: aux = aux->rightRotate(); // printf("<<<<<<<caso1"); break; case 2: if( aux->getLeft() ) ((Avlt *)(aux->getLeft()))->leftRotate(); aux = aux->rightRotate(); // printf("<<<<<<<caso2"); break; case 3: aux = aux->leftRotate(); // printf("<<<<<<<caso3"); break; case 4: if( aux->getRight() ) ((Avlt *)(aux->getRight()))->rightRotate(); aux = aux->leftRotate(); // printf("<<<<<<<caso4"); break; default:; } } setFdb( aux , aux2 ); //aggiorna il fdb nel percorso giusto ( forse si deve spostare prima delle rotazioni e poi controllare se fdb==2 ) return (Avlt *)root(); } void Avlt::erase(int key) { } bool Avlt::checkBalance() { if( isLeaf() ) return true; bool l, r; l = r = true; if( getLeft() ) l = ((Avlt *)getLeft())->checkBalance(); if( getRight() ) r = ((Avlt *)getRight())->checkBalance(); return l && r && (getFdb()==0 || getFdb()==-1 || getFdb()==1); } vector<Avlt *> Avlt::getWrongs() { vector<Avlt *> v , aux; if( getLeft() ) { aux = ((Avlt *)getLeft())->getWrongs(); for(int i=0;i < aux.size();i++) v.push_back(aux[i]); } if( getRight() ) { aux = ((Avlt *)getRight())->getWrongs(); for(int i=0;i<aux.size();i++) v.push_back(aux[i]); } if( getFdb()!=0 && getFdb()!=-1 && getFdb()!=1 ) v.push_back(this); return v; } Avlt::~Avlt() { } |
From: Gianlorenzo D\\'A. <ja...@us...> - 2004-06-21 20:13:11
|
Update of /cvsroot/avl/avl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25499 Added Files: ChangeLog Log Message: --- NEW FILE: ChangeLog --- --==21.06.2004==-- <jah2003>: -First commit (finally) (the code is about one month old) -Full implementation of BST -Partial implementation of Avl trees (all except the erase function) |