-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab.c
143 lines (113 loc) · 2.57 KB
/
lab.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
//This code is by Thanah Raveendran
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
int count;
struct node *left, *right;
}node;
node* newNode (int data, int count)
{
node *temp;
temp = (node*) malloc (sizeof (node));
temp->data = data;
temp->count = count;
temp->left = NULL;
temp -> right = NULL;
return temp;
free (temp);
}
node* insert(node *root, node *element)
{
if (root == NULL)
{
return element;
}
else {
if (element->data > root->data)
{
if (root->right != NULL)
root->right = insert(root->right, element);
else
root->right = element;
}
else
{
if (root->left != NULL)
root->left = insert(root->left, element);
else
root->left = element;
}
return root;
}
}
void inOrder (node *current, FILE *fp)
{
if (current != NULL)
{
inOrder (current->left, fp);
fprintf (fp, "(");
putw (current->data, fp);
fprintf (fp, ", ");
putw (current->count, fp);
fprintf (fp, ") ");
printf ("(%d, %d", current->data, current->count);
inOrder (current->right, fp);
}
}
node* maxVal(node *root)
{
if (root->right == NULL)
return root;
else
return maxVal(root->right);
}
int single (node *root)
{
int count;// = 0;
if (root ==NULL)
return 0;
//while (node != NULL)
if ((root->left ==NULL && root->right != NULL) || (root->left != NULL && root->right == NULL))
count ++;
count += (single(root->left)+single (root->right));
return count;
}
int main ()
{
int i, N, income, count;
printf ("test1");
node *root = NULL;
node *temp, *max;
FILE *fp, *fpo;
fp = fopen ("in.txt", "r");
fpo = fopen ("out.txt", "w");
printf ("test1.2");
N = getw (fp);
for (i=0; i<N; i++)
{
printf ("test3\n");
/*fscanf (fp, "%d", income);
fscanf (fp, "%d", count);*/
income = getw (fp);
count = getw(fp);
temp = newNode (income, count);
root = insert (root, temp);
}
fprintf (fpo, "In-Order: " );
printf ("In order: ");
inOrder (root, fpo);
fprintf (fpo,"\n");
fprintf (fpo,"Highest income: ");
max = maxVal (root);
printf ("\nHighest Income: %d\nTotal people with highest income: %d", max->data, max->count);
putw (max->data, fpo);
fprintf (fpo,"\nTotal People with highest income: ");
putw (max->count, fpo);
fprintf (fpo,"\n");
printf ("\n");
fclose (fp);
fclose (fpo);
return 0;
}