C Program to Implement External Sorting using B Tree and Inorder Traversal

«
»
This C program sorts integers using Inorder traversal in a B tree.

Here is the source code of the C program to display sorted list using B tree sort. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to Implement external sorting using Btree 
  3.  * using inorder traversal
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. struct btreenode
  9. {
  10.     struct btreenode *leftchild;
  11.     int data;
  12.     struct btreenode *rightchild;
  13. };
  14.  
  15. void insert(struct btreenode**, int);
  16. void inorder(struct btreenode*);
  17.  
  18. /*  The main() begins  */
  19. main()
  20. {
  21.     struct btreenode *bt;
  22.     int arr[100], size;
  23.     int i;
  24.     bt = NULL;
  25.     printf("Enter the Number of elements: ");
  26.     scanf("%d", &size);
  27.     for (i = 0; i < size; i++)
  28.         scanf("%d", &arr[i]);
  29.     printf("Binary tree sort.\n");
  30.     for (i = 0; i <= 9; i++)
  31.         insert(&bt, arr[i]);
  32.     printf("\nIn-order traversal of binary tree:\n");
  33.     inorder(bt);
  34.     printf("\n");
  35.     return 0;
  36. }
  37.  
  38. /*  Function to insert number into a Btree  */
  39. void insert(struct btreenode **sr, int num)
  40. {
  41.     if (*sr == NULL) {
  42.         *sr = malloc (sizeof(struct btreenode));
  43.         (*sr)->leftchild = NULL;
  44.         (*sr)->data = num;
  45.         (*sr)->rightchild = NULL;
  46.     }
  47.     else {
  48.         if (num < (*sr)->data)
  49.             insert(&((*sr)->leftchild), num);
  50.         else
  51.             insert(&((*sr)->rightchild), num);
  52.     }
  53. }
  54. /*  End of inert()  */
  55.  
  56. /*  Function for Inorder traversal  */
  57. void inorder(struct btreenode *sr)
  58. {
  59.     if (sr != NULL) {
  60.         inorder(sr->leftchild);
  61.         printf("%d ", sr->data);
  62.         inorder(sr->rightchild);
  63.     }
  64. }
  65. /*  End of inorder()  */

$ gcc btree.c
$ a.out
Enter the Number of elements: 10
9 1 5 2 77 4 22 8 12 56
Binary tree sort.
 
In-order traversal of binary tree:
1 2 4 5 8 9 12 22 56 77

Sanfoundry Global Education & Learning Series – 1000 C Algorithms.

advertisement
advertisement
If you wish to look at all C Algorithms and Solutions, go to C Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.