# Program to implement binary tree and print value of nodes in ascending order

Such a binary tree that has the property that all elements in the left subtree of a node n are less than the contents of n, and all elements in the right subtree of n are greater than or equal to the contents of n. A binary tree that has this property is called a Binary Search tree. If a binary search tree is traversed in inorder (left, root, right) and the contents of each node are printed as the node is visited, the numbers are printed in ascending order. Convince yourself that this is the case for the binary search tree. The program to implement this algorithm is given below:

/* Program to implement a binary tree */

```#include"alloc.h"

struct btreenode

{

struct btreenode *leftchild ;

int data ;

struct btreenode *rightchild ;

} ;

main( )

{

struct btreenode *bt ;

int req, i = 1, num ;

bt = NULL ; /* empty tree */

clrscr( ) ;

printf ( "Specify the number of data items to be inserted: " ) ;

scanf ( "%d", &req ) ;

while ( i++  leftchild = NULL ;

( *sr ) -> data =num ;

( *sr ) -> rightchild = NULL ;

return ;

}

else /* search the node to which new node will be attached */

{

/* if new data is less, traverse to left */

if ( num  data )

insert ( &( ( *sr ) -> leftchild ), num ) ;

else

/* else traverse to right */

insert (&( ( *sr ) -> rightchild ), num ) ;

}

return ;

}

/*traverse a binary search tree in a LDR (Left-Data-Right) fashion */

inorder ( struct btreenode *sr )

{

if ( sr != NULL )

{

inorder ( sr -> leftchild ) ; /* print the data of the node whose leftchild is NULL or the path

printf ( "%d ", sr-> data ) ;

inorder ( sr -> rightchild ) ;

}

else

return ;

}

/* traverse a binary search tree in a DLR (Data-Left-right) fashion */

preorder ( struct btreenode *sr )

{

if ( sr != NULL )

{

/* print the data of a node */

printf ( "%d ", sr -> data ) ; /* traverse till leftchild is not NULL */

preorder ( sr -> leftchild ) ;

/* traverse till rightchild is not NULL */

preorder ( sr -> rightchild ) ;

}

else

return ;

}

/* traverse a binary search tree in LRD (Left-Right-Data) fashion */

postorder ( struct btreenode *sr )

{

if ( sr != NULL )

{

postorder ( sr -> leftchild ) ;

postorder ( sr -> rightchild ) ;

printf ( "%d ", sr -> data ) ;

}

else

return ;

}```