-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestout.c
More file actions
98 lines (86 loc) · 1.89 KB
/
Copy pathtestout.c
File metadata and controls
98 lines (86 loc) · 1.89 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
#include "header.h"
struct node
{
char *word;
struct node *left_child,*right_child;
};
int stringsinInput = 0;
int leftcount = 0;
int rightcount = 0;
struct node *inserttree(struct node *tree, char *s){
int i;
if (tree==NULL){
if ((tree=(struct node *)malloc(sizeof (struct node)))!=NULL)
if ((tree->word=(char *) malloc(strlen(s)+1))==NULL)
{
free(tree);
tree=NULL;
}
else
{
stringsinInput++;
tree->left_child = NULL;
tree->right_child = NULL;
strcpy(tree->word,s);
}
}
else if ((i=strcmp(tree->word,s))>0){
tree->left_child=inserttree(tree->left_child,s);
leftcount++;
}
else if (i<0) {
tree->right_child=inserttree(tree->right_child,s);
rightcount++;
}
return tree;
}
void printtree(struct node *tree){
if (tree!=NULL)
{
printtree(tree->left_child);
printf("%s",tree->word);
printtree(tree->right_child);
}
}
void freetree(struct node *tree){
if (tree!=NULL)
{
free(tree->word);
freetree(tree->left_child);
freetree(tree->right_child);
free(tree);
}
}
int main(int argc, char *argv[]){
if(argc > 3 || argc < 3){
printf("Command line error\n");
exit(1);
}
FILE *f;
f = fopen(argv[1], "r");
/*if(f == NULL){
printf(stderr, "Couldn't open the file: %s\n", f);
exit(1);
} */
char s[16]; // assume 15 char
struct node *tree=NULL;
if (f == NULL){
printf("Unable to open Test.txt");
}
else
{
while (fscanf(f,"%16s",s)!=EOF)
tree=inserttree(tree,s);
fclose(f);
printtree(tree);
printf("\nNo. of strings in the input file = %d", stringsinInput);
printf("\nHeight of the search tree = --");
printf("\nNumber of leaves in the tree = --");
printf("\nHeight of the left_child subtree of root = --");
printf("\nNo. of strings in the left subtree of the root = %d", leftcount);
printf("\nHeight of the right_child subtree of root = --");
printf("\nNo. of strings in the right subtree of the root = %d\n", rightcount);
freetree(tree);
}
return 0;
}