Пример #1
0
int main() {
	// Simple example of usage 
	bsTree bst = bstCreate();
	bstSetCompare(bst, compare);

	int key_data[12] = {38, 13, 51, 10, 12, 40, 84, 25, 89, 37, 66, 95};
	int *keys[12];
	char *data[12];
	char data_count = 'a';

	for (int i = 0; i < 12; i++) {
		keys[i] = malloc(sizeof(**keys));
		data[i] = malloc(sizeof(**data));
		assert(keys[i] != NULL);
		assert(data[i] != NULL);
		*(keys[i]) = key_data[i];
		*(data[i]) = data_count++;
		printf("key = %d data = %c\n", *keys[i], *data[i]);
		bstInsert(bst, keys[i], data[i]);
	}

	printf("\ntree: ");
	bstPrintKeys(bst);
	printf("\n");


	printf("Checking tree data:\n");
	for (int i = 0; i < 12; i++) {
		assert(bstSearch(bst, keys[i]) == data[i]);
	}
	printf("Success\n\n");

	// remove key of root from tree
	// comparison to find key in tree is done using compare 
	// function defined above so no need to use original key
	// pointer
	int root = 38;
	printf("Deleting root (key = %d):\n", root);
	bstDelete(bst, &root);
	printf("tree: ");
	bstPrintKeys(bst);
	assert(bstSearch(bst, &root) == NULL);
	printf("Success\n\n");

	// key of what should be the new root
	// it is at index 5 of key_data so should have data at
	// index 5 of data array
	int new_root = 40;
	printf("Checking new root (key = %d):\n", new_root);
	assert(bstSearch(bst, &new_root) == data[5]);
	printf("Success\n");

	bstSetFreeData(bst, free);
	bstSetFreeKey(bst, free);
	bstDestroy(bst);

	return 0;
}
Пример #2
0
void main()
{
   short done=0;
   int newnum;
   char ch;
   char buffer[256];
   ITEM* newitem;
   BST bst;
   bstNode* node;
   ITEM tempitem;
   bstInit(&bst);
   bstSetCompareFunc(&bst,compare);
   while(!done)
   {
      fprintf(stderr,"(1) Add Item\n(2) Lookup\n(3) Remove Item\n");
      ch=getchar();
      fflush(stdin);
      switch(ch)
      {
         case '1':    
           newitem=(ITEM*)malloc(sizeof(ITEM));
           printf("\nEnter a string\n");
           scanf("%s",buffer);
           newitem->data=strdup(buffer);
           printf("Enter key\n");
           scanf("%d",&newnum);
           newitem->key=newnum;
           printf("inserting at %d\n",newnum);
           bstInsert(newitem,&bst);
           break;
         case '2':
           printf("Enter key\n");
     
           scanf("%d",&newnum);
           printf("Looking for %d\n",newnum);
           tempitem.key=newnum;
           node=bstFind(&tempitem,&bst);
           if(node != NULL)
           {
              newitem=(ITEM*)bstGetInfo(node);
              printf("found %s\n",newitem->data);
           }
           else
           {
              printf("Item not found\n"); 
           }
           break;
         case '3':
           printf("Enter key\n");
           scanf("%d",&newnum);
           tempitem.key=newnum; 
           node=bstFind(&tempitem,&bst);
           if(node != NULL)
           {
              newitem=(ITEM*)bstGetInfo(node);
              printf("removing %s\n",newitem->data);
              bstDelete(node,&bst);
           }
           else
           {
              printf("Item not found\n"); 
           }
           break;
         default:done=1;
      }
      fflush(stdin);
   }
}