B-Tree - Coder's point

Sunday, April 7, 2019

B-Tree

 #include<stdio.h>  
 #include<conio.h>  
 #include<alloc.h>  
 struct btreenode  
 {  
  struct btreenode *leftchild;  
  int data;  
  struct btreenode *rightchild;  
 };  
 void insert(struct btreenode **,int);  
 void inorder(struct btreenode *);  
 void preorder(struct btreenode *);  
 void postorder(struct btreenode *);  
 void main()  
 {  
  struct btreenode *bt;  
  int req,i=1,num;  
  bt=NULL;  
  clrscr();  
  printf("Specify the number of items to be inserted:");  
  scanf("%d",&req);  
  while(i++<=req)  
  {  
  printf("Enter the data:");  
  scanf("%d",&num);  
  insert(&bt,num);  
  }  
  printf("\nInorder traversal:");  
  inorder(bt);  
  printf("\nPreorder traversal:");  
  preorder(bt);  
  printf("\nPostorder traversal:");  
  postorder(bt);  
  getch();  
 }  
 void insert(struct btreenode **sr,int num)  
 {  
  if(*sr==NULL)  
  {  
  *sr=(struct btreenode *)malloc(sizeof(struct btreenode));  
  (*sr)->leftchild=NULL;  
  (*sr)->data=num;  
  (*sr)->rightchild=NULL;  
  return;  
  }  
  else  
  {  
  if(num<(*sr)->data)  
  {  
   insert(&((*sr)->leftchild),num);  
  }  
  else  
  {  
   insert(&((*sr)->rightchild),num);  
  }  
  return;  
  }  
 }  
 void inorder(struct btreenode *sr)  
 {  
  if(sr!=NULL)  
  {  
  inorder(sr->leftchild);  
  printf("\t%d",sr->data);  
  inorder(sr->rightchild);  
  }  
  else  
  return;  
 }  
 void preorder(struct btreenode *sr)  
 {  
  if(sr!=NULL)  
  {  
  printf("\t%d",sr->data);  
  preorder(sr->leftchild);  
  preorder(sr->rightchild);  
  }  
  else  
  return;  
 }  
 void postorder(struct btreenode *sr)  
 {  
  if(sr!=NULL)  
  {  
  postorder(sr->leftchild);  
  postorder(sr->rightchild);  
  printf("\t%d",sr->data);  
  }  
  else  
  return;  
 }  
OUTPUT:
Specify the number of items to be inserted:5                                                                                     
Enter the data:23                                                                                                                
Enter the data:45                                                                                                                
Enter the data:76                                                                                                                
Enter the data:89                                                                                                                
Enter the data:1                                                                                                                 
                                                                                                                                 
Inorder traversal:      1       23      45      76      89                                                                       
Preorder traversal:     23      1       45      76      89                                                                       
Postorder traversal:    1       89      76      45      23                                                                     

No comments:

Post a Comment