[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


1.Write a program in 'c' language to merge the contents of...

<< Back to: Binary Space Partitioning Trees FAQ

Question by Rashmi
Submitted on 4/14/2004
Related FAQ: Binary Space Partitioning Trees FAQ
Rating: Rate this question: Vote
1.Write a program in 'c' language to merge the contents of two binary search trees into one.What is the time and storage complexities of your program?

2.Write a program in 'c' language to count the number of internal nodes of a tree.



Answer by shahrukh
Submitted on 4/16/2004
Rating:  Rate this answer: Vote
i want to learn c

 

Answer by Yonique
Submitted on 5/4/2004
Rating: Not yet rated Rate this answer: Vote
How to write a program in C

 

Answer by Dimple
Submitted on 11/1/2004
Rating: Not yet rated Rate this answer: Vote
1.Write a program in 'c' language to merge the contents of two binary search trees into one.What is the time and storage complexities of your program?

2.Write a program in 'c' language to count the number of internal nodes of a tree.


 

Answer by jeyam
Submitted on 11/9/2006
Rating: Not yet rated Rate this answer: Vote
learn c program is easy

 

Answer by sanjay
Submitted on 2/11/2007
Rating: Not yet rated Rate this answer: Vote
How to write a program in C

 

Answer by sonu
Submitted on 4/24/2007
Rating: Not yet rated Rate this answer: Vote
Hi rashmi
          This is u'r ans....you can say me thanx on my no. 09927504777 or my mail id sur_raturi@yahoo.co.in....so call me in my no... next ans will give you sooooooooooon..bye..








/* Program for the merging of two binary search trees*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#define NL printf("\n")
typedef struct node *np;
np memalloc();
np create_newnode(int);
np create();
void inorder(np);
int insert(np,int);
void fre(np root);
int getvalue();
int leaf(np);
np search(np,int);
void merge(np, np);


struct node
      {
       int key;
       np left;
       np right;
      };

void main()
{
   np tree1,tree2;
   int num;
   char ans='y';
   printf("Cereating first tree.....\n");
   tree1=create();
   while(ans=='y'||ans=='Y')
   {
      printf("Enter a number : ");
      num=getvalue();
      insert(tree1,num);
      printf("Have more numbers? ");
      fflush(stdin);
      ans=getch();
   }
   printf("Inorder traversal of this tree is:\n");
   inorder(tree1);
   printf("Cereating second tree.....\n");
   tree2=create();
   while(ans=='y'||ans=='Y')
   {
      printf("Enter a number : ");
      num=getvalue();
      insert(tree2,num);
      printf("Have more numbers? ");
      fflush(stdin);
      ans=getch();
   }
   printf("Inorder traversal of this tree is:\n");
   inorder(tree2);
   printf("Merging the two trees......\n");
   merge(tree1,tree2);
   printf("Inorder traversal of tree1 is .........\n");
   inorder(tree1);
   fre(tree1);

}

void merge(np tree1, np tree2)
{
if(tree2)
{
  inorder(tree2->left);
  inorder(tree2->right);
  insert(tree1,tree2->key);
  fre(tree2);
}
}


np create()
{
int num;
clrscr();
printf("\nEnter number\t");
num=getvalue();
return(create_newnode(num));
}

np memalloc()
{
np n;
if((n=(np)malloc(sizeof (struct node)))==NULL)
{
printf("\nMemory full\n");
exit(1);
return(n);
}
else
return(n);
}



np create_newnode(int num)
{
np new_node;
new_node=memalloc();
new_node->left=new_node->right=NULL;
new_node->key=num;
return(new_node);
}

void inorder(np root)
{
if(root)
{
  inorder(root->left);
  printf("\t%d",root->key);    /* LEFT,NODE,RIGHT */
  inorder(root->right);
}
}

void fre(np root)
{
if(root)
{
  fre(root->left);
  fre(root->right);
  free(root);    /* LEFT,RIGHT,NODE */
}
}


int insert(np root,int num)
{
int rval;
while(1)
{
  if(root->key==num)
  {
   rval=0;
   printf("\nDuplication is not possible in the binary search tree.\n");
   break;
  }
  if(num<root->key)
  {
   if(root->left!=NULL)
   {
    root=root->left;
    continue;
   }
   else
   {
    root->left=create_newnode(num);
    rval=1;
    break;
   }
  }
  else
  {
   if(root->right!=NULL)
   {
    root=root->right;
    continue;
   }
   else
   {
    root->right=create_newnode(num);
    rval=1;
    break;
   }
  }
}
return(rval);
}


int getvalue()
{
int num;
while(!scanf("%d",&num))
{
  printf("\nYou should enter a number. Press any key ...");
  getch();
  fflush(stdin);
}
return(num);
}


int leaf(np node)
{
if(node->left==NULL && node->right==NULL)
return(1);
else
return(0);
}


np search(np root,int num)
{
while(root!=NULL)
  if(root->key==num)
   break;
  else
   if(num<root->key)
    root=root->left;
   else
    root=root->right;
  return(root);
}

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: Binary Space Partitioning Trees FAQ


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.