-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree.c
92 lines (78 loc) · 2.24 KB
/
BinaryTree.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
$$$$$$$\ $$\ $$\ $$\ $$$$$$$$\
$$ __$$\ $$$\ $$ | $$ | \__$$ __|
$$ | $$ |$$\ $$\ $$$$\ $$ | $$$$$$\ $$$$$$\ $$ | $$ | $$$$$$\ $$\ $$\
$$$$$$$\ |$$ | $$ | $$ $$\$$ |$$ __$$\ $$ __$$\ $$ | $$ |$$ __$$\ $$ | $$ |
$$ __$$\ $$ | $$ | $$ \$$$$ |$$ / $$ |$$$$$$$$ |$$ | $$ |$$ / $$ |$$ | $$ |
$$ | $$ |$$ | $$ | $$ |\$$$ |$$ | $$ |$$ ____|$$ | $$ |$$ | $$ |$$ | $$ |
$$$$$$$ |\$$$$$$$ |$$\ $$ | \$$ |\$$$$$$ |\$$$$$$$\ $$ | $$ |\$$$$$$ |\$$$$$$$ |
\_______/ \____$$ |$ | \__| \__| \______/ \_______|\__| \__| \______/ \____$$ |
$$\ $$ |\_/ $$\ $$ |
\$$$$$$ | \$$$$$$ |
\______/ \______/
@author Noel Toy
@credits https://www.thecrazyprogrammer.com/2013/12/c-program-to-create-binary-tree-using-recursion.html
*/
#include<stdio.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
}node;
node *create()
{
node *p;
int x;
printf("\nEnter the data(No data -1):");
scanf("%d",&x);
if(x==-1)
{
return NULL;
}
p=(node *)malloc(sizeof(struct node));
p->data=x;
printf("\nEnter the left child of %d:",x);
p->left=create();
printf("\nEnter the right child of %d:",x);
p->right=create();
return p;
}
void preorder(node *t)
{
if(t!=NULL)
{
printf("\nThe Data is:%d",t->data);
preorder(t->left);
preorder(t->right);
}
}
void inorder(node *t)
{
if(t!=NULL)
{
inorder(t->left);
printf("\nThe Data is:%d",t->data);
inorder(t->right);
}
}
void postorder(node *t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("\nThe Data is:%d",t->data);
}
}
void main()
{
node *root;
root=create();
printf("The pre order traversal of the tree is:\n");
preorder(root);
printf("\nThe in order traversal of the tree is:\n");
inorder(root);
printf("\nThe post order traversal of the tree is:\n");
postorder(root);
}