#include #include #include #include "bstree.h" #include "fields.h" prepostorder(int pre, void *bn, int depth) { int i; if (bn == NULL) return; if (pre) { for (i = 0; i < depth; i++) printf(" "); printf("%s %.2lf\n", bstree_key(bn), *(double *)bstree_value(bn)); } prepostorder(pre, bstree_left(bn), depth+2); prepostorder(pre, bstree_right(bn), depth+2); if (!pre) { for (i = 0; i < depth; i++) printf(" "); printf("%s %.2lf\n", bstree_key(bn), *(double *)bstree_value(bn)); } } inorder(void *bn) { if (bn == NULL) return; inorder(bstree_left(bn)); printf("%-30s %10.2lf\n", bstree_key(bn), *(double *)bstree_value(bn)); inorder(bstree_right(bn)); } double *new_double(double dval) { double *new_d = (double *)malloc(sizeof(double)); *new_d = dval; return new_d; } main() { void *b; void *bn; IS is; double dval; is = new_inputstruct(NULL); b = new_bstree(); while (1) { printf("BSTREE> "); fflush(stdout); if (get_line(is) < 0) exit(0); if (is->NF > 0) { if (strcmp(is->fields[0], "INSERT") == 0) { if (is->NF != 3) { printf("INSERT name value\n"); } else if (sscanf(is->fields[2], "%lf\n", &dval) != 1) { printf("INSERT name value -- value must be a number\n"); } else { (void) bstree_insert(b, strdup(is->fields[1]), new_double(dval)); } } else if (strcmp(is->fields[0], "DELETE") == 0) { if (is->NF != 2) { printf("DELETE name\n"); } else if (bstree_delete(b, is->fields[1]) == NULL) { printf("No %s in the tree\n", is->fields[1]); } } else if (strcmp(is->fields[0], "INORDER") == 0) { if (is->NF != 1) { printf("INORDER takes no arguments\n"); } else { inorder(bstree_root(b)); } } else if (strcmp(is->fields[0], "PREORDER") == 0) { if (is->NF != 1) { printf("PREORDER takes no arguments\n"); } else { prepostorder(1, bstree_root(b), 0); } } else if (strcmp(is->fields[0], "MAX") == 0) { if (is->NF != 1) { printf("MAX takes no arguments\n"); } else { bn = bstree_find_max(b); if (bn == NULL) { printf("No maximum element\n"); } else { printf("%s %.2lf\n", bstree_key(bn), *(double *)bstree_value(bn)); } } } else if (strcmp(is->fields[0], "MIN") == 0) { if (is->NF != 1) { printf("MIN takes no arguments\n"); } else { bn = bstree_find_min(b); if (bn == NULL) { printf("No minimum element\n"); } else { printf("%s %.2lf\n", bstree_key(bn), *(double *)bstree_value(bn)); } } } else if (strcmp(is->fields[0], "POSTORDER") == 0) { if (is->NF != 1) { printf("POSTORDER takes no arguments\n"); } else { prepostorder(0, bstree_root(b), 0); } } else if (strcmp(is->fields[0], "QUIT") == 0) { exit(0); } else { printf("Valid commands are INSERT DELETE INORDER PREORDER POSTORDER MAX MIN QUIT\n"); } } } }