Question: How to Merge Two Binary Search Tree?

#include<stdio.h>
#include<stdlib.h>

typedef struct tree_node_tag
{
    int data;
    struct tree_node_tag *left;
    struct tree_node_tag *right;
}tree_node_type;

tree_node_type* append(tree_node_type **head , int data);
void create_binary_tree(tree_node_type **head);
void visit(tree_node_type *head);
void in_order_traversal(tree_node_type *head);
void post_order_traversal(tree_node_type *head);
tree_node_type *insert_node_in_tree(tree_node_type *t1 , tree_node_type *t2);
void merge_tree(tree_node_type *t1 , tree_node_type *t2);
tree_node_type * merge_2_BST(tree_node_type *t1 , tree_node_type *t2);
void pre_order_traversal(tree_node_type *head);

main()
{
    tree_node_type *head1;
    tree_node_type *head2;
    tree_node_type *head3;
    create_binary_tree(&head1);
    printf("\n");
    in_order_traversal(head1);
    printf("\n");
    create_binary_tree(&head2);
    printf("\n");
    in_order_traversal(head2);
    printf("\n");
    head3=merge_2_BST(head1 , head2);
    printf("The merged tree is\n");
    in_order_traversal(head3);
    printf("\n");
    printf("The merged tree in pre order\n");
    pre_order_traversal(head3);


}



tree_node_type * merge_2_BST(tree_node_type *t1 , tree_node_type *t2)
{
    if(t1==NULL)
        return t2;
    if(t2==NULL)
        return t1;
	merge_tree(t1 , t2);
    return t1;
}
        

void merge_tree(tree_node_type *t1 , tree_node_type *t2)
{
	if(t2)
	{
		merge_tree(t1 , t2->left);
		merge_tree(t1 , t2->right);
		insert_node_in_tree(t1 , t2);

	}
}
        

tree_node_type *insert_node_in_tree(tree_node_type *t1 , tree_node_type *t2)
{
	if(t1==NULL)
	{
		t2->left=NULL;
		t2->right=NULL;
		return t2;
	}
	if(t2->data<t1->data)
	{
		t1->left = insert_node_in_tree(t1->left , t2);
		return t1;
	}
	else if(t2->data > t1->data)
	{
		t1->right = insert_node_in_tree(t1->right , t2);
		return t1;
	}
	else
	{
		printf("Duplicate , here you can delete the node\n");
		free(t2);
		return NULL;
	}
}


tree_node_type* append(tree_node_type **head , int data)
{
    if(*head == NULL)
    {
        *head = (tree_node_type *)malloc(sizeof(tree_node_type));
        (*head)->data = data;
        (*head)->left = NULL;
        (*head)->right = NULL;
        return *head;
    }
    else if((*head)->data > data)
    {
        (*head)->left = append( &(*head)->left , data );
        return *head;
    }
    else if( (*head)->data < data )
    {
        (*head)->right = append( &(*head)->right , data );
        return *head;
    }
    else
    {
        printf("Duplicate data in Binary Tree\n");
        return *head;
    }
}

void create_binary_tree(tree_node_type **head)
{
    int i , n;
    int array[50];

    printf("Give the number of elements in array\n");
    scanf("%d", &n);
    printf("Enter the elements of binary tree\n");

    *head = NULL;

    for(i = 0 ; i<n ; i++)
    {
        scanf("%d", &array[i]);    
        append(head , array[i]);
    }
}




void visit(tree_node_type *head)
{
    printf("%d ",head->data);
}

void in_order_traversal(tree_node_type *head)
{
    if(head != NULL)
    {
        in_order_traversal(head->left);
        visit(head);
        in_order_traversal(head->right);
    }
}


void pre_order_traversal(tree_node_type *head)
{
    if(head != NULL)
    {
        visit(head);
        pre_order_traversal(head->left);
        pre_order_traversal(head->right);
    }
}

void post_order_traversal(tree_node_type *head)
{
    if(head != NULL)
    {
        post_order_traversal(head->left);
        post_order_traversal(head->right);
        visit(head);
    }
}


also see Merge Two Tree (only Ordinary binary tree)

Free Web Hosting