forked from client69/Open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarytree.c
95 lines (88 loc) · 1.87 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
93
94
95
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left, *right;
};
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d \t", root->key);
inorder(root->right);
}
}
void preorder(struct node *root)
{
if(root!=NULL)
{
printf(" %d \t",root->key);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf(" %d \t",root->key);
}
}
struct node* insert(struct node* node, int key)
{
if (node == NULL) return newNode(key);
if (key > node->key)
node->left = insert(node->left, key);
else if (key < node->key)
node->right = insert(node->right, key);
return node;
}
int main()
{
struct node *root = NULL;
int ch,k;
printf("1 Insertion \n");
printf("2 Inorder \n");
printf("3 Preorder \n");
printf("4 Postorder \n");
printf("5 - Exit\n");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
{
printf("enter the element t\t\t");
scanf("%d",&k);
root = insert(root, k);
break;
}
case 2:{printf("inorder traversed \n");
inorder(root);
break;}
case 3:{printf("preorder traversed \n");
preorder(root);
break;}
case 4:{printf("postorder \n");
postorder(root);
break;}
case 5:
exit(0);
default :printf("Wrong choice, Please enter correct choice ");
}
}
return 0;
}