**What is Binary Tree?**

A tree is said to be a binary

tree if each node of the tree can have maximum of two children. Children of a

node of binary tree are ordered. One child is called “left” child and the other

is called “right” child. An example of binary tree is shown in below diagram.

tree if each node of the tree can have maximum of two children. Children of a

node of binary tree are ordered. One child is called “left” child and the other

is called “right” child. An example of binary tree is shown in below diagram.

**Creation of Binary Tree Using Recursion**

A binary tree can be created

recursively. The program will work as follow:

recursively. The program will work as follow:

1. Read a data in x.

2. Allocate memory for a new node

and store the address in pointer p.

and store the address in pointer p.

3. Store the data x in the node p.

4. Recursively create the left

subtree of p and make it the left child of p.

subtree of p and make it the left child of p.

5. Recursively create the right

subtree of p and make it the right child of p.

subtree of p and make it the right child of p.

**Also Read: C++ program to create Facebook Login screen**

The program is written in C

language which allows linked representation of binary tree. Code will be as

follow:

language which allows linked representation of binary tree. Code will be as

follow:

*#include<stdio.h>*

*typedef struct node*

*{*

*int data;*

*struct node *left;*

*struct node *right;*

*}node;*

*node *create()*

*{*

*node *p;*

*int x;*

*printf(“Enter data(-1 for no*

data):”);

data):”);

*scanf(“%d”,&x);*

*if(x==-1)*

*return NULL;*

*p=(node*)malloc(sizeof(node));*

*p->data=x;*

*printf(“Enter left child of*

%d:n”,x);

%d:n”,x);

*p->left=create();*

*printf(“Enter right child of*

%d:n”,x);

%d:n”,x);

*p->right=create();*

*return p;*

*}*

*void preorder(node *t) //address of root node is passed*

in t

in t

*{*

*if(t!=NULL)*

*{*

printf(“n%d”,t->data);

//visit the root

printf(“n%d”,t->data);

//visit the root

*preorder(t->left); //preorder traversal on left subtree*

*preorder(t->right); //preorder traversal om right subtree*

*}*

*}*

*int main()*

*{*

*node *root;*

*root=create();*

*printf(“nThe preorder traversal of*

tree is:n”);

tree is:n”);

*preorder(root);*

*return 0;*

*}*

In the above program I have used

preorder traversal to just show that the tree is created properly or not. You

can use any other traversal method here.

preorder traversal to just show that the tree is created properly or not. You

can use any other traversal method here.

I got the answer.

I just modified the program.

Used cout & cin instead of printf & scanf.

I got "malloc undeclared error".

But i added

#include

Your program is nice 🙂

Thank Youu

I am happy that my program was usefull for you. Thanks for visiting.

Nice and Easy Code… Easy to understand and implement.. Thank you

your program is very easy to understand and thank u so much

I can understand your program

easy to perform

As far as I understand to create above binary tree ABCDEFG. your program needs input in the form

A B D -1 -1 E -1 -1 C F -1 -1 G – 1-1

Can you please provide a program to create ABCDEFG binary tree which take input in this order : A B C D E F G