/
10-recursion.c
243 lines (199 loc) · 4.46 KB
/
10-recursion.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
typedef struct BinTree {
char name[10];
int value;
struct BinTree* left;
struct BinTree* right;
} BinTree;
typedef struct LinkedList {
int value;
struct LinkedList* rest;
} LinkedList;
int rest(int n) {
return n - 1;
}
void countdown(int n) {
if (n == 0) {
printf("0\n");
} else {
printf("%d ", n);
countdown(rest(n));
}
}
void countdownEven(int n) {
if (n == 0) {
printf("0\n");
} else {
if (n % 2 == 0) {
printf("%d ", n);
}
countdownEven(rest(n));
}
}
int sumEven(int n) {
if (n == 0) {
return 0;
} else {
if (n % 2 == 0) {
return n + sumEven(rest(n));
} else {
return sumEven(rest(n));
}
}
}
int sum(LinkedList* head) {
if (head == NULL) {
return 0;
} else {
int countSelf = head->value;
int countRest = sum(head->rest);
return countSelf + countRest;
}
}
bool isAllPositive(LinkedList* head) {
if (head == NULL) {
return true;
} else {
if (head->value > 0) {
return isAllPositive(head->rest);
} else {
return false;
}
}
}
bool isAllPositiveLessReadableButShorterWithALongerFunctionName(LinkedList* head) {
return (head == NULL)
|| ((head->value > 0) && isAllPositiveLessReadableButShorterWithALongerFunctionName(head->rest));
}
void printList(LinkedList* head) {
if (head == NULL) {
//do nothing
} else {
printf("%d -> ", head->value);
printList(head->rest);
}
}
void printListReverse(LinkedList* head) {
if (head == NULL) {
//do nothing
} else {
printListReverse(head->rest);
printf("%d -> ", head->value);
}
}
void doubleList(LinkedList* head) {
if (head == NULL) {
// do nothing
} else {
/* head->value = head->value * 2; */
head->value *= 2;
doubleList(head->rest);
}
}
/* stub
int maxSales(BinTree root) {
return 0;
}
*/
/*
int maxSales(BinTree root) {
if (base case) {
do something
} else {
do something for current
recursive call for next
}
}
*/
int pop(BinTree* root) {
if (root == NULL) {
return 0;
} else {
int countSelf = 1;
int countLeft = pop(root->left);
int countRight = pop(root->right);
return countSelf + countLeft + countRight;
}
}
int maxSales(BinTree* root) {
if (root == NULL) {
return 0;
} else {
int salesLeft = maxSales(root->left);
int salesRight = maxSales(root->right);
return fmax(fmax(root->value, salesLeft), salesRight);
}
}
int numLevels(BinTree* root) {
if (root == NULL) {
return 0;
} else {
return 1 + fmax(numLevels(root->left), numLevels(root->right));
}
}
void forever(int count) {
printf("In forever: %d\n", count);
forever(count + 1);
printf("Exiting forever");
}
char firstChar(char word[]) {
return word[0];
}
char lastChar(char word[], int length) {
return word[length - 1];
}
char* middle(char word[]) {
return word + 1;
}
void printWord(char word[], int length) {
for (int i = 0; i < length; i++) {
printf("%c", word[i]);
}
printf("\n");
}
bool isPalindrome(char word[], int length) {
printWord(word, length);
if (length == 0 || length == 1) {
return true;
} else {
if (firstChar(word) != lastChar(word, length)) {
return false;
} else {
return isPalindrome(middle(word), length - 2);
}
}
}
int main() {
countdown(10);
countdownEven(10);
printf("%s %s a palindrome\n", "MADAM", isPalindrome("MADAM", strlen("MADAM")) ? "is" : "is not");
LinkedList a = {3, NULL};
LinkedList b = {-4, &a};
LinkedList c = {2, &b};
LinkedList d = {1, &c};
printf("Sum: %d\n", sum(&d));
printf("isAllPositive: %d\n", isAllPositive(&d));
printf( isAllPositiveLessReadableButShorterWithALongerFunctionName(&d) ? "All positive!\n" : "Not all positive!\n");
printList(&d);
printf("\n");
printListReverse(&d);
printf("\n");
//1 -> 2 -> -4 -> 3 ->
//3 -> -4 -> 2 -> 1 ->
// BinTree* bt1 = NULL;
// BinTree bt = NULL; // can't assign NULL to struct
BinTree jean = {"Jean", 24, NULL, NULL};
BinTree jeane = {"Jeane", 27, NULL, NULL};
BinTree icel = {"Icel", 10, NULL, NULL};
BinTree candace = {"Candace", 48, &jean, &jeane};
BinTree lovely = {"Lovely", 9, &icel, NULL};
BinTree honey = {"Honey", 40, &lovely, NULL};
BinTree jj = {"JJ", 3, &honey, &candace};
printf("Population: %d\n", pop(&jj));
printf("Max sales: %d\n", maxSales(&jj));
printf("Num levels: %d\n", numLevels(&jj));
return 0;
}