Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <windows.h>
- #include <time.h>
- int tab[1000];
- int wysokosc = 5;
- int szerokosc = 20;
- typedef struct LEAF
- {
- float value;
- struct LEAF *parent;
- struct LEAF *left;
- struct LEAF *right;
- }leaf;
- void gotoxy(int x, int y);
- void add(leaf **root, float wartosc);
- void nextadd(leaf **root, leaf *parent, float wartosc);
- int treedelete (leaf **root);
- void console1(leaf **root);
- void console2(leaf **root);
- void test();
- void autoadd(leaf **root, int ilosc);
- leaf* maxvalue (leaf *root);
- leaf* minvalue (leaf *root);
- leaf* searchvalue (leaf *root, float wartosc);
- void deletenode(leaf *node, int value);
- /***********************************************************************************************/
- int main() {
- int usun;
- leaf *root = NULL;
- srand( time( NULL ) );
- console1(&root);
- if (root != NULL)
- {usun = treedelete(&root);
- if (usun == 0) printf("Drzewo bylo puste");
- else if (usun == 1) printf ("Drzewo zostalo usuniete");
- getch();
- };
- return 0;
- }
- /***********************************************************************************************/
- void gotoxy(int x, int y)
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- };
- void add(leaf **root, float wartosc)
- {
- if( *root == NULL )
- {
- *root = (leaf*) malloc( sizeof(leaf) );
- (*root)->value = wartosc;
- /* inicjalizacja rodzica */
- (*root)->parent = NULL;
- /* inicjalizacja dzieci */
- (*root)->left = NULL;
- (*root)->right = NULL;
- }
- else if(wartosc < (*root)->value)
- {
- nextadd(&(*root)->left, NULL, wartosc);
- }
- else if(wartosc > (*root)->value)
- {
- nextadd(&(*root)->right, NULL, wartosc);
- }
- }
- void nextadd (leaf **root, leaf *parent, float wartosc)
- {
- if (*root == NULL )
- {
- *root = (leaf*) malloc( sizeof(leaf) );
- (*root)->value = wartosc;
- /* inicjalizacja rodzica */
- (*root)->parent = parent;
- /* inicjalizacja dzieci */
- (*root)->left = NULL;
- (*root)->right = NULL;
- }
- else if(wartosc < (*root)->value)
- {
- nextadd(&(*root)->left, *root, wartosc);
- }
- else if(wartosc > (*root)->value)
- {
- nextadd(&(*root)->right, *root, wartosc);
- }
- }
- int treedelete (leaf **root){
- int usun;
- if (*root == NULL) usun = 0;
- else if( *root != NULL )
- {
- treedelete(&(*root)->left);
- treedelete(&(*root)->right);
- free(*root);
- *root = NULL;
- usun = 1;
- };
- return usun;
- }
- void autoadd(leaf **root, int ilosc)
- {
- int i, value;
- for (i=0;i<ilosc;i++)
- {
- value = rand() % 100000;
- value = value << 16;
- value = value | (rand() % 100000);
- add(root,value);
- }
- }
- leaf * maxvalue (leaf *root)
- { leaf *pom = root;
- if (pom != NULL)
- {
- while (pom->right != NULL) pom = pom->right;
- // printf("Maksymalna wartoscia jest: \t%f\n",pom->value);
- return pom;
- }
- else printf("Drzewo jest puste");
- return NULL;
- }
- leaf* minvalue (leaf *root)
- { leaf *pom = root;
- if (pom != NULL)
- {
- while (pom->left != NULL) pom = pom->left;
- return pom;
- // printf("Minimalna wartoscia jest: \t%f\n",pom->value);
- }
- else printf("Drzewo jest puste");
- return NULL;
- }
- leaf* searchvalue (leaf *root, float wartosc)
- {
- while (root != NULL)
- {
- if (root->value == wartosc) return root;
- else if (root->value > wartosc) root = root -> left;
- else if (root->value < wartosc) root = root -> right;
- }
- return NULL;
- }
- int draw (leaf *root)
- {
- if (root == NULL) return 0;
- else
- {
- printf("(");
- draw(root->left);
- printf("%f",root->value);
- //
- draw(root->right);
- printf(")");
- return 1;
- }
- }
- void delete_node(leaf *node, int value)
- {
- leaf *current = NULL, *del_node = NULL, *child = NULL;
- leaf *parent = NULL, *x = NULL;
- current = node;
- while (current != NULL) {
- if (value == current->value) {
- del_node = current;
- //case 1: node is a leaf (no descendants)
- if ((del_node->left == NULL) && (del_node->right == NULL)) {
- //if the node to delete is to the left of root
- if (parent->left == del_node) {
- parent->left = NULL;
- free(del_node);
- printf("Element usunieto");
- break;
- }
- //if the node to delete is to the right of root
- else if (parent->right == del_node) {
- parent->right = NULL;
- free(del_node);
- printf("Element usunieto");
- break;
- }
- }
- //case 2: node has one child
- else if ((del_node->left == NULL) || (del_node->right == NULL)) {
- //if the node to delete is to the left of root
- if (parent->left == del_node) {
- if (del_node->left != NULL) {
- child = del_node->left;
- parent->left = child;
- free(del_node);
- break;
- }
- else if (del_node->right != NULL) {
- child = del_node->right;
- parent->left = child;
- free(del_node);
- break;
- }
- }
- //if the node to delete is to the right of root
- else if (parent->right == del_node) {
- if (del_node->left != NULL) {
- child = del_node->left;
- parent->right = child;
- free(del_node);
- break;
- }
- else if (del_node->right != NULL) {
- child = del_node->right;
- parent->right = child;
- free(del_node);
- break;
- }
- }
- }
- //case 3: node has two children
- else if ((del_node->left != NULL) && (del_node->right != NULL)) {
- x = del_node;
- //if the node to delete is root
- if (parent == NULL) {
- child = del_node->right;
- if (child->left == NULL) {
- child->left = del_node->left;
- free(del_node);
- break;
- }
- else {
- while (child->left != NULL) {
- parent = child;
- child = parent->left;
- }
- x->value = child->value;
- parent->left = child->right;
- del_node = child;
- free(del_node);
- break;
- }
- }
- //if the node to delete is to the left of root
- else if (parent->left == del_node) {
- child = del_node->right;
- if (child->left == NULL) {
- parent->left = child;
- child->left = del_node->left;
- free(del_node);
- break;
- }
- else {
- while (child->left != NULL) {
- parent = child;
- child = parent->left;
- }
- x->value = child->value;
- parent->left = child->right;
- del_node = child;
- free(del_node);
- break;
- }
- }
- //if the node to delete is to the right of root
- else if (parent->right == del_node) {
- child = del_node->right;
- if (child->left == NULL) {
- parent->right = child;
- child->left = del_node->left;
- free(del_node);
- break;
- }
- else {
- while (child->left != NULL) {
- parent = child;
- child = parent->left;
- }
- x->value = child->value;
- parent->left = child->right;
- del_node = child;
- free(del_node);
- break;
- }
- }
- }
- }
- //if value is less than the node's value - go left
- else if (value < current->value) {
- parent = current;
- current = current->left;
- }
- //if value is greater than the node's value - go right
- else {
- parent = current;
- current = current->right;
- }
- }
- }
- //void deletenode(leaf **root)
- //{
- //float wartosc;
- //
- // leaf *szukaj = *root;
- // leaf *pom, *min;
- //
- // getch();
- // system("cls");
- // printf("%s\t","Podaj wartosc elementu do usuniecia:");scanf("%f",&wartosc);
- // szukaj = searchvalue(*root, wartosc);
- // if (szukaj == NULL)
- // ("Wartosc nie wystepuje w drzewie \n");
- // else if (szukaj != NULL)
- // {
- // if ((szukaj->left == NULL) && (szukaj->right == NULL))
- // {
- // /*Brak dzieci*/
- // szukaj->parent = NULL;
- // free(szukaj);
- // szukaj = NULL;
- // }
- // else if ((&(szukaj)->left == NULL) || (&(szukaj)->right == NULL))
- // {
- //
- // /*Jedno dziecko*/
- // if (&(szukaj)->left == NULL)
- // {
- // pom = szukaj;
- // szukaj = szukaj-> right;
- // pom->right = NULL;
- // pom->parent = NULL;
- // free(pom);
- // pom = NULL;
- // }
- // else if (&(szukaj)->right == NULL)
- // {
- // pom = szukaj;
- // szukaj = szukaj-> left;
- // pom->left = NULL;
- // pom->parent = NULL;
- // free(pom);
- // pom = NULL;
- // };
- // }
- // else if ((&(szukaj)->left != NULL) && (&(szukaj)->right != NULL))
- // { /*Dwoje dzieci*/
- //
- // min = minvalue(szukaj->right);
- // szukaj->value = min->value;
- // deletenode(min);
- //
- // }
- // };
- //}
- void console1(leaf **root){
- int a, wyjscie;
- wyjscie = 0;
- while (wyjscie == 0)
- {
- system("cls");
- gotoxy(szerokosc,wysokosc);printf("Lukasz Kozien\n");
- gotoxy(szerokosc,wysokosc+1);printf("Drzewo binarne\n\n");
- gotoxy(szerokosc-4,wysokosc+2);printf("1 -- Samodzielne wypelnienie drzewa\n");
- gotoxy(szerokosc-4,wysokosc+3);printf("2 -- Testowanie\n");
- gotoxy(szerokosc-4,wysokosc+4);printf("0 -- Wyjscie\n");
- gotoxy(1,wysokosc+6);printf("%s\n","Wcisnij odpowiedni klawisz");a = getch();
- //gotoxy(1,wysokosc+8);printf("%i\n",a);
- switch(a){
- case '1':
- system("cls");
- console2(root);
- break;
- case '2':
- system("cls");
- test();
- break;
- case '0':
- //exit(EXIT_SUCCESS);
- wyjscie+=1;
- break;
- // default:
- // console1(root);
- // break;
- };
- };
- };
- void console2(leaf **root){
- int a, powrot, usun;
- float wartosc;
- leaf *pom, *szukaj;
- powrot = 0;
- while (powrot == 0)
- {
- system("cls");
- gotoxy(szerokosc-4,wysokosc-1);printf("1 -- Dodanie elementu do drzewa\n");
- gotoxy(szerokosc-4,wysokosc);printf("2 -- Wyszukanie elementu w drzewie\n");
- gotoxy(szerokosc-4,wysokosc+1);printf("3 -- Wyszukiwanie maksimum\n");
- gotoxy(szerokosc-4,wysokosc+2);printf("4 -- Wyszukiwanie minimum\n");
- gotoxy(szerokosc-4,wysokosc+3);printf("5 -- Rysowanie drzewa\n");
- gotoxy(szerokosc-4,wysokosc+4);printf("6 -- Usuniecie elementu drzewa\n");
- gotoxy(szerokosc-4,wysokosc+5);printf("7 -- Usuniecie calego drzewa\n");
- gotoxy(szerokosc-4,wysokosc+6);printf("0 -- Powrot");
- gotoxy(1,wysokosc+8);printf("%s\n","Wcisnij odpowiedni klawisz"); a = getch();
- //gotoxy(1,wysokosc+8);printf("%i\n",a);
- switch(a){
- case '1':
- {printf("Dodawanie elementu\n");
- getch();//system("PAUSE");
- system("cls");
- printf("Podaj wartosc:\t");scanf("%f",&wartosc);
- add(root, wartosc);
- // if ((*root)->parent != NULL);
- // {
- // printf("%f\n Wartosc rodzica to", (*root)->(*parent)->value);
- // }
- //console2(root);
- }
- break;
- case '2':
- {printf("Wyszukuje\n");
- getch();
- system("cls");
- printf("%s\t","Podaj wartosc:");scanf("%f",&wartosc);
- szukaj = searchvalue(*root, wartosc);
- if (szukaj != NULL) printf("Wartosc %f wystepuje w drzewie \n",wartosc);
- else if (szukaj == NULL) printf("Wartosc %f nie wystepuje w drzewie \n",wartosc);
- getch();
- //system("PAUSE");
- //console2(root);
- }
- break;
- case '3':
- {printf("Maksimum\n");
- pom = maxvalue(*root);
- printf("Maksymalna wartoscia jest: \t%f\n",pom->value);
- getch();//system("PAUSE");
- //console2(root);
- }
- break;
- case '4':
- {printf("Minimum\n");
- pom = minvalue(*root);
- printf("Minimalna wartoscia jest: \t%f\n",pom->value);
- getch();//system("PAUSE");
- //console2(root);
- }
- case '5':
- {printf("Rysuje drzewo \n");
- draw(*root);
- getch();//system("PAUSE");
- //console2(root);
- }
- break;
- case '6':
- {printf("Usuwam \n");
- getch();
- system("cls");
- printf("%s\t","Podaj wartosc elementu do usuniecia:");scanf("%f",&wartosc);
- delete_node(*root, wartosc);
- //system("PAUSE");
- //console2(root);
- }
- break;
- case '7':
- {printf("Usuniecie calego drzewa\n");
- getch();//system("PAUSE");
- usun = treedelete(root);
- if (usun == 0) printf("Drzewo bylo puste\nn");
- else if (usun == 1) printf ("Drzewo zostalo usuniete\n");
- getch();// console2(root);
- }
- break;
- case '0':
- powrot+=1;
- break;
- // default:
- // console2(root);
- // break;
- };
- };
- };
- void test()
- { int i, j, value;
- int n[] = {1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000, 5000000, 10000000, 0};
- int *np;
- FILE *f;
- LARGE_INTEGER frequency, t1, t2;
- double elapsedTimeSec;
- leaf *root = NULL;
- srand (time(NULL));
- f = fopen("Raport.txt","w");
- fprintf(f,"Raport wyszukiwan drzewa binarnego\n");
- for (np = n; *np; np++) {
- treedelete(&root);
- for (i=*np;i>0;i--)
- {
- value = ((int)(rand() % 256) << 24) + ((int)(rand() % 256) << 16) + ((int)(rand() % 256) << 8) + (int)(rand() % 256);
- add(&root,value);
- if (i < 1000) {
- tab[i] = value;
- }
- }
- QueryPerformanceFrequency(&frequency);
- QueryPerformanceCounter(&t1);
- {
- for (j=0;j<1000;j++)
- searchvalue(root,tab[j]);
- }
- QueryPerformanceCounter(&t2);
- elapsedTimeSec = (double)(t2.QuadPart - t1.QuadPart) / frequency.QuadPart ;
- printf("%i\t %f\t %f\t %f\t %f\n",*np,elapsedTimeSec, t2.QuadPart, t1.QuadPart, frequency.QuadPart);
- fprintf(f,"Wyszukiwanie 1000 elementow w %i elementowym drzewie wynioslo %f \n", *np,elapsedTimeSec );
- }
- fclose(f);
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement