Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Data File StructureRSS Feeds

Binary Tree Sorting

Posted By: Vande Fischer     Category: C Programming     Views: 25810

Program of Binary Tree Sorting.

Code for Binary Tree Sorting in C Programming

#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 ) ;
    }
}
  
Share: 

 
 
 

Didn't find what you were looking for? Find more on Binary Tree Sorting Or get search suggestion and latest updates.

Vande Fischer
Vande Fischer author of Binary Tree Sorting is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!