Code:
#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 main( )
{
struct btreenode *bt ;
int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1,
90, 3 } ;
int i ;
bt = NULL ;
clrscr( ) ;
printf ( "Binary tree sort.\n" ) ;
printf ( "\nArray:\n" ) ;
for ( i = 0 ; i <= 9 ; i++ )
printf ( "%d\t", arr[i] ) ;
for ( i = 0 ; i <= 9 ; i++ )
insert ( &bt, arr[i] ) ;
printf ( "\nIn-order traversal of binary
tree:\n" ) ;
inorder ( bt ) ;
getch( ) ;
}
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 ) ;
}
}
void inorder ( struct btreenode *sr )
{
if ( sr != NULL )
{
inorder ( sr -> leftchild ) ;
printf ( "%d\t", sr -> data ) ;
inorder ( sr -> rightchild ) ;
}
}
This is an example of binary search sort in C language where you will find how stack is been used in this sorting. If you observe carefully you will find the concept of binary tree and node also. If you execute this program you will come to know the process of binary tree sorting.
Bookmarks