Пример #1
0
// prints BST in-order
void printBST(struct node * n) {
    if(n) {
        printBST((*n).left);
        printf("%d ",(*n).data);
        printBST((*n).right);
    }
}
Пример #2
0
int printBST(struct BST * root){

    if(root==NULL)
        return 0;

    printBST(root->left);
    printf("%d ",root->data);
    printBST(root->right);

    return 1;
}
Пример #3
0
int main(int argc, char** argv){
	BSTree* tree = allocBST(sizeof(int), &releasePrimitive, &compareInt);
	BSTree* balancedTree = allocBST(sizeof(int), &releasePrimitive, &compareInt);
	setPrinterBST(tree, &printInt);

	setPrinterBST(balancedTree, &printInt);

	printBST(stdout, tree);
	printf("\n");
	int i = 0;

	//below array will result in unbalanced tree height 3
	//int elements[] = {15, 7, 83, 5, 11, 5};
	// below array will result in balanced tree height 2
	int elements[] = {7, 5, 15, 5, 11, 83};


	//add will put elements on the tree in the exact order of the array.
	for(i = 0; i < sizeof(elements) / sizeof(int); ++i){
		addBST(tree, &elements[i]);
	}
	printBST(stdout, tree);
	printf("\nHEIGHT = %d\n", heightBST(tree));

	if (isBalanced(tree) == true) {
		printf("The tree is balanced\n");
	}
	else {
		printf("The tree is not balanced\n");
	}

	ArrayList* array = BST2Array(tree);
	setReleaseDArray(array, tree->releaser);
	setPrinterDArray(array, tree->printer);
	printDArray(stdout, array);
	balancedTree = allocBSTBalanced(array);
	printBST(stdout, balancedTree);
	printf("\n");
	releaseDArray(array);

	//still not releasing the tree, work pending
	releaseBST(tree);
	releaseBST(balancedTree);
	system("pause");
	return 0;
}
Пример #4
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;
}
Пример #5
0
int main() {
    // prepare binary search tree (BST)
    struct node * root = prepBST();
    printf("node: ");
    printBST(root);
    printf("\n");

    return 0;
}