-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreest 2.c
More file actions
221 lines (186 loc) · 5.81 KB
/
treest 2.c
File metadata and controls
221 lines (186 loc) · 5.81 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include<stdio.h>
#include "treeform.h"
#include<string.h>
#include<stdlib.h>
struct treest *temptree=NULL;
struct node *tempdown=NULL;
struct node *templine=NULL;
void createRight()
{
struct node *nodetemp=head;
struct treest *temproot=NULL;
struct node *temp=NULL;
while(nodetemp!=NULL)
{
printf("enterd while]\n");
struct treest *q=malloc(sizeof(struct treest));
int length=strlen(nodetemp->store);
q->info=malloc(length);
strncpy(q->info,nodetemp->store,length);
q->right=NULL;
q->left=NULL;
if(treeroot==NULL)
{
treeroot=q;
temproot=treeroot;
printf("Created root\n");
}
else
{
temproot->right=q;
printf("on pass root is %s and its right is %s\n",temproot->info,temproot->right->info);
}
temp=nodetemp->right;
if(temp!=NULL)
{
struct treest *wcr=createDependent(q,nodetemp->right);
temp=temp->right;
}
temproot=q;
nodetemp=nodetemp->down;
}
}
struct treest *createDependent(struct treest *treetrav,struct node *nodetrav)
{
printf("entered dependent funtion\n");
struct node *nodetemp=nodetrav;
// struct treest *p=malloc(sizeof(struct treest));
int fcase=1;
struct treest *treeleft=treetrav;
while(nodetemp!=NULL)
{
struct treest *p=malloc(sizeof(struct treest));
int length=strlen(nodetemp->store);
printf("length is %d string is %s\n",length,nodetemp->store);
p->info=malloc(length);
strncpy(p->info,nodetemp->store,length);
p->right=NULL;
p->left=NULL;
struct node *presentif=findPos(p);
if(presentif!=NULL)
{
printf("node present is %s \n",presentif->store);
treeleft=expandifPresent(treeleft,presentif,fcase);
fcase=0;
}
else
{
if(fcase)
{
treeleft->left=p;
fcase=0;
printf("in facse inserted to the left.info is %s\n",treeleft->left->info);
treeleft=p;
printf("treeleft in fcase is now %s\n",treeleft->info);
}
else
{
printf("common else treeleft is now %s\n",treeleft->info);
if(treeleft->left==NULL)
{
treeleft->left=p;
printf(" inserted to the left.info is %s\n",treeleft->left->info);
}
else if(treeleft->right==NULL)
{
printf("came here\n");
treeleft->right=p;
printf(" inserted to the right.info is %s\n",treeleft->right->info);
}
else
{
printf("in last else\n");
printf("treeleft is now %s\n",treeleft->info);
treeleft=treeleft->left;
printf(" inserted to the left.info is %s\n",treeleft->left->info);
treeleft->left=p;
}
}
}
nodetemp=nodetemp->right;
}
return treeleft;
}
struct node *findPos(struct treest *q)
{
struct node *nodetemp=head;
while(nodetemp!=NULL)
{
if(strcmp(nodetemp->store,q->info)==0)
return nodetemp;
nodetemp=nodetemp->down;
}
return NULL;
}
struct treest *expandifPresent(struct treest *treeexpand,struct node *downpos,int fcase)
{
printf("entered expand funtion\n");
struct node *nodetemp=downpos->right;
struct treest *treeleft=treeexpand;
while(nodetemp!=NULL)
{
struct treest *p=malloc(sizeof(struct treest));
int length=strlen(nodetemp->store);
printf("expand lengt is %d string is %s\n",length,nodetemp->store);
p->info=malloc(length);
strncpy(p->info,nodetemp->store,length);
p->right=NULL;
p->left=NULL;
if(fcase)
{
treeleft->left=p;
fcase=0;
printf("in facse inserted to the left.info is %s\n",treeleft->left->info);
treeleft=p;
printf("treeleft in fcase is now %s\n",treeleft->info);
}
else
{
printf("expand common else treeleft is now %s\n",treeleft->info);
if(treeleft->left==NULL)
{
treeleft->left=p;
printf("expand inserted to the left.info is %s\n",treeleft->left->info);
}
else if(treeleft->right==NULL)
{
printf("expand came here\n");
treeleft->right=p;
printf(" expand inserted to the right.info is %s\n",treeleft->right->info);
}
else
{
printf("expand in last else\n");
printf("expand treeleft is now %s\n",treeleft->info);
treeleft=treeleft->left;
printf(" expand inserted to the left.info is %s\n",treeleft->info);
treeleft->left=p;
}
}
nodetemp=nodetemp->right;
}
return treeleft;
}
void printTree()
{
printf("**************************FINAL RESULT***************\n");
struct treest *temp=treeroot;
struct treest *temp2=treeroot;
while(temp!=NULL)
{
printf("\ninfo is %s \n",temp->info);
temp2=temp->left;
printf("To its left is\n");
printLeft(temp2);
temp=temp->right;
}
}
void printLeft(struct treest *root)
{
if(root!=NULL)
{
printLeft(root->left);
printLeft(root->right);
printf("%s ",root->info);
}
}