All pastes #60369 Raw Edit

Mine

public cpp v1 · immutable
#60369 ·published 2006-05-31 18:19 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#define MAX 100typedef struct node{	struct node* left,*right;	int val;}Node;Node* list1[MAX],*list2[MAX],*templist[MAX];int top1 = 0,top2 = 0,temptop = 0;Node* insert(Node* cur,int val){	if(!cur)	{		cur = (Node*)malloc(sizeof(Node));		cur ->left = cur ->right = NULL;		cur ->val = val;			}	else if(val < cur ->val)		cur ->left = insert(cur ->left,val);	else		cur ->right = insert(cur ->right,val);	return cur;}Node* insert(Node* cur,Node* val){	if(!cur)		return val;	if(val ->val < cur ->val)		cur ->left = insert(cur ->left,val);	else		cur ->right = insert(cur ->right,val);	return cur;}void inorder(Node* root,int flag)//1 means BST1 and 2 means BST2{	if(!root)		return;	inorder(root ->left,flag);	//	if(flag == 1)		list1[top1++] = root;	else if(flag == 2)		list2[top2++] = root;	else if(flag == 3)		templist[temptop++] = root;	else		printf("%d ",root ->val);	//	inorder(root ->right,flag);}Node* mergeBST(Node* root1,Node* root2){	Node* dest = NULL;	int list1ind = 0,list2ind = 0;	if(list1[0] ->val < list2[0] ->val)	{		//the final tree is going to be root1		dest = root1;		list1ind++;		while(list1ind < top1 && list2ind < top2)		{			if(list1[list1ind] ->val <= list2[list2ind] ->val)				list1ind++;			else			{				//insert the new element;				list2[list2ind] ->left = list1[list1ind] ->left;				list2[list2ind] ->right = NULL;				list1[list1ind] ->left = list2[list2ind];				list2ind++;			}		}		if(list2ind != top2)		{			//locate the root of the subtree			int i = 0,j = 0,minind;			for(i = list2ind;i < top2;i++)			{				inorder(list2[i],3);				if(top2 - list2ind  == temptop)				{					for(j = 0;j < temptop;j++)						if(templist[j] != list2[j+list2ind])							break;					if(j == temptop)					{						minind = i;						break;					}									}				temptop = 0;			}			dest = insert(dest,list2[minind]);		}					}	else	{		dest = root2;		list2ind++;		while(list1ind < top1 && list2ind < top2)		{			if(list2[list2ind] ->val <= list1[list1ind] ->val)				list2ind++;			else			{				//insert the new element;				list1[list1ind] ->left = list2[list2ind] ->left;				list1[list1ind] ->right = NULL;				list2[list2ind] ->left = list1[list1ind];				list1ind++;			}		}		if(list1ind != top1)		{			//locate the root of the subtree			int i = 0,j = 0,minind;			for(i = list1ind;i < top1;i++)			{				inorder(list1[i],3);				if(top1 - list1ind  == temptop)				{					for(j = 0;j < temptop;j++)						if(templist[j] != list1[j+list1ind])							break;					if(j == temptop)					{						minind = i;						break;					}									}				temptop = 0;			}			dest = insert(dest,list1[minind]);		}	}	return dest;	}int main(){	int arr1[] = {4,2,6,5,1,9};	int arr2[] = {0,14,11,7,3,10};	int val = 6;//no.of.elements in the tree	Node* root1,*root2;	root1 = insert(NULL,arr1[0]);	root2 = insert(NULL,arr2[0]);	int i = 1;	for(i = 1;i < 6;i++)	{		insert(root1,arr1[i]);		insert(root2,arr2[i]);	}	//do the inorder traversal	inorder(root1,1);	inorder(root2,2);	printf("\nTree 1\n");	inorder(root1,0);	printf("\nTree 2\n");	inorder(root2,0);	Node* newroot;	newroot = mergeBST(root1,root2);	printf("\nMergedTree 2\n");	//inorder(root2,0);	inorder(newroot,0);	return 0;}