Advertisement
Guest User

Depth-First Search in C (v2)

a guest
Nov 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. int DFS(stos *S1, drzewo *D1) // Depth-First Search - Przeszukiwanie w głąb
  2. {
  3.     drzewo pomocniczeD = (*D1);
  4.  
  5.     DodawanieDoStosu(&(*S1), &(*D1));
  6.     fprintf(stdout, "%d ", (pomocniczeD->kluczD));
  7.     (*D1)->czyOdwiedzony = 0;
  8.  
  9.     while ((*S1) != 0)
  10.     {
  11.         while (((pomocniczeD->lewy) != 0) && ((pomocniczeD->lewy->czyOdwiedzony) != 0))
  12.         {
  13.             DodawanieDoStosu(&(*S1), &pomocniczeD->lewy);
  14.             if (pomocniczeD->lewy->czyOdwiedzony != 0)
  15.                 fprintf(stdout, "%d ", (pomocniczeD->lewy->kluczD));
  16.             pomocniczeD->lewy->czyOdwiedzony = 0;
  17.             pomocniczeD = pomocniczeD->lewy;
  18.         }
  19.         while ((pomocniczeD->prawy) == 0)
  20.         {
  21.             UsuwanieZPoczątku(&(*S1));
  22.             if ((*S1) != 0)
  23.                 pomocniczeD = (*S1)->kluczS;
  24.             else if ((*S1) == 0)
  25.                 return 0;
  26.         }
  27.  
  28.         if ((pomocniczeD->prawy->czyOdwiedzony) == 0)
  29.         {
  30.             UsuwanieZPoczątku(&(*S1));
  31.             if ((*S1) != 0)
  32.                 pomocniczeD = (*S1)->kluczS;
  33.             else if ((*S1) == 0)
  34.                 return 0;
  35.         }
  36.         else
  37.         {
  38.             DodawanieDoStosu(&(*S1), &pomocniczeD->prawy);
  39.             if (pomocniczeD->prawy->czyOdwiedzony != 0)
  40.                 fprintf(stdout, "%d ", (pomocniczeD->prawy->kluczD));
  41.             pomocniczeD->prawy->czyOdwiedzony = 0;
  42.             pomocniczeD = pomocniczeD->prawy;
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement