-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathBinaryTree.c
193 lines (180 loc) · 3.5 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include <stdlib.h>
//tree declaration
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
typedef struct Node node;
node* createnode(int data)
{
node* new_node = ((node*)malloc(sizeof(node)));
new_node->data=data;
new_node->left=new_node->right=NULL;
return new_node;
}
//queue declaration
node* queue[100]={};
int l=-1,r=-1;
void push(node* data)
{
if(r==100){
printf("queue is already full.");
}else if(r==-1){
l=0;
r=r+1;
queue[r]=data;
}else{
r=r+1;
queue[r]=data;
}
}
void pop()
{
if(l==-1 && r==-1){
printf("queue is already empty.\n");
}
else if (l<r){
l++;
}else{
l=r=-1;
}
}
void display()
{
for (int i = l; i <= r; i++)
{
printf("%d ",queue[i]->data);
}
}
// insertion
node* insert(node *root,int data)
{
if (root==NULL){
root = createnode(data);
return root;
}
push(root);
int len=r+1;
while (len!=0)
{
node *temp = queue[l];
printf("%d\n",temp->data);
pop();
if(!temp->left){
temp->left=createnode(data);
return root;
}else{
push(temp->left);
}
if (!temp->right)
{
temp->right=createnode(data);
return root;
}else{
push(temp->right);
}
len=r-l+1;
}
}
//deletion
void deepest_deletion(node *root,node *d_node){
node *temp;
push(root);
int size=r+1;
while (size!=0)
{
temp=queue[l];
pop();
if(temp==d_node){
temp=NULL;
free(d_node);
return;
}
if (temp->left==d_node)
{
temp->left=NULL;
free(d_node);
return;
}else{
push(temp->left);
}
if (temp->right==d_node)
{
temp->right=NULL;
free(d_node);
return;
}else{
push(temp->right);
}
size=r-l+1;
}
}
node *delete(node *root, int data)
{
if (root==NULL)
return root;
if (root->left==NULL && root->right==NULL){
if (root->data==data)
return NULL;
else
return root;
}
push(root);
node *temp,*d_node;
node *key_node=NULL;
int size =r+1;
while (size!=0)
{
temp=queue[l];
d_node=queue[r];
pop();
if (temp->data==data){
key_node=temp;
}
if(temp->left!=NULL){
push(temp->left);
}
if (temp->right!=NULL)
{
push(temp->right);
}
if(l==-1 && r==-1){
key_node->data=d_node->data;
deepest_deletion(root,d_node);
break;
}
size=r-l+1;
}
return root;
}
//traversing
void inordertraversal(node *root)
{
if (root==NULL)
{
return ;
}
inordertraversal(root->left);
printf("%d ",root->data);
inordertraversal(root->right);
}
//main
int main()
{
node* root = createnode(10);
root->left = createnode(11);
root->left->left = createnode(7);
root->left->right = createnode(12);
root->left->right->right = createnode(69);
root->right = createnode(9);
root->right->left = createnode(15);
root->right->right = createnode(8);
inordertraversal(root);
root=delete(root,12);
printf("\n");
inordertraversal(root);
return 0;
}