int main(int argc, char* argv[]){ 
	struct BinarySearchTree tree;
	BSTinit(&tree);

	/*Reading from a file*/
	FILE * f = fopen("animals.txt", "r");;
	char buffer[LINELENGTH];
	if (!f){
		BSTadd(&tree,"cat");
	}
	else{
		tree.root = readATree(f);
		tree.size = count_tree(tree.root);
		fclose(f);
	}

	struct node * current = tree.root;
	while(current){
		if(current->left || current->right){
			printf("%s\n", current->value);
			fgets(buffer,sizeof(buffer),stdin);
			if(toupper(buffer[0]) == 'Y')
				current = current->left;
			else{
				assert(toupper(buffer[0])=='N');
				current=current->right;
			}
		}
		else{ //if(!current->left && !current->right)
			printf("Is %s the animal?\n", current->value);
			fgets(buffer, sizeof(buffer),stdin);
			buffer[strlen(buffer)-1]='\0';
			if(toupper(buffer[0]) == 'Y'){
				printf("I WIN!!\n");
	                	current = tree.root;   // restart game at the top of the tree
			}
			else{
				printf("I'm sorry, I don't know your animal.\n"
						"What animal were you thinking of?\n");
				fgets(buffer, sizeof(buffer),stdin);
				buffer[strlen(buffer)-1]='\0';
				current->right = newNode(current->value,0,0);
				current->left = newNode(strdup(buffer), 0,0);
				printf("Please enter a yes/no question which is true of a %s but not true of a %s?\n",current->left->value, current->right->value);
				fgets(buffer, sizeof(buffer),stdin);
				buffer[strlen(buffer)-1]='\0';
				printf("Thank you!\n");
				current->value = strdup(buffer);
				f = fopen("binary_search_tree.txt", "w");;
				assert(f);
				writeATree(f,&tree);
				fclose(f);
				exit(0);
			}
		}
	}
	return 0;
}
Ejemplo n.º 2
0
rbtree* nuovo(){
	rbtree* mosaico = createrbtree();
	adm_positions = BSTinit();
	mosaico->count = 0;
	mosaico->xdown=0;
	mosaico->yup=0;
	mosaico->ydown=0;
	mosaico->yup=0;
	mosaico->ordine = 1;
	adm_positions = BSTinsert( adm_positions, 0, 0 );
	rbinsert(mosaico,0,0);
	mosaico->perimetro = 4;
	return mosaico;
}