ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
implement general tree using link list
 Question Submitted By :: Soumyajoy
I also faced this Question!!     Rank Answer Posted By  
 
  Re: implement general tree using link list
Answer
# 1
#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 ?    1 Yes 6 No
Abdur Rab
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
WAP &#8211; represent a char in binary format Motorola4
How to find the given no is odd or even without checking of any condition and loops. (Hint: Using array)  4
what is the difference between strcpy() and memcpy() function?  1
writw a program to insert an element in the begning of a doubly linked list  1
write a program to check whether a given integer is a strong number or not? [Hint: 145=1!+4!+5! =1+24+120 =145]  4
if array a conatins 'n' elements and array b conatins 'n-1' elements.array b has all element which are present in array a but one element is missing in array b. find that element. Zycus-Infotech9
Write code for initializing one dimentional and two dimentional array in a C Program? Deshaw5
what is the output of the following program? main() { int c[]={2,8,3,4,4,6,7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf("%d",*c); ++q; } for(j=0;j<5;j++) { printf("%d",*p); ++p; } }  4
There is a 100-story building and you are given two eggs. The eggs (and the building) have an interesting property that if you throw the egg from a floor number less than X, it will not break. And it will always brake if the floor number is equal or greater than X. Assuming that you can reuse the eggs which didn't broke; you got to find X in a minimal number of throws. Give an algorithm to find X in minimal number of throws.  2
enum day = { jan = 1 ,feb=4, april, may} what is the value of may? a)4 b)5 c)6 d)11 e)none of the above HCL2
write a program that print itself even if the source file is deleted?  1
what is link list?  2
if ENTERED FIVE DIGITS DESIGN A PROGRAM THAT WILL FIND CORRESPONDING VALUE FROM ASCII TABLE  1
char ch=10;printf("%d",ch);what is the output Accenture11
#include<stdio.h> int SumElement(int *,int); void main(void) { int x[10]; int i=10; for(;i;) { i--; *(x+i)=i; } printf("%d",SumElement(x,10)); } int SumElement(int array[],int size) { int i=0; float sum=0; for(;i<size;i++) sum+=array[i]; return sum; } output? Ramco5
What is the purpose of Scanf Print, getchar, putchar, function?  2
what is the difference between const char *p, char const *p, const char* const p Accenture4
What is the diffences between Windows XP and Windows Visa Aricent1
C program to find frequency of each character in a text file?  3
WAP TO ACCEPT STRING AND COUNT A COMES N TIMES B COMES N TIMES C COMES N TIMES D COMES N TIMES AND SO ON......... AT LAST UNTIL Z COMES N TIMES...............  3
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com