BinaryTreeNode* bstFind(BinaryTreeNode * root, double x)
{
	if(root == NULL)
		return NULL;
	else
	{	
		printf("%.2f\n", root->key);
		if(root->key == x)
			return root;
		else
			if(root->key > x)
				return bstFind(root->leftChild, x);
			else 
				return bstFind(root->rightChild, x);
	}
}
Example #2
0
/*
 * Attempts to add the element to the set. If the element is already present in the set, then it is
 * not added. If the element was added, then the size of the set will be incremented by 1.
 *
 * Arguments:
 * set     -- The set to add the element to
 * element -- The element to add to the set
 */
void setAdd( Set *set, void *element ) {
    if( element ) {
        if( ! bstFind(set->elements, element) ) {
            bstInsert( set->elements, element );
            set->size += 1;
        }
    }
}
Example #3
0
Bst& 
MeChart::
recordedBPGH(Item* itm, BstMap& atm, FullHist* h)
{
  int subfv[MAXNUMFS];
  int i;
  for(i = 0 ; i < MAXNUMFS ; i++) subfv[i] = -1;

  if(!itm->term()->terminal_p())
    {
      getHt(h, subfv);
    }
  CntxArray ca(subfv);
  return bstFind(ca, atm);
}
void main()
{
	BinaryTreeNode* node;
	BinaryTreeNode* root = (BinaryTreeNode*) malloc(sizeof(BinaryTreeNode));
	root->key = 5.5;
	root->leftChild = NULL;
	root->rightChild = NULL;
	bstInsert(root, 7.7);
	bstInsert(root, 3.2);
	bstInsert(root, 4.0);
	
	node = bstFind(root, 3.2);
	if(node != NULL)
	       printf("The value %.2f was found.\n", node->key);
	else
	       printf("Value 3.2 was not found.\n");
	       
	/*node = bstMin(root);
	if(node != NULL)
	       printf("The minimum value is %f.\n", node->key);
	node = bstMax(root);
	if(node != NULL)
	       printf("The maximum value is %f.\n", node->key);
	root = bstDelete(root, 3.2);
	node = bstFind(root, 3.2);
	if(node != NULL)
	       printf("The value %f was found.\n", node->key);
	else
	       printf("Value 3.2 was not found.\n";
	// delete the root
	root = bstDelete(root, 7.7);
	printf("Height of binary search tree is %d.\n", bstHeight(root)); bstClear(root);
*/

	bstPrint(root);
	printf("Height of binary tree is %d.\n", bstHeight(root));
}
Example #5
0
/*
 * Determines whether or not the provided item is in the set
 *
 * Arguments:
 * set     -- The set to search through
 * element -- The element to check the set for
 *
 * Returns:
 * True if the element is in the set, false otherwise
 */
bool isInSet( Set *set, void *element ) {
    return bstFind( set->elements, element ) != NULL;
}
Example #6
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);
   }
}