Binary Tree ( Array implementation ) | C ++ implementation of tree using array numbering starting from 0 to n - 1. ; create root ; create left son of root ; create right son of root ; Print tree ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char tree [ 10 ] ; int root ( char key ) { if ( tree [ 0 ] != ' \0' ) cout << " Tree ▁ already ▁ had ▁ root " ; else tree [ 0 ] = key ; return 0 ; } int set_left ( char key , int parent ) { if ( tree [ parent ] == ' \0' ) cout << " Can ' set child at " << ( parent * 2 ) + 1 << " ▁ , ▁ no ▁ parent ▁ found " ; else tree [ ( parent * 2 ) + 1 ] = key ; return 0 ; } int set_right ( char key , int parent ) { if ( tree [ parent ] == ' \0' ) cout << " Can ' set child at " << ( parent * 2 ) + 2 << " ▁ , ▁ no ▁ parent ▁ found " ; else tree [ ( parent * 2 ) + 2 ] = key ; return 0 ; } int print_tree ( ) { cout << " STRNEWLINE " ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( tree [ i ] != ' \0' ) cout << tree [ i ] ; else cout << " - " ; } return 0 ; } int main ( ) { root ( ' A ' ) ; set_right ( ' C ' , 0 ) ; set_left ( ' D ' , 1 ) ; set_right ( ' E ' , 1 ) ; set_right ( ' F ' , 2 ) ; print_tree ( ) ; return 0 ; }