Guest User

Untitled

a guest
Dec 6th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.78 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. //struktura wg konspektu
  7. struct Node
  8. {
  9.     struct Node * leftChild;
  10.     struct Node * rightChild;
  11.     int value;
  12. };
  13.  
  14. //zmienna globalna na wskaznik do root'a drzewa
  15. struct Node * g_Root;
  16.  
  17. //deklaracje funkcji wg konspektu, na dole implementacje
  18. struct Node * Create(int value);
  19. void Add(struct Node * root, int value);
  20. //te trzy printy.. to niescislosc w konspekcie, bo tam jest 1 print ale potem nagle gosciu chce trzy metody - zrobilem trzy
  21. void PrintInOrder(struct Node * root);
  22. void PrintPreOrder(struct Node * root);
  23. void PrintOrder(struct Node * root);
  24. struct Node * Find(struct Node * root, int value);
  25. struct Node * Remove(struct Node * root, int value);
  26. void Free(struct Node * root);
  27.  
  28. int main()
  29. {
  30.     //scenariusz wg konspektu
  31.     g_Root = Create(100);
  32.  
  33.     Add(g_Root, 50);
  34.     Add(g_Root, 120);
  35.     Add(g_Root, 40);
  36.     Add(g_Root, 45);
  37.     Add(g_Root, 150);
  38.     Add(g_Root, 35);
  39.     Add(g_Root, 48);
  40.  
  41.     PrintInOrder(g_Root);
  42.  
  43.     Remove(g_Root, 120);
  44.     Remove(g_Root, 40);
  45.     Remove(g_Root, 50);
  46.  
  47.     PrintInOrder(g_Root);
  48.  
  49.     printf("\n\n48 search result: %s", (NULL != Find(g_Root, 48))
  50.         ? "Found!" : "Not found..");
  51.  
  52.     printf("\n70 search result: %s\n", (NULL != Find(g_Root, 70))
  53.         ? "Found!" : "Not found..");
  54.  
  55.     srand(time(NULL));
  56.     for (int i = 0; i < 10; i++)
  57.     {
  58.         Add(g_Root, rand() % 100);
  59.     }
  60.  
  61.     PrintInOrder(g_Root);
  62.     PrintOrder(g_Root);
  63.     PrintPreOrder(g_Root);
  64.  
  65.     Free(g_Root);
  66.  
  67.     printf("\n");
  68.     system("PAUSE");
  69.     return 0;
  70. }
  71.  
  72. Node * Create(int value)
  73. {
  74.     //tworzymy wskaznik i alokujemy miejsce w pamieci na nowy node
  75.     struct Node * result = (struct Node *)malloc(sizeof(struct Node));
  76.  
  77.     //przypisujemy wartosci dla nowego node
  78.     result->value = value;
  79.     result->leftChild = NULL;
  80.     result->rightChild = NULL;
  81.  
  82.     //zwracamy nowy node
  83.     return result;
  84. }
  85.  
  86. void Add(Node * root, int value)
  87. {
  88.     //sprawdzamy czy root z argumentu nie jest nullem
  89.     if (NULL != root)
  90.     {
  91.         //jesli wartosc z argumentu jest mniejsza od wartosci w aktualnym node..
  92.         if (root->value > value)
  93.         {
  94.             //i lewy child jest nullem..
  95.             if (NULL == root->leftChild)
  96.             {
  97.                 //to wlasnie znalezlismy dla niego miejsce w drzewie i robimy mu nowy node korzystajac z funkcji create
  98.                 root->leftChild = Create(value);
  99.             }
  100.             else
  101.             {
  102.                 //albo jesli root w ktorym teraz jestesmy ma lewego childa, no to rekurencyjnie nurkujemy dalej..
  103.                 Add(root->leftChild, value);
  104.             }
  105.         }
  106.         //albo jesli value z argumentu funkcji jest wiekszy od value node w ktorym aktualnie jestesmy, to..
  107.         else if(root->value < value)
  108.         {
  109.             //jesli lewy child jest nullem, wlasnie znalezlismy dla niego miejsce w drzewie..
  110.             if (NULL == root->rightChild)
  111.             {
  112.                 //no i tworzymy nowego node
  113.                 root->rightChild = Create(value);
  114.             }
  115.             //jesli jednak prawy child nie jest nullem..
  116.             else
  117.             {
  118.                 //no to rekurencyjnie nurkujemy w prawego childa..
  119.                 Add(root->rightChild, value);
  120.             }
  121.         }
  122.         //jesli jednak znajdziemy node ktorego value jest rowne value z argumentu funkcji..
  123.         else if (root->value == value)
  124.         {
  125.             //no to obslugujemy to, poniewaz nie moga byc dwie takie same wartosci w jednym drzewie - wyswietlamy stosowny komunikat
  126.             printf("Value %d already exist in the tree!\n", value);
  127.         }
  128.     }
  129. }
  130.  
  131. void PrintInOrder(Node * root)
  132. {
  133.     if (g_Root == root)
  134.     {
  135.         //podpis przy pierwszym wywolaniu funkcji, gdy root jest pierwszym node drzewa :) zeby ladniej bylo
  136.         printf("\nInOrder print:\t");
  137.     }
  138.  
  139.     //jesli aktualny node nie jest nullem, to..
  140.     if (NULL != root)
  141.     {
  142.         //rekurencyjnie nurkujemy w lewego childa
  143.         PrintInOrder(root->leftChild);
  144.         //wyswietlamy value aktualnego node w ktorym jestesmy
  145.         printf("%d, ", root->value);
  146.         //i rekurencyjnie nurkujemy w prawego childa
  147.         PrintInOrder(root->rightChild);
  148.     }
  149.  
  150. }
  151.  
  152. void PrintPreOrder(Node * root)
  153. {
  154.     if (g_Root == root)
  155.     {
  156.         //to samo co w in order
  157.         printf("\nPreOrder print:\t");
  158.     }
  159.  
  160.     //tez..
  161.     if (NULL != root)
  162.     {
  163.         //roznica taka, ze kolejnosc sie zmienia - szczegoly patrz w PrintInOrder
  164.         printf("%d, ", root->value);
  165.         PrintPreOrder(root->leftChild);
  166.         PrintPreOrder(root->rightChild);
  167.     }
  168. }
  169.  
  170. void PrintOrder(Node * root)
  171. {
  172.     //tez tylko roznica w kolejnosci, patrz w PrintInOrder
  173.     if (g_Root == root)
  174.     {
  175.         printf("\nOrder print:\t");
  176.     }
  177.  
  178.     if (NULL != root)
  179.     {
  180.         PrintOrder(root->leftChild);
  181.         PrintOrder(root->rightChild);
  182.         printf("%d, ", root->value);
  183.     }
  184. }
  185.  
  186. Node * Find(Node * root, int value)
  187. {
  188.     //robimy sobie zmienna ze wskaznikiem na rezultat metody
  189.     struct Node * result = NULL;
  190.    
  191.     //sprawdzamy czy root z argumenty nie jest nullem
  192.     if (NULL != root)
  193.     {
  194.         //jesli value w aktualnym node jest mniejsza od value z argumentu funkcji..
  195.         if (root->value > value)
  196.         {
  197.             //to nurkujemy rekurencyjnie w lewego childa, jednoczesnie przypisujac wartosc zwracana do zmiennej result (w niej bedzie znaleziony node)
  198.             result = Find(root->leftChild, value);
  199.         }
  200.         //jesli value w aktualnym node jest wieksza od value z argumentu funkcji no to..
  201.         else if (root->value < value)
  202.         {
  203.             //rekurencyjnie nurkujemy w praweo childa jednoczesnie przypisujac wynik find do zmiennej result - tak wyluskamy wynik wyszukiwania
  204.             result = Find(root->rightChild, value);
  205.         }
  206.         //jesli obie value sa rowne..
  207.         else if (root->value == value)
  208.         {
  209.             //no to brawo, znalezlismy szukana wartosc i juz konczymy rekurencje, tylko przypisujemy do zmiennej result root w ktorym aktualnie jestesmy bo go szukalismy
  210.             result = root;
  211.         }
  212.     }
  213.  
  214.     //zwracamy zmienna result z wynikiem wyszukiwania
  215.     return result;
  216. }
  217.  
  218. struct Node* Remove(struct Node* root, int valueToRemove)
  219. {
  220.     struct Node * result = root;
  221.     if (NULL != result)
  222.     {
  223.         struct Node * tempNode = NULL;
  224.  
  225.         if (root->value > valueToRemove)
  226.         {
  227.             root->leftChild = Remove(root->leftChild, valueToRemove);
  228.         }
  229.         else if (root->value < valueToRemove)
  230.         {
  231.             root->rightChild = Remove(root->rightChild, valueToRemove);
  232.         }
  233.         else if(valueToRemove == root->value)
  234.         {
  235.             if (root->leftChild == NULL)
  236.             {
  237.                 tempNode = root->rightChild;
  238.                 free(root);
  239.                 result = tempNode;
  240.             }
  241.             else if (root->rightChild == NULL)
  242.             {
  243.                 tempNode = root->leftChild;
  244.                 free(root);
  245.                 result = tempNode;
  246.             }
  247.             else
  248.             {
  249.                 struct Node* minValueNode = root->rightChild;
  250.                 while (NULL != minValueNode->leftChild)
  251.                 {
  252.                     minValueNode  = minValueNode->leftChild;
  253.                 }
  254.                 root->value = minValueNode->value;
  255.                 root->rightChild = Remove(root->rightChild, minValueNode->value);
  256.                 result = root;
  257.             }
  258.         }
  259.     }
  260.  
  261.     return result;
  262. }
  263.  
  264. void Free(Node * root)
  265. {
  266.     if (NULL != root && root != g_Root)
  267.     {
  268.         Free(root->leftChild);
  269.         Free(root->rightChild);
  270.         free(root);
  271.     }
  272.     else if (root == g_Root)
  273.     {
  274.         Free(root->leftChild);
  275.         Free(root->rightChild);
  276.         free(root);
  277.         g_Root = NULL;
  278.     }
  279. }
Add Comment
Please, Sign In to add comment