Exemple #1
0
int main(void) {
    bst b = bst_new();

    printf("inserting d,b,f,a,c,e,g\n");
    b = bst_insert(b, "d");
    b = bst_insert(b, "b");
    b = bst_insert(b, "f");
    b = bst_insert(b, "a");
    b = bst_insert(b, "c");
    b = bst_insert(b, "e");
    b = bst_insert(b, "g");

    printf("inorder traversal\n");
    bst_inorder(b, print_key);

    printf("preorder traversal\n");
    bst_preorder(b, print_key);

    printf("searching\n");
    dosearch(b, "f");
    dosearch(b, "o");
    dosearch(b, "x");
    dosearch(b, "e");
    dosearch(b, "d");
   
    bst_free(b);
    return EXIT_SUCCESS;
}
Exemple #2
0
bst bst_dup(bst B)
{
  tree copy = tree_dup(B->root);
  bst result = bst_new();
  result->root = copy;
  return result;
}
Exemple #3
0
int main(int argc, char const* argv[])
{
    /* create a new city bst */
    bst_t* bst = bst_new(city_free, city_cmp_pol);

    /* read data and insert into bst */
    int read = 0;
    char name_buf[256];
    char country_buf[256];
    int population;
    while (scanf("%[^,],%[^,],%d\n", name_buf, country_buf, &population) == 3) {
        city_t* new_city = city_new(name_buf, country_buf, population);
        int ret = bst_insert(bst, new_city);
        if (ret != BST_SUCCESS) {
            printf("error inserting city. duplicate?");
            city_free(new_city);
        }
        read++;
    }
    printf("parsed %d cities\n", bst->num_elements);
    assert(bst->num_elements == read);

    /* print all cities */
    bst_traverse(bst, BST_INORDER, city_print);

    /* find the largest */
    node_t* largest_city_node = bst_max(bst);
    if (largest_city_node) {
        printf("largest city is:\n");
        city_print(largest_city_node->data);
    }

    return 0;
}
Exemple #4
0
int main()
{
  bst B = bst_new();
  int *x = malloc(sizeof(int));
  *x = 1;
  bst_insert(B, (void*)x);
  bst copy = bst_dup(B);
  // Make sure copy is actually a completely different bst
  // than B in memory
  printf("orig adr - %p, new adr - %p\n", B, copy);

  // Make sure contents are the same
  printf("contents of orig bst\n");
  print_inorder(B);
  printf("\n\n");
  printf("contents of copy bst\n");
  print_inorder(copy);

  int *b = malloc(sizeof(int));
  int *c = malloc(sizeof(int));
  *b = 2;
  *c = 3;
  bst_insert(B, (void*)b);
  bst_insert(B, (void*)c);

  bst copy2 = bst_dup(B);
  printf("orig adr - %p, new adr - %p\n", B, copy2);
  printf("contents of orig bst\n");
  print_inorder(B);
  printf("\n\n");
  printf("contents of copy2 bst\n");
  print_inorder(copy2);

  free(x);
  free(b);
  free(c);
  return 0;
}
Exemple #5
0
void test()
{
  // tests for bst_new()
  bst B = bst_new(&elem_compare);
  printf("bst_new() passes tests\n");

  // tests for bst_insert()
  int size = 5;
  int i = 0;
  for (i = 0; i < size; i++) {
    wcount w = (wcount) xmalloc(sizeof(struct wcount));
    w->word = (char*) xmalloc(size);
    sprintf(w->word,"%d", i);
    w->count = i;
    bst_insert(B, w);
  }
  printf("bst_insert() passes tests\n");
  print_bst(B);

  // tests for bst_lookup()
  //ASSERT(B == NULL);
  return;
}