#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node * left;
struct node * right;
} stick;
stick * createnode( int data)
{
stick * newNode;
newNode
= ( stick
* ) calloc ( 1 , sizeof ( stick
) ) ; newNode-> data= data;
newNode-> left= NULL;
newNode-> right= NULL;
return newNode;
}
stick * binarysearchAndentry( int value, stick * node, int operation)
{
if ( node== NULL)
{
if ( operation== 0 )
{ printf ( "%d is not in the tree" , value
) ; } else
{
printf ( "%d is entered in appropiate position\n " , value
) ; node= createnode( value) ;
return node;
}
}
else
{
if ( node-> left== NULL && node-> data> value)
{
stick * lol;
lol= createnode( value) ;
node-> left= lol;
printf ( "%d is entered in appropiate position\n " , value
) ; }
else
{
if ( node-> right== NULL && node-> data< value)
{
stick * lol;
lol= createnode( value) ;
node-> right= lol;
printf ( "%d is entered in appropiate position\n " , value
) ; }
else
{
if ( value== node-> data)
{
printf ( "%d is found in the tree" , value
) ; }
else
{
if ( node-> data> value)
{
binarysearchAndentry( value, node-> left, operation) ;
}
else
{
binarysearchAndentry( value, node-> right, operation) ;
}
}
}
}
}
}
int main( void ) {
stick * root;
root= binarysearchAndentry( 20 , NULL, 1 ) ;
binarysearchAndentry( 30 , root, 1 ) ;
binarysearchAndentry( 10 , root, 1 ) ;
binarysearchAndentry( 50 , root, 1 ) ;
binarysearchAndentry( 25 , root, 1 ) ;
printf ( "%d\n " , root
-> left
-> data
) ; printf ( "%d\n " , root
-> right
-> data
) ; printf ( "%d\n " , root
-> right
-> right
-> data
) ; printf ( "%d\n " , root
-> right
-> left
-> data
) ;
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KdHlwZWRlZiBzdHJ1Y3Qgbm9kZXsKCWludCBkYXRhOwoJc3RydWN0IG5vZGUgKmxlZnQ7CglzdHJ1Y3Qgbm9kZSAqcmlnaHQ7Cn1zdGljazsKc3RpY2sgKmNyZWF0ZW5vZGUoaW50IGRhdGEpCnsKCXN0aWNrICpuZX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