int countTrees(int numKeys) {
int count=0;
if(numKeys == 1 || numKeys == 0)
  return 1;
if(numKeys == 2)
  return 2;
for(int i=1;i<=numKeys;i++) {
count += countTrees(numKeys - i) * countTrees(i - 1);
}

return count;
}
Exemple #2
0
/*
* For the key values 1...numKeys,
* how many structurally unique binary search trees are possible
* that store those keys.
*
* Strategy: consider that each value could be the root.
*/
int countTrees(int numKeys){
   if (numKeys <= 1){
       return 1;
   }else{
       int sum = 0;
       int left, right, root;
       for (root = 1; root <= numkeys; ++root){
           left = countTrees(root - 1);
           right = countTrees(numKeys - root);
           // number of possible trees with this root = left*right
           sum += left*right;
       }
       return sum;
   }
}
Exemple #3
0
void CATrees() {
	for (int y = 0; y < torch.buf.geth(); y++) {
		for (int x = 0; x < torch.buf.getw(); x++) {
			if (!game.map.solid(x,y) && (rand4() < 6)) set_tile(x,y, TREE);
		}
	}
	bool *next = new bool[torch.buf.geth()*torch.buf.getw()];
	for (int i = 0; i < 4; i++) {
		memset(next, 0, torch.buf.geth()*torch.buf.getw()*sizeof(bool));
		for (int y = 0; y < torch.buf.geth(); y++) {
			for (int x = 0; x < torch.buf.getw(); x++) {
				if (countTrees(x,y,2) <= 3 || countTrees(x,y,1) >= 5) next[y*torch.buf.getw()+x] = true;
				else next[y*torch.buf.getw()+x] = false;
			}
		}
		for (int y = 0; y < torch.buf.geth(); y++) {
			for (int x = 0; x < torch.buf.getw(); x++) {
				if (!game.map.solid(x,y) || game.map.at(x,y)->type == TREE) {
					if (next[y*torch.buf.getw()+x])
						set_tile(x,y, TREE);
					else
						set_tile(x,y, GROUND);
				}
			}
		}
	}
	for (int i = 0; i < 3; i++) {
		memset(next, 0, torch.buf.geth()*torch.buf.getw()*sizeof(bool));
		for (int y = 0; y < torch.buf.geth(); y++) {
			for (int x = 0; x < torch.buf.getw(); x++) {
				if (countTrees(x,y,1) >= 5) next[y*torch.buf.getw()+x] = true;
				else next[y*torch.buf.getw()+x] = false;
			}
		}
		for (int y = 0; y < torch.buf.geth(); y++) {
			for (int x = 0; x < torch.buf.getw(); x++) {
				if (!game.map.solid(x,y) || game.map.at(x,y)->type == TREE) {
					if (next[y*torch.buf.getw()+x])
						set_tile(x,y, TREE);
					else
						set_tile(x,y, GROUND);
				}
			}
		}
	}
	delete [] next;
}
Exemple #4
0
int countTrees(int n){

    if(n<=1){
        return 1;
    }
    else{
        int sum, left,right, root;
        sum = 0;
        for(root=1;root<=n;root++){
            left = countTrees(root-1);
            right =countTrees(n-root);

            sum += left*right;
        }
        return sum;
    }
}
Exemple #5
0
int main()
{
//    printf("Hello world!\n");
/*
          4
        /   \
       2     8
           /   \
          5     10
           \
            7
           /
          6

*/
    struct BST* root = newNode(4);
    root->left = newNode(2);
    root->right = newNode(8);
    root->right->left = newNode(5);
    root->right->right = newNode(10);
    root->right->left->right = newNode(7);
    root->right->left->right->left = newNode(6);

    struct BST *temp;
    int a = -1;
//    int *p = &a;
/*
    if(checkForBST(root,&a)){
        printf("Tree is BST\n");
    }
    else{
        printf("Tree is not BST\n");
    }

    a= 3;

    temp = kthSmallestElement(root,&a);
    printf("Kth Smallest Element : %d\n",temp->data);

    temp = inOrderSuccessor(root,root->right->left->right);
    printf("Inorder Successor of 7:: %d\n",temp->data);

    deleteNode(root,8);

    temp = inOrderSuccessor(root,root->right->left->right);

    printf("Inorder Successor of 7:: %d\n",temp->data);
*/

    printBST(root);
    printf("\n");
    temp = TrimBST(root,3,7);
    //printBST(temp);


    printf("\nNumber of Trees with 3 Nodes : %d\n",countTrees(3));
    return 0;
}
/* 
 For the key values 1...numKeys, how many structurally unique 
 binary search trees are possible that store those keys. 
 Strategy: consider that each value could be the root. 
 Recursively find the size of the left and right subtrees. 
*/ 
int countTrees(int numKeys) { 

  if (numKeys <=1) { 
    return(1); 
  } 
  else { 
    // there will be one value at the root, with whatever remains 
    // on the left and right each forming their own subtrees. 
    // Iterate through all the values that could be the root... 
    int sum = 0; 
    int left, right, root; 

    for (root=1; root<=numKeys; root++) { 
      left = countTrees(root - 1); 
      right = countTrees(numKeys - root); 

      // number of possible trees with this root == left*right 
      sum += left*right; 
    } 

    return(sum); 
  } 
} 
int main()
{

#if 0
        int a[] = {2, 4, 1, 8, 3, 5, 9, 6, 7};
        tree_t root = NULL;

        for(int i = 0; i < 9; ++i)
        {
                root = insert(root,a[i]);
        }

        display(root);

        printf("----------------------\n");
#endif
#if 0
        printf("In-order traversal \n");
        display(root);

        printf("No of nodes: %d\n", count(root));

        printf("----------------------\n");
        printf("No of leaf nodes: %d\n",count_leaf(root));

        printf("----------------------\n");
        printf("Pre-order traversal\n");
        post_traversal(root);
        printf("----------------------\n");
        printf("Post-order traversal\n");
        pre_traversal(root);
        printf("----------------------\n");

        printf("Height of the tree is: %d\n", height(root));
        printf("Least value in tree is: %d\n", least_of_tree(root));
        printf("Max value of tree is: %d\n", max_of_tree(root));

        int height_ = height(root);
        int vertical_sum[10000];
        for(int i = 0; i < 10000; ++i)
        {
                vertical_sum[i] = 0;
        }
        /*
                I assumed Vertical sum for root is 1... :P if there are more no of nodes on the
                left subtree of the root..they we cant implement this using arrays in c..as negative indices is not possible
        */

        verticalSumArray(root,vertical_sum,1);
        for(int i = 0;vertical_sum[i] != 0 ; ++i)
        {
                printf("%d : %d \n",i,vertical_sum[i]);
        }

        // tree_t mirrored_root = mirror_tree(root);
        printf("----------------------\n");
        // display(mirrored_root);
        //printf("%d\n", RLMirrors(root));
        //inorder(root);

        tree_t largest = largestSumSubtree(root);
        printf("%d\n",largest -> key);

        int key = 1;
        if(search(root,key) != NULL)
        {
                printf("%d is present\n",key);
        }
        else
        {
                printf("%d is absent\n",key);
        }

        iterativeInorder(root);

        printf("----------------------\n");

        iterativePreorder(root);


        printf("----------------------\n");
        iterativePostorder(root);

        levelorder(root);

        int height = 0;
        printf("the diameter of the tree: %d\n",diameter(root));

        int key = 9;
        printAncestorsI(root,key);

        int k = 3;
        printKdistance(root,k);

        int sum = 36;
        if(hasSumPath(root,sum) == 1)
        {
                printf("Yes\n");
        }
        else
        {
                printf("No\n");
        }

        root  = getNode(10);
        root -> llink = getNode(8);
        root -> rlink = getNode(2);
        root -> llink -> llink = getNode(3);
        root -> llink -> rlink = getNode(5);
        root -> rlink ->rlink = getNode(2);

        display(root);
        printf("----------------------\n");
        if(isSum(root))
        {
                printf("Yes\n");
        }
        else
        {
                printf("No\n");
        }

        root  = getNode(20);
        root -> llink = getNode(8);
        root -> rlink = getNode(22);
        root -> llink -> llink = getNode(4);
        root -> llink -> rlink = getNode(12);
        root -> llink -> rlink -> rlink = getNode(14);
        root -> llink -> rlink -> llink = getNode(10);

        int n1 = 4;
        int n2 = 14;
        printf("Least common ancestor of %d & %d is %d \n", n1,n2,lowestCommonAncestor(root,n1,n2));
        printf("\n");
#endif
        // printPaths(root);
#if 0
        int num = 4;
        printf("The no of bst for %d keys is %d\n", num,countTrees(num));i
#endif
#if 0
        root = getNode(20);
        root -> llink = getNode(8);
        root -> rlink = getNode(22);
        root -> llink -> llink = getNode(4);
        root -> llink -> rlink = getNode(12);
        root -> llink -> rlink -> llink = getNode(10);
        root -> llink -> rlink -> rlink = getNode(14);

        tree_t successor = inorderSuccessor(root,root -> llink);
        if(successor != NULL)
        {
                printf("thei inorder successor is %d\n",successor -> key);
        }
#endif
#if 0
        root = getNode(20);
        root -> llink = getNode(8);
        root -> rlink = getNode(22);
        root -> llink -> llink = getNode(100);
        if(checkAllLeavesSameLevel(root) == 1)
        {
                printf("All the leaves are in the same level\n");
        }
#endif
#if 0
        display(root);
        printf("-------------------------------------\n");
        replaceNodeWithSum(root,0);
        display(root);
int getSum(tree_t root)
{
        if(root == NULL)
        {
                return 0;
        }
        else
        {
                return root -> key + getSum(root -> rlink);
        }
}
#endif
        // printf("%d\n",countTrees(3));
#if 0
        root = getNode(10);
        root->llink = getNode(20);
        root->rlink = getNode(30);



        if(isBalanced(root) == 1)
        {
                printf("The tree is balanced\n");
        }
        else
        {
                printf("Ther tree is unbalanced\n");
        }
#endif
#if 0
        tree_t root = NULL;
        int array[] = {1,2,3,4,5,6,7,8,9};
        int n = 9;
        root = createMinimalBST(root,array,n);
        display(root);
#endif
//        tree_t root = NULL;
#if 0
        root = getNode(20);
        root -> llink = getNode(8);
        root -> rlink = getNode(22);
        root -> llink -> llink = getNode(4);
        root -> llink -> rlink = getNode(12);
        root -> llink -> rlink -> llink = getNode(10);
        root -> llink -> rlink -> rlink = getNode(14);

        // printPathWithSum(root,40);

        // levelorderRecursive(root);
        // rightView(root);
#endif
#if 0
        int inorder[] = {4, 2, 5, 1, 6, 3};
        int preorder[] = {1, 2, 4, 5, 3, 6};

        int n = 6;
        // int preIndex = 0;
        // root = buildTreeWithInorderAndPreOrder(inorder,preorder,0,n - 1,&preIndex);
        // display(root);

        int index = 0;
        printPostOrder(inorder,preorder,0,n - 1,&index);
#endif
#if 0
        /*
                        10
                    -2      7
                 8      -4

        */
        tree_t root = NULL;
        root = getNode(10);
        root->llink = getNode(-2);
        root->rlink = getNode(7);
        root->llink->llink = getNode(8);
        root->llink->rlink = getNode(-4);
 //       printf("Sum: %d\n",maxSumPath(root));

        pre_traversal(root);
        printf("----------------------------------");
        display(root);
#endif

        return 0;
}
void testCountTrees() {
printf("Number of unique trees with values from 1 to 5 are %d \n",countTrees(5)); 
}