-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment6.c
More file actions
executable file
·143 lines (123 loc) · 3.8 KB
/
Assignment6.c
File metadata and controls
executable file
·143 lines (123 loc) · 3.8 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
#include <stdio.h>
#include <stdlib.h>
//Program that simulates a single elimination tournament running
//in a bst. Output represents an 'excitement' level. Basic BST
//functions come from Dr. Meade's example code, unique functions are
//my own.
//Will Mitchell CS1 April 2021
//global var tracks number of null nodes reached in bst
long long int counter = 0;
typedef struct Node Node;
struct Node{
long long int identifier;
long long int victor;
long long int excitement;
Node *l, * r;
};
//returns new node
Node * createNode(long long int identifier){
Node * ret = calloc(1, sizeof(Node));
ret->identifier = identifier;
ret->r = ret->l = NULL;
return ret;
}
//returns the root of the tree
Node * insert(Node * root, long long int identifier){
if (root == NULL){
return createNode(identifier);
}
//Don't add duplicates (optional)
if (root->identifier == identifier){
return root;
}
//root is smaller than our target
if (root->identifier < identifier){
root->r = insert(root->r, identifier);
}
//root is larger than our target
else{
root->l = insert(root->l, identifier);
}
return root;
}
//returns larger of two ints
long long int isGreater(long long int a, long long int b){
long long int ret;
if (a >= b){
ret = a;
}
else ret = b;
return ret;
}
//assign stucts a victor and excitement level
long long int simulate(Node * root, long long int * skillArr){
//if root is null, pull skill from array and return it
if(root == NULL){
long long int tmp = skillArr[counter];
counter++;
return tmp;
}
//recursive calls return skill levels of children's winner
long long int a = simulate(root->l, skillArr);
long long int b = simulate(root->r, skillArr);
//calculate winner
root->victor = isGreater(b, a);
//calculate excitement
root->excitement = abs(a - b);
return root->victor;
}
//calculater total tournament excitement lvl
long long int getExcitement(Node * root, long long int excitement){
//if NULL, there's no excitement
if (root == NULL){
return 0;
}
//excitement is the sum of curr root's excitement, and excitement of children
excitement = root->excitement + getExcitement(root->l, root->excitement) + getExcitement(root->r, root->excitement);
return excitement;
}
//free all memory other than long long int arrays in main
void destroyTree(Node * root){
if (root == NULL){
return;
}
destroyTree(root->l);
destroyTree(root->r);
free(root);
}
int main(){
long long int numPlayers;
long long int *tableArr, *skillArr;
Node * root = NULL;
scanf("%lli", &numPlayers);
//unique case for boring 1 player tournaments
if(numPlayers == 1){
printf("%i\n", 0);
return 0;
}
tableArr = (long long int*)calloc(numPlayers - 1, sizeof(long long int));
skillArr = (long long int*)calloc(numPlayers, sizeof(long long int));
//fill table activation arr in post order
for(long long int i = numPlayers - 2 ; i >= 0; i--){
scanf("%lli", &tableArr[i]);
}
//insert table identifiers into bst
for(long long int i = 0; i < numPlayers - 1; i++){
root = insert(root, tableArr[i]);
}
//fill player skill arr
for(long long int i = 0; i < numPlayers; i++){
scanf("%lli", &skillArr[i]);
}
//tournament simulate function returns
//skill of winner, but parameters of assignment
//dictate we dont need to do anything with it
long long int winner = simulate(root, skillArr);
//get excitment of tournament, excitement starts at 0
printf("%lli\n", getExcitement(root, 0));
//free all memory
destroyTree(root);
free(tableArr);
free(skillArr);
return 0;
}