int create_bst(struct node *temp_root, int val)
{
	struct node *temp;

	if (temp_root == NULL) {
		temp_root = (struct node *)malloc(sizeof(struct node));
		root = temp_root;
		root->data = val;
		return 0;
	}
	
	if (temp_root->data >= val) {
		if (temp_root->right != NULL) {
			create_bst(temp_root->right, val);
		} else {
			temp = (struct node *)malloc(sizeof(struct node));
			temp->data = val;
			temp->left = NULL;
			temp->right = NULL; 
			return 0;
		}
	} else {
		if (temp_root->left != NULL) {
			create_bst(temp_root->left, val);
		} else {
			temp = (struct node *)malloc(sizeof(struct node));
			temp->data = val;
			temp->left = NULL;
			temp->right = NULL; 
			return 0;
		}

	}
	return -1;
}
int main (void)
{
    std::vector<int> A(10, 0);
    A[0] = 10;
    A[1] = 5; 
    A[2] = 7;
    A[3] = 20;
    A[4] = 3;
    A[5] = 15;
    A[6] = 17;
    A[7] = 17;
    A[8] = 100;
    A[9] = 0;
    std::unique_ptr<BSTN<int>> root = create_bst(A);
    int a = 17;
    auto x = find_first_no_rec(root, a);
    auto y = find_first_equal (root, a);
    assert (x == y);

    x = find_first_no_rec(root, 100);
    y = find_first_equal(root, 100);
    assert (x == y);

    x = find_first_no_rec(root, 1000);
    y = find_first_equal(root, 1000);
    assert (x == y);
    return 0;

}
void create_bst (struct bst **bstroot,char item)
{
if (*bstroot==NULL)
{
*bstroot=(struct bst*)malloc(sizeof (struct bst));
(*bstroot)->lchild=NULL;
(*bstroot)->rchild=NULL;
(*bstroot)->data=item;
}
else
{
if (item<(*bstroot)->data)
create_bst (& ((*bstroot) ->lchild), item);
if (item>(*bstroot)->data)
create_bst (& ( (*bstroot) ->rchild), item);
}
}
int main() {
  struct binary_tree* bst = create_bst();
  insert(bst->root, 3, "Foot");
  insert(bst->root, 1, "Stewart");
  insert(bst->root, 2, "Grace");
  insert(bst->root, 4, "Job");
  insert(bst->root, 5, "Burrito");
  traverse(bst->root, 0);
  return 0;
}
Beispiel #5
0
int main() {
	srand(time(NULL));
	node* tree = create_bst(40);
	for(uint i = 0; i < 10; ++i) {
		int elem = rand() % 100 + 1;
		insert(&tree, elem);
	}
	display_tree(tree);
	printf("Max Root to Leaf sum is %d.", max_path(tree));
	return 0;
}
void main(int argc, char *argv[])
{
	int arr_size=argc-1;
	int count;
	int arr[arr_size];
	for(count=0;count<=arr_size-1;count++)
		arr[count]=atoi(argv[count+1]);
	bstn *root=create_bst(arr, arr_size);
	preorder_traverse(root, -1);
	print_nodes_levelwise();
}
Beispiel #7
0
int main() {
	srand(time(NULL));
	node* tree = create_bst(40);
	for(uint i = 0; i < 10; ++i) {
		int elem = rand() % 100 + 1;
		insert(&tree, elem);
	}
	insert(&tree, 1337);
	display_tree(tree);
	node* test = search(tree, 1337);
	printf("Grandparent of 1337 is: %d", get_grandparent(tree, test)->data);
	return 0;
}
Beispiel #8
0
int main( )
{
	int		depth	   = 0;
	Bst		* bst	   = create_bst( show_string, NULL, strcmp );
	char	a[10][10]  = { "2", "8", "1", "3", "9", "4", "7", "5", "6", "0" };
	char	*b[10];
	int		i;
	int		ret;

	for( i = 0; i < 10; i++ )
	{
		b[i] = a[i];
	}

	bst_insert( &bst, b[0] );
	bst_insert( &bst, b[1] );
	bst_insert( &bst, b[2] );
	bst_insert( &bst, b[3] );
	bst_insert( &bst, b[4] );
	in_order( bst );
	ret = bst_search( bst, b[2] );
	printf( "search result %d\n", ret );

	ret = bst_delete( &bst, b[0] );
	printf( "0 in order after delete %d\n", ret );
	in_order( bst );

	ret = bst_delete( &bst, b[1] );
	printf( "1 in order after delete %d\n", ret );
	in_order( bst );

	ret = bst_delete( &bst, b[2] );
	printf( "2 in order after delete %d\n", ret );
	in_order( bst );
	printf("bst %p\n", bst);

	destroy_bst(&bst);
	
	printf("bst %p\n", bst);

	ret = bst_delete( &bst, b[3] );
	printf( "3 in order after delete %d\n", ret );
	in_order( bst );

	ret = bst_delete( &bst, b[4] );
	printf( "4 in order after delete %d\n", ret );

	in_order( bst );

}
void main ( )
{
clrscr ( );
create_bst (&root, 'A');
create_bst (&root, 'B');
create_bst (&root, 'a');
create_bst (&root, 'b');
create_bst (&root, 'C');
create_bst (&root, 'c');
create_bst (&root, 'D');
create_bst (&root, 'd');
create_bst (&root, 'E');
clrscr ( ) ;
printf ("\nTraversing bst into Inorder :\n");
inorder_traverse (root);
printf ("\nTraversing bst into Preorder :\n");
preorder_traverse (root);
printf ("\nTraversing bst into Postorder:\n");
postorder_traverse (root);
getch ( );
}
Beispiel #10
0
void main(int argc, char *argv[])
{
	int arr_size=argc-1;
	int count;
	int arr[arr_size];
	for(count=0;count<=arr_size-1;count++)
		arr[count]=atoi(argv[count+1]);
	bstn *root=create_bst(arr, arr_size);
	//bstn *ancestors[100]={NULL};
	printf("Enter element to find its ancestors\n");
	int element;
	scanf("%d", &element);
	int element2;
	printf("Enter element to find common ancestor\n");
	scanf("%d", &element2);
	print_node_ancestor(root, element, element2);
}
int main (void)
{
    std::vector<int> A(10, 0);
    A[0] = 10;
    A[1] = 5; 
    A[2] = 7;
    A[3] = 20;
    A[4] = 3;
    A[5] = 15;
    A[6] = 17;
    A[7] = 17;
    A[8] = 100;
    A[9] = 0;
    std::unique_ptr<BSTN<int>> root = create_bst(A);
    auto x = get_lca_bst(root, 0, 7);
    std::cout << x->data << std::endl;
    return 0;
}
int main()
{

	create_bst(root, 10);
	create_bst(root, 7);
	create_bst(root, 15);
	create_bst(root, 20);
	create_bst(root, 25);
	create_bst(root, 26);
	
	in_order(root);

}
Beispiel #13
0
int main() {
	srand(time(NULL));
	node* tree = create_bst(40);
	for(uint i = 0; i < 10; ++i) {
		int elem = rand() % 100 + 1;
		insert(&tree, elem);
	}
	display_tree(tree);
	
	int input;
	do {
		printf("Enter value to delete: ");
		scanf("%d", &input);
		delete_elem(&tree, input);
		display_tree(tree);
	} while(input != -1);
	
	return 0;
}