-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbt.c
More file actions
105 lines (97 loc) · 2.39 KB
/
bt.c
File metadata and controls
105 lines (97 loc) · 2.39 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
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
struct tree
{
int data;
struct tree *left, *right;
};
struct tree* create();
struct tree* insert(int data);
void inorder(struct tree* root);
void postorder(struct tree* root);
void preorder(struct tree* root);
int main()
{
struct tree *root = NULL;
int choice=0, key=0;
do
{
printf("\nEnter choice:\n1. Insert\n2. Traverse\nAny other number to exit.\nEnter Choice : ");
scanf("%d", &choice);
if(choice==1)
{
printf("\nEnter value to insert for root(-1 for no node) : ");
scanf("%d", &key);
root=insert(key);
printf("BINARY TREE:\n");
inorder(root);
}
else if(choice==2)
{
printf("Enter choice of traversal:\n1. Inorder Traversal\n2. Preorder Traversal\n3. Postorder Traversal\nEnter Option : ");
scanf("%d", &key);
printf("\nBinary Tree : ");
if(key==1)
inorder(root);
else if(key==2)
preorder(root);
else if(key==3)
postorder(root);
else
printf("Invalid Selection");
}
else
{
printf("EXIT.");
}
}while(choice==1 || choice==2);
return 0;
}
struct tree* create()
{
struct tree *newnode=(struct tree*)malloc(sizeof(struct tree));
newnode->left=NULL, newnode->right=NULL;
return newnode;
}
struct tree* insert(int data)
{
struct tree *newnode = create();
int temp=data;
if(data==-1)
return 0;
newnode->data=data;
printf("Enter left child of %d:\n",temp);
scanf("%d",&data);
newnode->left=insert(data);
printf("Enter right child of %d:\n",temp);
scanf("%d",&data);
newnode->right=insert(data);
return newnode;
}
void inorder(struct tree* root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
void postorder(struct tree* root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
void preorder(struct tree* root)
{
if(root!=NULL)
{
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}