-
Notifications
You must be signed in to change notification settings - Fork 1
/
BST_strings
142 lines (134 loc) · 2.31 KB
/
BST_strings
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
#include <stdio.h>
#include<stdlib.h>
struct node
{
char data[100];
struct node* left;
struct node* right;
};
struct head
{
int count;
struct node* root;
}*heads;
void insert(char val[])
{ struct node *current=(struct node*)malloc(sizeof(struct node));
struct node *prev=(struct node*)malloc(sizeof(struct node));
struct node *newnode=(struct node *)malloc(sizeof(struct node));
strcpy(newnode->data,val);
newnode->left=NULL;
newnode->right=NULL;
current=heads->root;
if(current==NULL)
{
heads->root=newnode;
printf("\nNode Inserted Successfully...");
return;
}
else
{
while(current!=NULL)
{
prev=current;
if(strlen(current->data)<strlen(val))
current=current->left;
else
current=current->right;
}
if(strlen(val)<strlen(prev->data))
prev->left=newnode;
else
prev->right=newnode;
printf("\nNode Inserted Successfully...");
return;
}
}
void delet(char val[],struct node *current,struct node *prev)
{ char del[20];
if(current==NULL)
printf("Element Not Found");
else
{
if(strlen(val)<strlen(current->data))
delet(val,current->left,current);
else if(strlen(val)>strlen(current->data))
delet(val,current->right,current);
else
{
if(current->left==NULL)
{
if(prev!=NULL)
{
if(current->data<prev->data)
{
prev->left=current->right;
free(current);
}
else
{
prev->right=current->right;
free(current);
}
}
}
else
{
if(current->right==NULL)
{
if(prev!=NULL)
{
if(current->data<prev->data)
{
prev->left=current->left;
free(current);
}
else
{
prev->right=current->left;
free(current);
}
}
}
else
{ struct node *ptr;
ptr=current->left;
while(ptr->right!=NULL)
{
ptr=ptr->right;
}
strcpy(del,current->data);
strcpy(current->data,ptr->data);
strcpy(ptr->data,del);
delet(del,current->left,prev);
}
}
}
}
}
void display(struct node *root)
{
if(root!=NULL)
{
display(root->left);
printf("%s",root->data);
display(root->right);
}
}
int main()
{
heads=(struct head *)malloc(sizeof(struct head));
clrscr();
heads->count=0;
heads->root=NULL;
insert("qwe");
insert("asd");
insert("zxc");
display(heads->root);
printf("\n");
delet("asd",heads->root,NULL);
delet("zxc",heads->root,NULL);
//delet(5,heads->root,NULL);
display(heads->root);
getch();
return 0;
}