Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // --------------------------------------------------------
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <string.h>
- #include <stdbool.h>
- #include <time.h>
- #include <ctype.h>
- // --------------------------------------------------------
- #define RED SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0*16+12); // ROT
- #define GRN SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0*16+10) // GRÜN
- #define YEL SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0*16+14) // GELB
- #define BLU SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0*16+1) // BLAU
- #define RESET SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0*16+15) // RESET
- // --------------------------------------------------------
- int start(int);
- int addelement(int);
- int addroot();
- int suche(int);
- int showroot(int);
- // --------------------------------------------------------
- struct knoten
- {
- int wert;
- struct knoten *links;
- struct knoten *rechts;
- };
- // --------------------------------------------------------
- struct knoten *root = NULL;
- struct knoten *element = NULL;
- // --------------------------------------------------------
- void main()
- {
- int wurzel;
- int suche;
- system("CLS");
- RESET;
- printf("\nBitte Wurzel eingeben: ");
- scanf("%d",&wurzel);
- root = malloc(sizeof(struct knoten));
- root -> wert = wurzel;
- root -> rechts = NULL;
- root -> links = NULL;
- system("CLS");
- printf("\t\t\tWurzel: %d",root -> wert);
- start(wurzel);
- }
- // --------------------------------------------------------
- int start(int wurzel)
- {
- struct knoten *zeiger = root;
- int i;
- char string[63] = "-----------------------------BAUM------------------------------";
- printf("\n\n\t\t\t");
- for(i = 0; i < 63; i++)
- {
- if(i % 2 == 0)
- {
- RED;
- printf("%c",string[i]);
- RESET;
- }
- else
- {
- BLU;
- printf("%c",string[i]);
- RESET;
- }
- }
- printf("\n\n");
- /*
- RED;
- printf("\n\n\t\t\t-----------------------------BAUM-----------------------------\n\n\n\n");
- RESET;
- */
- int auswahl;
- int zahl;
- RED;
- printf("Was m\x94 \bchten Sie tun?");
- RESET;
- printf("\n-----------------------------\n");
- RED;
- printf(" 1: Suche!\n");
- printf(" 2: Element hinzuf\x81gen!\n");
- printf(" 3: Wurzel \x84ndern!\n");
- printf(" 4: Wurzel Anzeigen!\n");
- RESET;
- printf("-----------------------------\n");
- RED;
- printf("WAHL --> ");
- RESET;
- scanf("%d",&auswahl);
- switch(auswahl)
- {
- case 1: suche(wurzel); start(wurzel); break;
- case 2: addelement(wurzel); start(wurzel); break;
- case 3: addroot(); start(wurzel); break;
- case 4: showroot(wurzel); start(wurzel); break;
- }
- }
- // --------------------------------------------------------
- int showroot(int wurzel)
- {
- printf("\n\nWert der Wurzel: %d\n\n",root -> wert);
- }
- // --------------------------------------------------------
- int addelement(int wurzel)
- {
- struct knoten *zeiger = root;
- bool f = true;
- printf("Abbrechen mit ESC + Eingabe!");
- while(f)
- {
- printf("\n\n\nGeben Sie die Zahl ein: ");
- scanf("%d",&wurzel);
- zeiger = root;
- while(1)
- {
- if(GetAsyncKeyState(VK_ESCAPE))
- {
- f = false;
- }
- if(wurzel < zeiger -> wert)
- {
- if(zeiger -> links == NULL)
- {
- printf("\nElement:[");
- YEL;
- printf("L");
- RESET;
- printf("]");
- element = malloc(sizeof(struct knoten));
- element -> wert = wurzel;
- zeiger -> links = element;
- element -> rechts = NULL;
- element -> links = NULL;
- break;
- }
- else
- {
- printf("\nElement:[");
- YEL;
- printf("L");
- RESET;
- printf("]");
- zeiger = zeiger -> links;
- }
- }
- else if(wurzel > zeiger -> wert)
- {
- if(zeiger -> rechts == NULL)
- {
- element = malloc(sizeof(struct knoten));
- element -> wert = wurzel;
- zeiger -> rechts = element;
- element -> rechts = NULL;
- element -> links = NULL;
- printf("\nElement:[");
- BLU;
- printf("R");
- RESET;
- printf("]");
- break;
- }
- else
- {
- printf("\nElement:[");
- BLU;
- printf("R");
- RESET;
- printf("]");
- zeiger = zeiger -> rechts;
- }
- }
- else
- {
- break;
- }
- }
- }
- }
- // --------------------------------------------------------
- int addroot()
- {
- int wurzel;
- printf("\nGeben Sie die Wurzel ein: ");
- scanf("%d",&wurzel);
- root = malloc(sizeof(struct knoten));
- root -> wert = wurzel;
- root -> rechts = NULL;
- root -> links = NULL;
- }
- // --------------------------------------------------------
- int suche(int wurzel)
- {
- system("CLS");
- struct knoten *zeiger = root;
- int suche;
- printf("\nWelche Nummer m\x94 \bchten sie suchen?: ");
- scanf("%d",&suche);
- zeiger = root;
- while(1)
- {
- if(suche == zeiger -> wert)
- {
- GRN;
- printf("\nElement gefunden.\n");
- RESET;
- break;
- }
- else
- {
- if(suche < zeiger -> wert && zeiger -> links != NULL)
- {
- zeiger = zeiger -> links;
- if(suche == zeiger -> wert)
- {
- GRN;
- printf("\nElement gefunden.\n");
- RESET;
- break;
- }
- }
- else if(suche > zeiger -> wert && zeiger -> rechts != NULL)
- {
- zeiger = zeiger -> rechts;
- if(suche == zeiger -> wert)
- {
- GRN;
- printf("\nElement gefunden.\n");
- RESET;
- break;
- }
- }
- else
- {
- RED;
- printf("\nElement NICHT gefunden.\n");
- RESET;
- break;
- }
- }
- }
- }
- // --------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement