-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbinarySearchTree_part2.c
More file actions
162 lines (148 loc) · 4.12 KB
/
Copy pathbinarySearchTree_part2.c
File metadata and controls
162 lines (148 loc) · 4.12 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// YouTube video https://youtu.be/1Qk6if4pl6s?si=_bZmtBlu7Mo71xVS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct BinaryTreeNode{
int highScore;
char username[100];
struct BinaryTreeNode* leftChild;
struct BinaryTreeNode* rightChild;
}bst;
bst* add(bst* root, bst*node){
if (root == NULL){ //empty tree
root = node;
}
else if (strcmp(root->username, node->username) < 0){
//new user name goes on right side of tree
if (root->rightChild == NULL){
root->rightChild = node;
}
else{
add(root->rightChild, node);
}
}
else if (strcmp(root->username, node->username) > 0){
//new user name goes on left side of tree
if (root->leftChild == NULL){
root->leftChild = node;
}
else{
add(root->leftChild, node);
}
}
else{
printf("A user with that name already exists. Select a new name.\n");
free(node);
}
return root;
}
void updateTree(bst* root){
char user[100];
bst* r=root;
printf("Enter username to update: ");
scanf("%s", user);
while (r != NULL){
if (strcmp(r->username, user)==0){
printf("Enter new high score: ");
scanf("%d", &r->highScore);
return;
}
else if (strcmp(r->username, user) > 0){
//user on left side of the tree
if (r->leftChild != NULL){
r=r->leftChild;
}
}
else if (strcmp(r->username, user) < 0){
//user on right side of the tree
if (r->rightChild != NULL){
r=r->rightChild;
}
}
}
printf("User does not exist.\n");
}
void printTree(bst* root){
if (root != NULL){
//in order traversal
printTree(root->leftChild);
printf("%s - %d\n", root->username, root->highScore);
printTree(root->rightChild);
}
}
bst* findMin(bst* root) {
while (root->leftChild != NULL) {
root = root->leftChild;
}
return root;
}
bst* delete(bst* root, char username[]) {
if (root == NULL) {
printf("User not found.\n");
return root;
}
if (strcmp(username, root->username) < 0) {
root->leftChild = delete(root->leftChild, username);
} else if (strcmp(username, root->username) > 0) {
root->rightChild = delete(root->rightChild, username);
} else {
// Node to be deleted found
if (root->leftChild == NULL) {
bst* temp = root->rightChild;
free(root);
return temp;
} else if (root->rightChild == NULL) {
bst* temp = root->leftChild;
free(root);
return temp;
} else {
// Node with two children
bst* temp = findMin(root->rightChild);
strcpy(root->username, temp->username);
root->highScore = temp->highScore;
root->rightChild = delete(root->rightChild, temp->username);
}
}
return root;
}
void freeTree(bst* root) {
if (root != NULL) {
freeTree(root->leftChild);
freeTree(root->rightChild);
free(root);
}
}
int main(){
bst* root=NULL;
bst* node;
int op;
char user[100];
do{
printf("1=add 2=update 3=delete 4=list all 5=exit : ");
scanf("%d", &op);
if (op == 1){
node = (bst*) malloc(sizeof(bst));
printf("Enter a new username: ");
scanf("%s", node->username);
printf("Enter a high score: ");
scanf("%d", &node->highScore);
node->leftChild = NULL;
node->rightChild = NULL;
root = add(root, node);
}
else if (op==2){
updateTree(root);
}
else if (op==3){
printf("Enter the username to delete: ");
scanf("%s", user);
root = delete(root, user);
}
else if (op==4){
printTree(root);
}
} while (op < 5);
// Free all allocated memory before exiting
freeTree(root);
return 0;
}