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.
/*
* C program to Implement external sorting using Btree
* using inorder traversal
*/
#include <stdio.h>
#include <stdlib.h>
struct btreenode
{
struct btreenode *leftchild;
int data;
struct btreenode *rightchild;
};
void insert(struct btreenode**, int);
void inorder(struct btreenode*);
/* The main() begins */
main()
{
struct btreenode *bt;
int arr[100], size;
int i;
bt = NULL;
printf("Enter the Number of elements: ");
scanf("%d", &size);
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("Binary tree sort.\n");
for (i = 0; i <= 9; i++)
insert(&bt, arr[i]);
printf("\nIn-order traversal of binary tree:\n");
inorder(bt);
printf("\n");
return 0;
}
/* Function to insert number into a Btree */
void insert(struct btreenode **sr, int num)
{
if (*sr == NULL) {
*sr = malloc (sizeof(struct btreenode));
(*sr)->leftchild = NULL;
(*sr)->data = num;
(*sr)->rightchild = NULL;
}
else {
if (num < (*sr)->data)
insert(&((*sr)->leftchild), num);
else
insert(&((*sr)->rightchild), num);
}
}
/* End of inert() */
/* Function for Inorder traversal */
void inorder(struct btreenode *sr)
{
if (sr != NULL) {
inorder(sr->leftchild);
printf("%d ", sr->data);
inorder(sr->rightchild);
}
}
/* 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.
Related Posts:
- Practice Computer Science MCQs
- Check Data Structure Books
- Practice Design & Analysis of Algorithms MCQ
- Check Computer Science Books
- Apply for Computer Science Internship