Demo entry 6715130

trees

   

Submitted by anonymous on Feb 16, 2018 at 04:49
Language: C. Code size: 576 Bytes.

void print_bst(bst *B) {
    print_in_order(B->root);
}

void print_in_order(tree *T) {
    if(T = NULL) return;
    print_in_order(T->left);
    print_in_order(T->data);
    print_in_order(T->right);
}

int num_trees(int n) {
    if(n == 0) return 1;
    int total = 0;
    for(int i = 0; i < n; i++) {
	/* Fix i as the node. Then, the left subtree reduces 
	   to all the numbers less than i, which is i numbers
           while the right subtree reduces to all the numbers > i, which is n-i numbers
	 */
	total += num_trees(i) * num_trees(n-1-i);
    }
    return total;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).