예제 #1
0
파일: binaryTree.c 프로젝트: kiki7/form
//Number node
int numberNode(node *pelt){
	if (pelt == NULL){
		return 0;
	}else{
		return (1 + numberNode(pelt->left) + numberNode(pelt->right));
	}
}
예제 #2
0
AST performAction(AST t)
{
	AST s = t;
	AST ret;
	
	if(s->kind == ACTION_NK)
	{
		AST x = performAction(s->fields.subtrees.s1);
		AST y = applyNode(s->fields.subtrees.s2,x);
		ret = performAction(simplify(y));
	}
	else if(s->kind == BASIC_FUNC_NK)
	{
		if(s->extra == PRILST_FK)
		{
			AST x= simplify(s->fields.subtrees.s1);
			while(x->kind != EMPTYLIST)
			{	
				if(x->kind == ERROR_NK)
					ret= errorNode(x->fields.stringval);
				AST y = applyBasicFunc(x,"head");
				AST tmp = simplify(y);
				printValue(simplify(tmp));
				y = applyBasicFunc(x,"tail");
				x = simplify(y);
			}
			ret = emptyList();
		}
		else if(s->extra == PRINT_FK)
		{
			AST r = simplify(s->fields.subtrees.s1);
			printValue(r);
			ret = emptyList();
		}
		else if(s->extra == PROD_FK)
		{
			ret = simplify(s->fields.subtrees.s1);
		}
		else if(s->extra == READI_FK)
		{
			char str[11];
			if(fgets(str,11,stdin) != NULL)		
				ret = numberNode(atoi(str));
			else
				ret = errorNode("Not a valid number");
		}
		else if(s->extra == READC_FK)
		{
			char str[2];
			if(fgets(str,2,stdin) != NULL)		
				ret = charNode(str);
			else
				ret = errorNode("Not a valid character");
		}
		else
		{
			ret = errorNode("No action specified");
		}
	}
	else if(s->kind == ERROR_NK)
	{
		ret = errorNode(s->fields.stringval);
	}
	else
	{
		ret = errorNode("No action specified");
	}
	return ret;
}
예제 #3
0
파일: binaryTree.c 프로젝트: kiki7/form
int main(void)
{

	int chMenu, chElement, value, addValue, find;
	node *pRootNode = NULL;


	do {
		printf("\n\tBINARY TREE\n");

		printf("\n1) Create binary tree\n");
		printf("2) List binary tree\n");
		printf("3) Delete binary tree\n");
		printf("4) Total node \n");
		printf("5) List tree with values \n");
		printf("6) Insert value\n");
		printf("7) Find value\n");


		printf("\nENTRER VOTRE CHOIX : ");
		scanf("%d", &chMenu);
		printf("\n");

		switch (chMenu) {
		case 1:{
			chElement = 1;
			while (chElement != 0) {
				printf("Entrer la valeur pour l'inserer dans l'arbre:\n");
				scanf("%d", &value);//
				addNode(&pRootNode,value); // on appel addnode avec comme parametre l'adresse de pRootNode, et la valeur
				printf("Voulez vous ajoutez un element?  (1 / 0)\n");
				scanf("%d", &chElement);
			}
			printf("\n");
			break;
		}

		case 2:{
			printf("\n**Display**\n");
			displayTree(pRootNode);// l'adresse de pRootNode
			break;
		}
		case 3:{
			printf("Delete Tree\n");
			deleteTree(pRootNode);
			break;
		}
		case 4:{
			int nb = numberNode(pRootNode);
			printf("Number of node : %d\n", nb);
			break;
		}
		case 5:{
			printf("List with values \n");
			displayTreeWith(pRootNode);
			break;
		}

		case 6 : {
			printf("Insert value");
			scanf("%d",&addValue);
			printf("%d",addValue);
			insert(pRootNode,addValue);
			break;
		}
		case 7 : {
			printf("Find values\n");
			scanf("%d",&find);
			findNode(pRootNode,find);
			break;
		}
		}

	} while (chMenu != 0);

	return 1;
}