/*----------------------------------------------------------------------- File : bst.c Author: Stephan Schulz (schulz@eprover.org) Contents Program that builds and tests binary search trees. Copyright 2015-2018 by the author. This code is released under the GNU General Public Licence, version 2, or, at your choice, any later version. See the file COPYING. Changes <1> Mon Jun 22 20:43:15 CEST 2015 New -----------------------------------------------------------------------*/ #include #include #include #include #include #define MAXLINE 256 /* Binary tree cell */ typedef struct bintreecell { long key; char* payload; struct bintreecell *lchild; struct bintreecell *rchild; }BinTree, *BinTree_p; /*----------------------------------------------------------------------- // // Function: xmalloc() // // Thin wrapper around malloc() - fail noisily (with error message) // if no memory is available. // // Global Variables: - // // Side Effects : Only via malloc() or in case of error // /----------------------------------------------------------------------*/ void* xmalloc(size_t size) { void *mem = malloc(size); if(!mem) { fprintf(stderr, "lin_list: Out of memory\n"); exit(EXIT_FAILURE); } return mem; } /*----------------------------------------------------------------------- // // Function: xstrdup() // // Thin wrapper around strdup() - fail noisily (with error message) // if no memory is available. // // Global Variables: - // // Side Effects : Only via malloc() or in case of error // /----------------------------------------------------------------------*/ char* xstrdup(char* str) { void *newstr = strdup(str); if(!newstr) { fprintf(stderr, "lin_list: Out of memory\n"); exit(EXIT_FAILURE); } return newstr; } /*----------------------------------------------------------------------- // // Function: main() // // Read a file of insert/delete/find/print instructions for a // binary search tree, and "execute" them (well, execute the // comments that tell you where to put your code). // // Global Variables: - // // Side Effects : Reads input, prints output // /----------------------------------------------------------------------*/ int main(int argc, char *argv[]) { FILE *in = stdin; char line[MAXLINE]; char* inp; int key; char* valptr; BinTree_p tree; if(argc > 2) { fprintf(stderr, "Usage: %s []\n", argv[0]); exit(EXIT_FAILURE); } if(argc == 2) { in = fopen(argv[1], "r"); if(!in) { perror(argv[0]); exit(EXIT_FAILURE); } } tree = NULL; while((inp = fgets(line, MAXLINE, in))) { if(strncmp(line, "I:", 2)==0) { key = atoi(&line[2]); valptr = strchr(line, '\n'); if(valptr) { *valptr = '\0'; } valptr = strchr(line, ','); if(valptr) { valptr++; printf("Insert (%d, %s) into the tree\n", key, valptr); /* Put insertion code here ! */ } } else if(strncmp(line, "F:", 2)==0) { key = atoi(&line[2]); printf("Searching key %d in the tree\n", key); /* Put find code here ! */ } else if(strncmp(line, "D:", 2)==0) { key = atoi(&line[2]); printf("Remove record with key %d from the tree\n", key); /* Put delete code here ! */ } else if(strncmp(line, "P:", 2)==0) { printf("Print the full tree\n"); /* Put Print code here ! */ } } if(in!=stdin) { fclose(in); } exit(EXIT_SUCCESS); } /*---------------------------------------------------------------------*/ /* End of File */ /*---------------------------------------------------------------------*/