예제 #1
0
void printParenthesis(binTree *root)
{
    if (root == NULL)
    {
        return;
    }
    printf("( %d ", root->value);
    if(root->right)
    {
        printParenthesis(root->right);
    }
    else
    {
        printf(" null ");
    }
    printf(" ");
    if(root->left)
    {
        printParenthesis(root->left);
        printf(" ) ");
    }
    else
    {
        printf(" null) ");
    }
    return;
}
/* driver program to test above functions */
int main()
{
  int n = 4;
  printParenthesis(n);
  getchar();
  return 0;
}
void printParenthesis(int pos, int n, int open, int close){
	static char str[100];
	if (close == n){
		printf("%s \n", str);
		return;
	}

	if (open > close){
		str[pos] = '}';
		printParenthesis(pos + 1, n, open, close + 1);
	}
	if (open < n){
		str[pos] = '{';
		printParenthesis(pos + 1, n, open + 1, close);
	}
}
예제 #4
0
int main()
{
    binTree* root = NULL;
    int i = 0;
    while(i == 0)
    {
        char c;
        int cur;
        scanf("%c", &c);
        switch (c)
        {
            case 'a':
                scanf("%d", &cur);
                root = addElement(root, cur);
            break;
            case 'r':
                scanf("%d", &cur);
                root = removeElement(root, cur);
            break;
            case 'i':
                scanf("%d", &cur);
                if(hasElement(root, cur) == 0)
                {
                        printf("Element doesn't belong to tree\n");
                }
                else
                {
                        printf("Element belongs to tree\n");
                }
            break;
            case 'p':
                printParenthesis(root);
                printf("\n");
            break;
             case 'u':
                printUp(root);
                printf("\n");
            break;
             case 'd':
                printDown(root);
                printf("\n");
            break;
            case 'c':
                clearTree(root);
                i = 1;
            break;
        }
    }
    return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
	int n = 3;
	printParenthesis(0, n, 0, 0);
	return 0;
}