Binary tree using Array

#include < stdio.h >
#include < conio.h >
#define SIZE 50 

int insert_element( int []) ;
void display_tree ( int [] , int );

void main()
{
 int tree [ SIZE ] = { 0 } ;
 int ch , s = 0;
 

 while ( 1 )
 {
  //system("cls");
  printf ( "\n Binary Tree Using Array \n  \n 1. Make Tree \n 2. Display Tree \n 3. Exit  : " ) ;
  printf ( " \n Enter choice : " ) ;
  scanf ( "%d" , &ch );
  
  switch ( ch )
  {
  case 1 :
    s = insert_element( tree ) ;
    break ;
  case 2 :
    display_tree ( tree , s );
   break ;
  case 3 :
   printf ( " \n Thank you " ) ;
   getch(); 
   exit ();
  default : 
   printf ( " \n\n Invalid choice...! " );
  }
  getch();
 }
 
}


int insert_element( int tree[]  )
{
 int value ;
 int i = 0 , position = 0 ;
 char ch ;

 printf ( " \n\n Enter value : " );
 scanf ( "%d" , &value );
 
 tree [ i ] = value ;

 for ( i = 0 ; i < SIZE ; i++ )
 {
  if ( tree [ i ] != 0 )
  {
    printf ( " \n\n %d has a left child node  ? (y/n) : " , tree [ i ] );
    scanf ( " %c" , &ch );

    if ( ch == 'y' )
    {
    printf ( " \n\n Enter value : " );
    scanf ( "%d" , &value );

    position = i + 1 ;
    position = ( 2* position ) - 1 ;
    tree [ position ] = value ;
    }

    printf ( " \n\n %d has a right child node  ? (y/n) : " ,  tree [ i ] );
    scanf ( " %c" , &ch );

    if ( ch == 'y' )
    {
    printf ( " \n\n Enter value : " );
    scanf ( "%d" , &value );

    position = i + 1 ;
    position = ( 2* position )  ;
    tree [ position ] = value ;
    } 

    printf ( " \n Do you want to continue ? (y/n) : " );
    scanf ( " %c" , &ch );

    if ( ch != 'y' )
     break ;

  }
 }

 return position ;
}

void display_tree ( int tree[] , int s )
{
 int i ;
 printf ( " \n\n Tree  : \n\n" ) ;

 for ( i =0 ; i <= s ; i++ )
 {
  printf ( "\n position : %d  ::  %d " , i , tree [ i ] );
 }
}

/*

***********************************************************************************************************************
------------------------------------------------------------------------

         code
------------------------------------------------------------------------



 Binary Tree Using Array

 1. Make Tree
 2. Display Tree
 3. Exit  :
 Enter choice : 1


 Enter value : 5


 5 has a left child node  ? (y/n) : y


 Enter value : 3


 5 has a right child node  ? (y/n) : n

 Do you want to continue ? (y/n) : y


 3 has a left child node  ? (y/n) : y


 Enter value : 2


 3 has a right child node  ? (y/n) : y


 Enter value : 8

 Do you want to continue ? (y/n) : y


 2 has a left child node  ? (y/n) : y


 Enter value : 9


 2 has a right child node  ? (y/n) : n

 Do you want to continue ? (y/n) : n

 Binary Tree Using Array

 1. Make Tree
 2. Display Tree
 3. Exit  :
 Enter choice : 2


 Tree  :


 position : 0  ::  5
 position : 1  ::  3
 position : 2  ::  0
 position : 3  ::  2
 position : 4  ::  8
 position : 5  ::  0
 position : 6  ::  0
 position : 7  ::  9
 Binary Tree Using Array

 1. Make Tree
 2. Display Tree
 3. Exit  :
 Enter choice : 3

 Thank you

*/
SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment