Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


implement general tree using link list



implement general tree using link list..

Answer / abdur rab

#include <stdio.h>
#include <math.h>

struct node {
int element;
struct node* left;
struct node* right;
};


int lookup ( struct node* _node_ptr, int target_element )
{
if ( NULL == _node_ptr ) return ( 0 );
if ( target_element == _node_ptr -> element )
return ( 1 );
else {
if ( target_element < _node_ptr ->
element ) return ( lookup ( _node_ptr -> left,
target_element ) );
else return ( lookup ( _node_ptr -> right,
target_element ) );
}
}

int max_depth ( struct node* _node_ptr )
{
if ( NULL == _node_ptr ) return ( 0 );
else {
int _l_depth = max_depth ( _node_ptr ->
left );
int _r_depth = max_depth ( _node_ptr ->
right );
return ( ( _l_depth > _r_depth ) ? (
_l_depth + 1 ) : ( _r_depth + 1 ) );
}
}

struct node* create_node ( int _element )
{
struct node* _node = ( struct node* ) malloc (
sizeof (struct node) );
_node -> element = _element;
_node -> left = NULL;
_node -> right = NULL;

return ( _node );
}

struct node* insert ( struct node* _node_ptr, int _element )
{
if ( NULL== _node_ptr ) return ( create_node (
_element ) );
else {
if ( _element <= _node_ptr -> element )
_node_ptr -> left = ( insert ( _node_ptr -> left,
_element ) );
else _node_ptr -> right = ( insert (
_node_ptr -> right, _element ) );
}
return ( _node_ptr );
}

void inorder_traversal ( struct node* _node_ptr )
{
if ( NULL == _node_ptr ) return;
inorder_traversal ( _node_ptr -> left );
printf ("\n The element :%d", _node_ptr ->
element );
inorder_traversal ( _node_ptr -> right );
}

void preorder_traversal ( struct node* _node_ptr )
{
if ( NULL == _node_ptr ) return;
printf ("\n The element :%d", _node_ptr ->
element );
preorder_traversal ( _node_ptr -> left );
preorder_traversal ( _node_ptr -> right );
}

void postorder_traversal ( struct node* _node_ptr )
{
if ( NULL == _node_ptr ) return;
postorder_traversal ( _node_ptr -> left );
postorder_traversal ( _node_ptr -> right );
printf ("\n The element :%d", _node_ptr ->
element );
}


int main ( int argc, char* argv[] )
{
int* array_int, nTotalCount = 10, i;
struct node* _node_ptr = NULL;
int temp = 0;

printf ( "\n/********************** Binary Tree
********************/" );
array_int = ( int* ) malloc ( nTotalCount * sizeof
( int ) );

for ( i = 0; i < nTotalCount; i++ )
{
temp = rand() % 100 ;
array_int [i] = ( temp == 0 ) ? ( temp +
1 ) : temp;
}

printf( "\nThe Numbers in given order" );
for ( i = 0; i < nTotalCount; i++ )
printf ( "\t%d", array_int [i] );

for ( i = 0; i < nTotalCount; i++ )
_node_ptr = insert ( _node_ptr, array_int
[i] );

inorder_traversal ( _node_ptr );
printf ("\n max possible nodes :%f", pow ( 2,
max_depth ( _node_ptr ) ) );

free ( array_int [i] );
}

Is This Answer Correct ?    10 Yes 15 No

Post New Answer

More C Interview Questions

what is the return type of printf

3 Answers  


How to write a program for swapping two strings without using 3rd variable and without using string functions.

7 Answers   iGate, Infotech,


which operator having lowest precedence?? a.)+ b.)++ c.)= d.)%

4 Answers  


What is "Hungarian Notation"?

0 Answers   Celstream,


void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); }

3 Answers   ME, pspl,


Simplify the program segment if X = B then C &#8592; true else C &#8592; false

0 Answers  


WAP to accept basic salary of an employee? Calculate it HRA=25%,DA=30%,PF=30%&net salary display all contents?

6 Answers   Finite Infotech, Lovely Professional University, Wipro,


What is a macro, and explain how do you use it?

0 Answers  


What is the difference between array_name and &array_name?

0 Answers  


In C language, the variables NAME, name, and Name are all the same. TRUE or FALSE?

0 Answers  


What is the use of typedef in c?

0 Answers  


What is page thrashing?

0 Answers  


Categories