Here you will get program to create binary tree in C using recursion.

**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.

**Also Read: Binary Search Tree in C**

**Creation of Binary Tree Using Recursion**

A binary tree can be created recursively. The program will work as follow:

- Read a data in x.
- Allocate memory for a new node and store the address in pointer p.
- Store the data x in the node p.
- Recursively create the left subtree of p and make it the left child of p.
- Recursively create the right subtree of p and make it the right child of p.

## Program to Create Binary Tree in C Using Recursion

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#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):"); scanf("%d",&x); if(x==-1) return NULL; p=(node*)malloc(sizeof(node)); p->data=x; printf("Enter left child of %d:\n",x); p->left=create(); printf("Enter right child of %d:\n",x); p->right=create(); return p; } void preorder(node *t) //address of root node is passed in t { if(t!=NULL) { 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"); preorder(root); return 0; } |

**Output**

*Enter data(-1 for no data):5*

*Enter left child of 5:*

*Enter data(-1 for no data):7*

*Enter left child of 7:*

*Enter data(-1 for no data):8*

*Enter left child of 8:*

*Enter data(-1 for no data):3*

*Enter left child of 3:*

*Enter data(-1 for no data):-1*

*Enter right child of 3:*

*Enter data(-1 for no data):-1*

*Enter right child of 8:*

*Enter data(-1 for no data):-1*

*Enter right child of 7:*

*Enter data(-1 for no data):-1*

*Enter right child of 5:*

*Enter data(-1 for no data):-1*

*The preorder traversal of tree is:*

*5*

*7*

*8*

*3*

Arun MI 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

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

Himanshu ChourasiaNice and Easy Code… Easy to understand and implement.. Thank you

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

swethaI can understand your program

easy to perform

MarkAs 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

enesvoid inorder(node *t)

{

if(t!=NULL)

{

inorder(t->left); //inorder traversal on left subtree

printf(“\n%d”,t->data); // visit the root

inorder(t->right); //inorder traversal om right subtree

}

}

write this code and delete function preorder

YashCan u plz..explain this code

See..here I am confused.

When the recursive function call of inorder(t->left) is done

i.e when t->left=null(at the leftmost node)

then if condition should no longer execute as t=null despite it is, why?

popCorrect therefore this Code is 100% BS live your life like a lone wolf nigglet

June 09The program was really helpful. Thanks.

MUKTARUL HOSSAINtoo easy to learn thank you

ycNice program!

Thank you so much!

Hrishikesh PatilVery simplified code, thanks..!!

mahendranIts really useful thank u

Saurav BhagatThanks, I was searching for something like this only.

tushar mahaleThanks. Really good.

SreejithThanks that was so simple and easy to understand

Dinanath SinhaTo be honest, I found this code a bit complicated and not properly arranged…what,s about the case if you want to insert a new node to the tree???..

MiyaHow to implement serch and delete function in this code plzzz help me

yashithacan we use a display function instead of preorder traversal ?

pulkit sharmawell traversal function is a kind of function for displaying the tree elements.

pulkit sharmaWell its a good code, seriously helped a lot.Keep Doing the good Work!!!

eeeThanks for your code.

It is so useful for us.

etiI was searching for tree creation program for a long time, this one is so neat and nice. Thanks for helping.

vbghjkWhat is the time complexity of above program? Is it O(n)?

partha pratimas we using recursion the time complexity of both create and post order program is n order so n+n is 2n thats O(n)……………….

partha pratimit will also work if we omit return p; in create………..why?????????

aaaeasy to understand

shwetacan u explain why he used x=-1 condition here?

Vicky SharmaCan anyone explain me this line:

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

p->data=x; //mainly this line im unalbe to understand this.

mayank mohakread structures and dynamic memory allocation.

mayank mohakfor dynamic memory allocation stdlib.h header file must be included.

use #include just after #include

mayank mohak#include”stdlib.h”

Sumanta BanerjeeGood and efficient program.