0% found this document useful (0 votes)
3 views17 pages

Group5 087 098 108 120

Mini project for Data structures and algotithm

Uploaded by

rohithraghu3228
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

Group5 087 098 108 120

Mini project for Data structures and algotithm

Uploaded by

rohithraghu3228
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

GAMING

LEADERBOARD SYSTEM

EFFICIENTLY MANAGING PLAYER


RANKINGS WITH BINARY SEARCH TREES

By Team: Group 5
Members :
Rohith
Nishith R
Prashanth Kulal
Pruthvi
PROBLEM STATEMENT

• The task is to create a Gaming Leaderboard that allows


administrators to:
• Add new players and their scores.
• Update existing player scores.
• View the leaderboard with players sorted by score.
• Search for a player by name.
• Delete a player from the leaderboard.
• Display the top N players based on their scores.
• The system should use a Binary Search Tree (BST) to
store and manage the players efficiently, ensuring fast
insertions, deletions, and score look-ups.
EXPLANATION OF THE DATA
STRUCTURE USED: BINARY
SEARCH TREE (BST)

• A Binary Search Tree (BST) is a tree-based data structure where:


• Each node contains two children: a left child and a right child.
• The left child contains a value less than its parent node.
• The right child contains a value greater than its parent node.
• Why BST was chosen:
• Efficient Searching: Searching for players based on scores is fast,
with a time complexity of O(log n) for balanced trees.
• Efficient Insertion and Deletion: Players can be added or removed
efficiently based on their scores. Insertion and deletion operations also
have an average time complexity of O(log n).
• Sorted Data: The BST keeps data sorted automatically, making it easy
to display the leaderboard in ascending or descending order.
• Flexible: The structure can be modified or expanded to add more
features, such as top N rankings, without significant changes to the
overall design.
ALGORITHM FOR MAIN
OPERATIONS:
• 1. Insertion:
• To add a player:
• Compare the player's score with the root node’s score.
• If the score is less, move to the left child.
• If the score is greater, move to the right child.
• Insert the new node where an empty position is found (left or right child).

• 2. Deletion:
• To remove a player:
• Find the player by name using a search.
• If the player has no children, remove the node.
• If the player has one child, replace the node with its child.
• If the player has two children, find the inorder successor (minimum node in the right subtree), copy its
data, and remove the successor.

• 3. Search:
• To find a player:
• Compare the player's name with the root node's name.
• Recursively search left or right depending on the alphabetical comparison.

• 4. Update Score:
• To update a player's score:
• Find the player by name.
• Update their score directly in the node.

• 5. Reverse Inorder Traversal:


• For displaying the top N players:
• Traverse the tree in reverse inorder (right -> root -> left), printing the top N players based on their score.
INPUT, OUTPUT, AND TEST CASES

• Inputs
• Player Name (String): Examples: "John", "Alice", "Mike".
• Player Score (Integer): Examples: 200, 150, 300.
• Number of Top Players (Integer): Examples: 3, 5.

• Outputs
• Player List:
• Example:
• Player: John, Score: 300
• Player: Alice, Score: 200
• Player: Mike, Score: 150
• Search Result:
• Example: "Player found: John, Score: 300."
• Example (non-existent): "Player not found."
• Confirmation Messages:
• Example (addition): "Player John added successfully."
• Example (deletion): "Player John has been removed from the leaderboard."
Test Cases:

Test Case ID Input Expected Output Actual Output Status

Add "John" with score "Player John added


TC1 As expected Pass
200 successfully."

Search for player "Player found: John,


TC2 As expected Pass
"John" Score: 200."

Update score for "Score updated for


TC3 As expected Pass
"John" to 250 John to 250."

Search for non-


TC4 "Player not found." As expected Pass
existent player "Jane"

"Player John has been


TC5 Delete player "John" removed from the As expected Pass
leaderboard."

"Rank 1: Alice, Score:


TC6 Display top 3 players As expected Pass
300"
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Node structure for the leaderboard


typedef struct Node {
char name[50]; // Player's name
int score; // Player's score
struct Node* left; // Left child node
struct Node* right; // Right child node
} Node;

// Function prototypes
Node* createNode(char name[], int score);
Node* insertNode(Node* root, char name[], int score);
Node* searchNode(Node* root, char name[]);
Node* deleteNode(Node* root, char name[]);
Node* findMin(Node* root);
void updateScore(Node* root, char name[], int newScore);
void inorderTraversal(Node* root);
void reverseInorderTraversal(Node* root, int* rank, int topN);
void freeTree(Node* root);
void flushInput();

// Main function
int main() {
Node* root = NULL;
int choice, score, newScore, topN;
char name[50];
while (1) {
// Display the menu
printf("\nGaming Leaderboard\n");
printf("1. Add Player\n");
printf("2. Update Player Score\n");
printf("3.View Leaderboard\n");
printf("4. Search Player\n");
printf("5. Delete Player\n");
printf("6. Display Top N Players\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
flushInput(); // Clear the input buffer
:
switch (choice) {
case 1:
printf("Enter player name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0'; // Remove newline character from name

printf("Enter player score (must be positive): ");


scanf("%d", &score);
flushInput();

// Validate score
if (score < 0) {
printf("Error: Score cannot be negative. Please enter a valid score.\n");
break;
}

root = insertNode(root, name, score);


printf("Player %s has been successfully added with score %d!\n", name, score);
break;

case 2:
printf("Enter player name to update: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0'; // Remove newline character from name

printf("Enter new score (must be positive): ");


scanf("%d", &newScore);
flushInput();

// Validate score
if (newScore < 0) {
printf("Error: Score cannot be negative. Please enter a valid score.\n");
break;
}

updateScore(root, name, newScore);


break;

case 3:
printf("\nLeaderboard:\n");
inorderTraversal(root);
break;
case 4:
printf("Enter player name to search: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0'; // Remove newline character from name

Node* found = searchNode(root, name);


if (found) {
printf("Player found: %s, Score: %d\n", found->name, found->score);
} else {
printf("Player not found. Did you mean to add them first?\n");
}
break;

case 5:
printf("Enter player name to delete: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0'; // Remove newline character from name

root = deleteNode(root, name);


printf("Player %s has been removed from the leaderboard.\n", name);
break;

case 6:
printf("Enter number of top players to display: ");
scanf("%d", &topN);
flushInput();

printf("\nTop %d Players:\n", topN);


int rank = 1;
reverseInorderTraversal(root, &rank, topN);
break;

case 7:
freeTree(root);
printf("Exiting...\n");
return 0;

default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
// Create a new node for a player
Node* createNode(char name[], int score) {
Node* newNode = (Node*)malloc(sizeof(Node));
strcpy(newNode->name, name);
newNode->score = score;
newNode->left = newNode->right = NULL;
return newNode;
}

// Insert a node into the BST based on score


Node* insertNode(Node* root, char name[], int score) {
if (!root) {
return createNode(name, score);
}

// Insert based on score, smaller score goes to the left, larger to the right
if (score < root->score) {
root->left = insertNode(root->left, name, score);
} else if (score > root->score) {
root->right = insertNode(root->right, name, score);
} else {
printf("Player with the same score already exists!\n");
}

return root;
}

// Search for a player node by name


Node* searchNode(Node* root, char name[]) {
if (!root || strcmp(root->name, name) == 0) {
return root;
}

if (strcmp(name, root->name) < 0) {


return searchNode(root->left, name);
} else {
return searchNode(root->right, name);
}
}

// Update the score of an existing player


void updateScore(Node* root, char name[], int newScore) {
Node* player = searchNode(root, name);
if (player) {
player->score = newScore;
printf("Score updated for %s to %d.\n", name, newScore);
} else {
printf("Player not found.\n");
}
}
// Find the node with the minimum score (for deletion)
Node* findMin(Node* root) {
while (root && root->left) {
root = root->left;
}
return root;
}

// Delete a node from the BST


Node* deleteNode(Node* root, char name[]) {
if (!root) {
printf("Player not found.\n");
return root;
}

// Search for the player by name


if (strcmp(name, root->name) < 0) {
root->left = deleteNode(root->left, name);
} else if (strcmp(name, root->name) > 0) {
root->right = deleteNode(root->right, name);
} else {
// Node with one or no child
if (!root->left) {
Node* temp = root->right;
free(root);
return temp;
} else if (!root->right) {
Node* temp = root->left;
free(root);
return temp;
}

// Node with two children: get the inorder successor (smallest in the right subtree)
Node* temp = findMin(root->right);
strcpy(root->name, temp->name);
root->score = temp->score;
root->right = deleteNode(root->right, temp->name);
}

return root;
}
// In-order traversal (ascending order of scores)
void inorderTraversal(Node* root) {
if (root) {
inorderTraversal(root->left);
printf("Player: %s, Score: %d\n", root->name, root->score);
inorderTraversal(root->right);
}
}

// Reverse in-order traversal (descending order of scores)


void reverseInorderTraversal(Node* root, int* rank, int topN) {
if (root && *rank <= topN) {
reverseInorderTraversal(root->right, rank, topN);
if (*rank <= topN) {
printf("Rank %d: %s, Score: %d\n", *rank, root->name, root->score);
(*rank)++;
}
reverseInorderTraversal(root->left, rank, topN);
}
}

// Free the entire BST


void freeTree(Node* root) {
if (root) {
freeTree(root->left);
freeTree(root->right);
free(root);
}
}

// Flush the input buffer to handle trailing newline characters


void flushInput() {
while (getchar() != '\n');
}
OUTPUT
CONCLUSION AND FUTURE
ENHANCEMENTS

• Conclusion
• The Gaming Leaderboard System effectively manages player
rankings using a Binary Search Tree (BST), allowing
functionalities like adding, updating, deleting, searching, and displaying
the leaderboard. It ensures efficient operations with a user-friendly
interface.
• Future Enhancements
1.Priority Queue: Implement dynamic ranking based on additional
criteria.
2.Database Integration: Store data persistently for long-term use.
3.Web Interface: Create a web-based version for online interaction.
4.Graphical Visualization: Add charts for player rankings.
5.User Profiles: Introduce authentication for tracking player
progress.

You might also like