Question: How to Merge Two Binary 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 * merge_tree(tree_node_type *t1 , tree_node_type *t2); 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_tree(head1 , head2); printf("The merged tree is\n"); in_order_traversal(head3); printf("\n"); } tree_node_type * merge_tree(tree_node_type *t1 , tree_node_type *t2) { tree_node_type *temp; if(t1==NULL) return t2; if(t2==NULL) return t1; temp=t1; while(temp->left) temp=temp->left; temp->left=t2; return t1; } 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 How to Merge Two Binary Search Tree