int main(void) {
  char buf[BUFSIZE];
  int operator, operand;
  bool terminal_flag = false;
  node_pointer_t root;

  while(!terminal_flag) {
    operator = -1;
    operand = -1;
    fgets(buf, BUFSIZE-2, stdin);
    sscanf(buf, "%d %d", &operator, &operand);

    switch(operator) {
      case 0:
        if(!insert_bst(&root, operand)) {
          fprintf(stderr, " [!] insertion failed, end program\n");
          exit(EXIT_FAILURE);
        }
        break;
      case 1:
        if(!delete_bst(root, operand)) {
          fprintf(stderr, " [!] deletion failed, end program\n");
          exit(EXIT_FAILURE);
        }
        break;
      case 2:
        print_preorder_bst(root);
        printf("\n");
        print_inorder_bst(root);
        printf("\n");
        print_postorder_bst(root);
        printf("\n");
        break;
      case 3:
        terminal_flag = true;
        continue;
      default:
        fprintf(stderr, " ERROR: wrong operator\n");
        continue;
    }
  }

  exit(EXIT_SUCCESS);
}
Exemplo n.º 2
0
int main()
{
    int i, key;
    link root = NULL;
    srand(time(NULL));
    for (i = 0; i < N; i++)
        root = insert_bst(root, rand() % RANGE);

    printf("\t\\tree");
    print_bst(root);
    printf("\n\n");
    while (root) {
        key = rand() % RANGE;
        if (search_bst(root, key)) {
            printf("delete %d in tree\n", key);
            root = delete_bst(root, key);
            printf("\t\\tree");
            print_bst(root);
            printf("\n\n");
        }
    }
    
    return 0;
}