Advertisement
Jak0b

baum

May 24th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.51 KB | None | 0 0
  1. // --------------------------------------------------------
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. #include <string.h>
  6. #include <stdbool.h>
  7. #include <time.h>
  8. #include <ctype.h>
  9. // --------------------------------------------------------
  10. #define RED         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0*16+12);  // ROT
  11. #define GRN         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0*16+10)   // GRÜN
  12. #define YEL         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0*16+14)   // GELB
  13. #define BLU         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0*16+1)    // BLAU
  14. #define RESET       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0*16+15)   // RESET
  15. // --------------------------------------------------------
  16. int start(int);
  17. int addelement(int);
  18. int addroot();
  19. int suche(int);
  20. int showroot(int);
  21. // --------------------------------------------------------
  22. struct knoten
  23. {
  24.     int wert;
  25.     struct knoten *links;
  26.     struct knoten *rechts;
  27. };
  28. // --------------------------------------------------------
  29. struct knoten *root = NULL;
  30. struct knoten *element = NULL;
  31. // --------------------------------------------------------
  32. void main()
  33. {
  34.     int wurzel;
  35.     int suche;
  36.  
  37.     system("CLS");
  38.     RESET;
  39.     printf("\nBitte Wurzel eingeben: ");
  40.     scanf("%d",&wurzel);
  41.    
  42.     root =  malloc(sizeof(struct knoten));
  43.     root -> wert = wurzel;
  44.     root -> rechts = NULL;
  45.     root -> links = NULL;
  46.    
  47.     system("CLS");
  48.    
  49.     printf("\t\t\tWurzel: %d",root -> wert);
  50.    
  51.     start(wurzel);
  52. }
  53. // --------------------------------------------------------
  54. int start(int wurzel)
  55. {  
  56.     struct knoten *zeiger = root;
  57.     int i;
  58.    
  59.     char string[63] = "-----------------------------BAUM------------------------------";
  60.    
  61.     printf("\n\n\t\t\t");
  62.     for(i = 0; i < 63; i++)
  63.     {
  64.         if(i % 2 == 0)
  65.         {
  66.             RED;
  67.             printf("%c",string[i]);
  68.             RESET;
  69.         }
  70.         else
  71.         {
  72.             BLU;
  73.             printf("%c",string[i]);
  74.             RESET;
  75.         }
  76.     }
  77.     printf("\n\n");
  78.  
  79.     /*
  80.     RED;
  81.     printf("\n\n\t\t\t-----------------------------BAUM-----------------------------\n\n\n\n");
  82.     RESET;
  83.     */
  84.     int auswahl;
  85.     int zahl;
  86.     RED;
  87.     printf("Was m\x94 \bchten Sie tun?");
  88.     RESET;
  89.     printf("\n-----------------------------\n");
  90.     RED;
  91.     printf(" 1: Suche!\n");
  92.     printf(" 2: Element hinzuf\x81gen!\n");
  93.     printf(" 3: Wurzel \x84ndern!\n");
  94.     printf(" 4: Wurzel Anzeigen!\n");
  95.     RESET;
  96.     printf("-----------------------------\n");
  97.     RED;
  98.     printf("WAHL --> ");
  99.     RESET; 
  100.     scanf("%d",&auswahl);
  101.    
  102.     switch(auswahl)
  103.     {
  104.         case 1: suche(wurzel);          start(wurzel); break;
  105.         case 2: addelement(wurzel);     start(wurzel); break;
  106.         case 3: addroot();              start(wurzel); break;
  107.         case 4: showroot(wurzel);       start(wurzel); break;
  108.     }
  109. }
  110. // --------------------------------------------------------
  111. int showroot(int wurzel)
  112. {
  113.     printf("\n\nWert der Wurzel: %d\n\n",root -> wert);
  114. }
  115. // --------------------------------------------------------
  116. int addelement(int wurzel)
  117. {
  118.     struct knoten *zeiger = root;
  119.    
  120.     bool f = true;
  121.    
  122.     printf("Abbrechen mit ESC + Eingabe!");
  123.    
  124.     while(f)
  125.     {
  126.         printf("\n\n\nGeben Sie die Zahl ein: ");
  127.         scanf("%d",&wurzel);
  128.        
  129.         zeiger = root;
  130.        
  131.         while(1)
  132.         {
  133.             if(GetAsyncKeyState(VK_ESCAPE))
  134.             {          
  135.                 f = false;
  136.             }
  137.             if(wurzel < zeiger -> wert)
  138.             {
  139.                 if(zeiger -> links == NULL)
  140.                 {
  141.                     printf("\nElement:[");
  142.                     YEL;
  143.                     printf("L");
  144.                     RESET;
  145.                     printf("]");
  146.                     element = malloc(sizeof(struct knoten));
  147.                     element -> wert = wurzel;
  148.                     zeiger -> links = element;
  149.                     element -> rechts = NULL;
  150.                     element -> links = NULL;
  151.                     break;
  152.                 }
  153.                 else
  154.                 {
  155.                     printf("\nElement:[");
  156.                     YEL;
  157.                     printf("L");
  158.                     RESET;
  159.                     printf("]");
  160.                     zeiger = zeiger -> links;
  161.                 }
  162.             }  
  163.             else if(wurzel > zeiger -> wert)
  164.             {
  165.                 if(zeiger -> rechts == NULL)
  166.                 {
  167.                     element = malloc(sizeof(struct knoten));
  168.                     element -> wert = wurzel;
  169.                     zeiger -> rechts = element;
  170.                     element -> rechts = NULL;
  171.                     element -> links = NULL;
  172.                     printf("\nElement:[");
  173.                     BLU;
  174.                     printf("R");
  175.                     RESET;
  176.                     printf("]");
  177.                     break;
  178.                 }
  179.                 else
  180.                 {
  181.                     printf("\nElement:[");
  182.                     BLU;
  183.                     printf("R");
  184.                     RESET;
  185.                     printf("]");
  186.                     zeiger = zeiger -> rechts;
  187.                 }
  188.             }
  189.             else
  190.             {
  191.                 break;
  192.             }
  193.         }
  194.     }
  195. }
  196. // --------------------------------------------------------
  197. int addroot()
  198. {
  199.     int wurzel;
  200.    
  201.     printf("\nGeben Sie die Wurzel ein: ");
  202.     scanf("%d",&wurzel);
  203.    
  204.     root = malloc(sizeof(struct knoten));
  205.     root -> wert = wurzel;
  206.     root -> rechts = NULL;
  207.     root -> links = NULL;
  208. }
  209. // --------------------------------------------------------
  210. int suche(int wurzel)
  211. {
  212.     system("CLS");
  213.     struct knoten *zeiger = root;
  214.     int suche;
  215.     printf("\nWelche Nummer m\x94 \bchten sie suchen?: ");
  216.     scanf("%d",&suche);
  217.    
  218.     zeiger = root;
  219.     while(1)
  220.     {
  221.         if(suche == zeiger -> wert)
  222.         {      
  223.             GRN;
  224.             printf("\nElement gefunden.\n");
  225.             RESET;
  226.             break;
  227.         }
  228.         else
  229.         {
  230.             if(suche < zeiger -> wert && zeiger -> links != NULL)
  231.             {
  232.                 zeiger = zeiger -> links;
  233.                 if(suche == zeiger -> wert)
  234.                 {
  235.                     GRN;
  236.                     printf("\nElement gefunden.\n");
  237.                     RESET;
  238.                     break;
  239.                 }
  240.             }
  241.             else if(suche > zeiger -> wert && zeiger -> rechts != NULL)
  242.             {
  243.                 zeiger = zeiger -> rechts;
  244.                 if(suche == zeiger -> wert)
  245.                 {
  246.                     GRN;
  247.                     printf("\nElement gefunden.\n");
  248.                     RESET;
  249.                     break;
  250.                 }
  251.             }
  252.            
  253.             else
  254.             {
  255.                 RED;
  256.                 printf("\nElement NICHT gefunden.\n");
  257.                 RESET;
  258.                 break;
  259.             }
  260.         }
  261.     }
  262. }
  263. // --------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement