-
Notifications
You must be signed in to change notification settings - Fork 22
/
_4.binarytreell.c
163 lines (151 loc) · 2.96 KB
/
_4.binarytreell.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Node{
int data;
struct Node * left;
struct Node *right;
}*root;
struct Node *search(struct Node *r,int key)
{
struct Node *x=NULL;
if(r->data==key)
{
return(r);
}
else if(r->left!=NULL)
{
x=search(r->left,key);
}
if(x==NULL && r->right!=NULL)
{
x=search(r->right,key);
}
return(x);
}
struct Node *createnode()
{ int item;
struct Node *temp;
temp=(struct Node*)malloc(sizeof(struct Node));
printf("Enter element to be inserted:-\n");//if no node insert -1
scanf("%d",&item);
if(item==-1)
{return 0;
}
temp->data=item;
printf("Enter left child of %d:-\n",item);
temp->left=createnode();
printf("Enter right child of %d:-\n",item);
temp->right=createnode();
return temp;
}
struct Node* parent(struct Node* r, struct Node* i)
{
struct Node* x=NULL;
if(r->left == i || r->right == i)
{
return(r);
}
else if(r->left != NULL)
{
x = parent(r->left,i);
}
if(x==NULL && r->right != NULL)
{
x=parent(r->right,i);
}
return(x);
}
void delete()
{
int key,flag=0;
printf("ENTER THE ELEMENT TO DELETE : ");
scanf("%d",&key);
struct Node* i = search(root,key);
if(i!=NULL)
{
if((i->left==NULL)&&(i->right==NULL))
{
flag=1;
struct Node *p = parent(root,i);
if(p->left==i)
{
printf("%d DELETED \n",i->data);
free(i);
p->left=NULL;
}
else if(p->right==i)
{
printf("%d DELETED \n",i->data);
free(i);
p->right=NULL;
}
}
else
{
printf("NODE CAN'T BE DELETED!!\n");
}
}
else if((i==0)||(flag==0))
{
printf("NODE DOES NOT EXIST!\n");
}
}
void preorder(struct Node *root)
{if(root==0)
return;
printf("%d\t",root->data);
preorder(root->left);
preorder(root->right);
}
void inorder(struct Node *root)
{if(root==0)
return;
inorder(root->left);
printf("%d\t",root->data);
inorder(root->right);
}
void postorder(struct Node *root)
{if(root==0)
return;
postorder(root->left);
postorder(root->right);
printf("%d\t",root->data);
}
void main()
{root=0;
int key,ch,c;
root=createnode();
do
{
printf("\n\tMenu");
printf("\n1.Insert a node");
printf("\n2.Delete a node");
printf("\n3. Preorder Traversal");
printf("\n4. Inorder Traversal");
printf("\n5. Postorder Traversal");
printf("\n6. Exit");
printf("\nEnter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
root=createnode();
break;
case 2:
delete();
break;
case 3: printf("Preorder traversal is:-\n");
preorder(root);
break;
case 4: printf("Inorder traversal is:-\n");
inorder(root);
break;
case 5: printf("Postorder traversal is:-\n");
postorder(root);
break;
case 6:
c=1;
}
}while(c!=1);
}