/
1050_seminar9.cpp
129 lines (109 loc) · 2.8 KB
/
1050_seminar9.cpp
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
#include<iostream>
struct Casino {
int noSlotMachines;
char* name;
};
Casino createCasino(char* name, int number) {
Casino c;
c.name = (char*)malloc(sizeof(char)*(strlen(name) + 1));
strcpy(c.name, name);
c.noSlotMachines = number;
return c;
}
void printCasino(Casino c){
printf("Casino %s has %d slots machines.\n", c.name, c.noSlotMachines);
}
struct node {
Casino info;
node*left;
node*right;
};
node* insertCasinoInTree(Casino c, node* root) {
if (root) {
if (c.noSlotMachines < root->info.noSlotMachines) {
root->left = insertCasinoInTree(c, root->left);
}
else if (c.noSlotMachines > root->info.noSlotMachines) {
root->right = insertCasinoInTree(c, root->right);
}
return root;
}
else {
node* newNode = (node*)malloc(sizeof(node));
newNode->info = c;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
}
void printTree(node* root) {
if (root) {
printTree(root->left);
printCasino(root->info);
printTree(root->right);
}
}
int height(node* root) {
if (root) {
int left = height(root->left);
int right = height(root->right);
return (left > right) ? left + 1 : right + 1;
}
else {
return 0;
}
}
Casino searchCasinoByNo(node* root, int numberSlotMachines) {
if (root) {
if (numberSlotMachines < root->info.noSlotMachines) {
return searchCasinoByNo(root->left, numberSlotMachines);
}
else if (numberSlotMachines > root->info.noSlotMachines) {
return searchCasinoByNo(root->right, numberSlotMachines);
}
else {
return root->info;
}
}
else {
return createCasino("", -1);
}
}
void freeTree(node**root) {
if (*root) {
freeTree(&(*root)->left);
freeTree(&(*root)->right);
free((*root)->info.name);
free(*root);
*root = NULL;
}
}
void printNodesFromALevel(node* root, int desiredLevel, int currentLevel) {
if (root) {
if (desiredLevel == currentLevel) {
printCasino(root->info);
}
else {
currentLevel++;
printNodesFromALevel(root->left, desiredLevel, currentLevel);
printNodesFromALevel(root->right, desiredLevel, currentLevel);
}
}
}
void main() {
node* root = NULL;
root = insertCasinoInTree(createCasino("MaxBet", 6), root);
root = insertCasinoInTree(createCasino("Admiral", 3), root);
root = insertCasinoInTree(createCasino("MaxBet 1", 4), root);
root = insertCasinoInTree(createCasino("BlingBet", 8), root);
root = insertCasinoInTree(createCasino("Maximus", 7), root);
root = insertCasinoInTree(createCasino("Fortuna", 11), root);
root = insertCasinoInTree(createCasino("Bonus", 9), root);
printTree(root);
Casino c = searchCasinoByNo(root,7);
printf("\n");
printCasino(c);
printf("Height: %d\n\n", height(root));
printNodesFromALevel(root, 3, 1);
freeTree(&root);
}