veddotcom
Journeyman
/* Program to build a binary search tree from arrays. */
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct node
{
struct node *left ;
char data ;
struct node *right ;
} ;
struct node * buildtree ( int ) ;
void inorder ( struct node * ) ;
char arr[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' } ;
int lc[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 } ;
int rc[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 } ;
void main( )
{
struct node *root ;
clrscr( ) ;
root = buildtree ( 0 ) ;
printf ( “In-order Traversal:\n” ) ;
inorder ( root ) ;
getch( ) ;
}
struct node * buildtree ( int index )
{
struct node *temp = NULL ;
if ( index != -1 )
{
temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
temp -> left = buildtree ( lc[index] ) ; //LINE 1
temp -> data = arr[index] ; //LINE 2
temp -> right = buildtree ( rc[index] ) ; //LINE 3
}
return temp ;
}
void inorder ( struct node *root )
{
if ( root != NULL )
{
inorder ( root -> left ) ;
printf ( "%c\t", root -> data ) ;
inorder ( root -> right ) ;
}
}
OUTPUT Of the Above Programm is
In-Order Traversal:
D B H E A F C G
I want to know How the Function BUILDTREE is Working, The Problem is When we Calling the Function buildtree recursively by the expression LINE 1,HOw Compliter would be Able to Read The LINE 2 and LINE 3, After Recursive Call The Function Buildtree the Compiler would return to 1st Statement i.e "struct node *temp = NULL ;",After that it will check Condition, and if Condition is True it will Allocate the new Node after that It will again call Buildtree funtion, now again compiler will be back to first statement , Then How LINE 1 and LINE 2 Will Work.