Jak0b

Baum.c

May 22nd, 2016
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.86 KB | None | 0 0
  1. // -------------------------------------------------------
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <ctype.h>
  5. #include <process.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdbool.h>
  9. #include <time.h>
  10. #include <windows.h>
  11. // -------------------------------------------------------
  12. #define RED         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0*15+12)
  13. #define RESET       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0*15+15)
  14. // -------------------------------------------------------
  15. struct knoten
  16. {
  17.     struct knoten *rechts;
  18.     struct knoten *links;
  19.     int wert;
  20. };
  21. // -------------------------------------------------------
  22. struct knoten *root = NULL;     // Zeiger auf die Wurzel
  23. struct knoten *next = NULL;     // Zeiger auf das rechte Element
  24. struct knoten *prev = NULL;     // Zeiger auf das linke  Element
  25. // -------------------------------------------------------
  26. int start(int);
  27. int addelement(int);
  28. int addroot(int);
  29. // -------------------------------------------------------
  30. int main()
  31. {
  32.     system("CLS");
  33.     int wurzel;
  34.    
  35.     printf("Geben Sie ihre Wurzel ein: ");
  36.     scanf("%d",&wurzel);
  37.    
  38.     root = malloc(sizeof(struct knoten));
  39.     root -> wert = wurzel;
  40.    
  41.     start(wurzel);
  42. }
  43. // -------------------------------------------------------
  44. int start(int wurzel)
  45. {
  46.     int auswahl;
  47.    
  48.     printf("\n\n");
  49.    
  50.     printf("Was m\x94 \bchten Sie tun?\n");
  51.     printf("--------------------\n");
  52.     printf(" 1: Wurzel bestimmen!\n");
  53.     printf(" 2: Element hinzuf\x81 \bgen!\n");
  54.     printf("--------------------\n");
  55.     printf("Wahl: ");
  56.     scanf("%d",&auswahl);
  57.    
  58.     switch(auswahl)
  59.     {
  60.         case 1: addroot(wurzel); break;
  61.         case 2: addelement(wurzel); start(wurzel); break;
  62.     }
  63. }
  64. // -------------------------------------------------------
  65. int addroot(wurzel)
  66. {
  67.     struct knoten *zeiger;
  68.     printf("Geben Sie ihre Wurzel ein: ");
  69.     scanf("%d",&wurzel);
  70.    
  71.     if(root == NULL)
  72.     {
  73.         root = malloc(sizeof(struct knoten));
  74.         root -> wert = wurzel;
  75.     }
  76.     else
  77.     {
  78.         // root = malloc(sizeof(struct knoten));
  79.         root -> wert = wurzel;
  80.     }
  81.    
  82.     printf("\nIhre Wurzel hat nun den Wert: %d\n\n",root -> wert);
  83.    
  84.     start(wurzel);
  85. }
  86. // -------------------------------------------------------
  87. int addelement(int wurzel)
  88. {
  89.     printf("\nIhre Wurzel hat nun den Wert: %d\n\n",root -> wert);
  90.    
  91.     struct knoten *zeiger;
  92.     int element;
  93.     printf("Geben Sie ihr Element ein: ");
  94.     scanf("%d",&element);
  95.    
  96.     if(element < wurzel)
  97.     {
  98.         if(prev == NULL)
  99.         {
  100.             printf("CHECK 1\n");
  101.             printf("Element[");RED;printf("L");RESET;printf("]");
  102.             prev = malloc(sizeof(struct knoten));
  103.             prev -> wert = element;
  104.         }
  105.         else
  106.         {
  107.             while(zeiger -> links != NULL && zeiger -> rechts != NULL)
  108.             {
  109.                 if(element < prev -> wert)
  110.                 {
  111.                     printf("CHECK 2\n");
  112.                     printf("Element[");RED;printf("L");RESET;printf("]");
  113.                     prev = malloc(sizeof(struct knoten));
  114.                     prev -> wert = element;
  115.                 }
  116.                 else
  117.                 {
  118.                     printf("CHECK 3\n");
  119.                     printf("Element[");RED;printf("R");RESET;printf("]");
  120.                     next = malloc(sizeof(struct knoten));
  121.                     next -> wert = element;
  122.                 }
  123.                 zeiger = zeiger -> rechts;
  124.             }
  125.         }
  126.     }
  127.     else
  128.     {
  129.         if(next == NULL)
  130.         {
  131.             printf("CHECK 4\n");
  132.             printf("Element[");RED;printf("R");RESET;printf("]");
  133.             next = malloc(sizeof(struct knoten));
  134.             next -> wert = element;
  135.         }
  136.         else
  137.         {
  138.             while(zeiger -> rechts != NULL && zeiger -> links != NULL)
  139.             {
  140.                 if(element < prev -> wert)
  141.                 {
  142.                     printf("CHECK 5\n");
  143.                     printf("Element[");RED;printf("R");RESET;printf("]");
  144.                     prev = malloc(sizeof(struct knoten));
  145.                     prev -> wert = element;
  146.                 }
  147.                 else
  148.                 {
  149.                     printf("CHECK 6\n");
  150.                     printf("Element[");RED;printf("L");RESET;printf("]");
  151.                     next = malloc(sizeof(struct knoten));
  152.                     next -> wert = element;
  153.                 }
  154.                 zeiger = zeiger -> rechts;
  155.             }
  156.         }
  157.        
  158.     }
  159. }
  160. // -------------------------------------------------------
Add Comment
Please, Sign In to add comment