-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinHeap.h
More file actions
58 lines (47 loc) · 1.36 KB
/
MinHeap.h
File metadata and controls
58 lines (47 loc) · 1.36 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
/*
MinHeap.h
Author: [Your Name]
Roll Number: [Your Roll Number]
Project Title: Xonix Game - DSA Project
Description:
This file contains the MinHeap class implementation for the leaderboard system.
*/
#ifndef MINHEAP_H
#define MINHEAP_H
#include <string>
#include <ctime>
struct PlayerScore {
std::string username;
int score;
time_t timestamp;
PlayerScore() : score(0), timestamp(0) {}
PlayerScore(const std::string& uname, int s) : username(uname), score(s), timestamp(time(nullptr)) {}
};
class MinHeap {
private:
static const int MAX_SIZE = 10; // Keep top 10 players
PlayerScore* heap;
int size;
// Helper functions
void heapifyUp(int index);
void heapifyDown(int index);
int parent(int i) const { return (i - 1) / 2; }
int leftChild(int i) const { return 2 * i + 1; }
int rightChild(int i) const { return 2 * i + 2; }
void swap(int i, int j);
public:
// Constructor and Destructor
MinHeap();
~MinHeap();
// Core operations
void insert(const std::string& username, int score);
PlayerScore removeMin();
PlayerScore getMin() const;
bool isEmpty() const;
int getSize() const;
// Leaderboard specific operations
void updateScore(const std::string& username, int newScore);
PlayerScore* getTopPlayers() const;
void clear();
};
#endif // MINHEAP_H