-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytreecreation.c
More file actions
99 lines (94 loc) · 2.04 KB
/
binarytreecreation.c
File metadata and controls
99 lines (94 loc) · 2.04 KB
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
93
94
95
96
97
98
99
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*llink;
struct node*rlink;
};
struct node *root, *tree, *temp;
int isleftchild(struct node *tree)
{ int ch;
printf("\nDo you want a left child of %d",tree->data);
printf("\nEnter 1 for Yes and 0 for No: ");
scanf("%d",&ch);
if(ch==1)
return 1;
else
return 0;
}
int isrightchild(struct node *tree)
{ int ch;
printf("\nDo you want a right child of %d",tree->data);
printf("\nEnter 1 for Yes and 0 for No: ");
scanf("%d",&ch);
if(ch==1)
return 1;
else
return 0;
}
void create(struct node *tree)
{
if(isleftchild(tree)==1)
{
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter data for left child of %d: ",tree->data);
scanf("%d",&temp->data);
tree->llink=temp;
create(temp);
}
else
{
tree->llink=NULL;
}
if(isrightchild(tree)==1)
{
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter data for right child of %d: ",tree->data);
scanf("%d",&temp->data);
tree->rlink=temp;
create(temp);
}
else
{
tree->rlink=NULL;
}
}
void inorder(struct node *tree)
{
if(tree!=NULL)
{
inorder(tree->llink);
printf("%d ",tree->data);
inorder(tree->rlink);
}
}
void preorder(struct node *tree)
{
if(tree!=NULL)
{
printf("%d ",tree->data);
preorder(tree->llink);
preorder(tree->rlink);
}
}
void postorder(struct node *tree)
{
if(tree!=NULL)
{
postorder(tree->llink);
postorder(tree->rlink);
printf("%d ",tree->data);
}
}
void main()
{
root = (struct node*)malloc(sizeof(struct node));
printf("Enter data for first node: ");
scanf("%d",&root->data);
root->llink=root->rlink=NULL;
create(root);
printf("the inorder of: "); inorder(root);
printf("the preorder of: "); preorder(root);
printf("the postorder of: "); postorder(root);
}