-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinHeap2.cpp
More file actions
158 lines (130 loc) · 4.45 KB
/
MinHeap2.cpp
File metadata and controls
158 lines (130 loc) · 4.45 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
/*
MinHeap.cpp
Author: [Your Name]
Roll Number: [Your Roll Number]
Project Title: Xonix Game - DSA Project
Description:
This file contains the implementation of the MinHeap class methods.
*/
#include "MinHeap.h"
#include <algorithm>
#include <iostream>
MinHeap::MinHeap() {
heap = new PlayerScore[MAX_SIZE];
size = 0;
}
MinHeap::~MinHeap() {
delete[] heap;
}
void MinHeap::swap(int i, int j) {
PlayerScore temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}
void MinHeap::heapifyUp(int index) {
while (index > 0 && heap[parent(index)].score > heap[index].score) {
swap(index, parent(index));
index = parent(index);
}
}
void MinHeap::heapifyDown(int index) {
int smallest = index;
int left = leftChild(index);
int right = rightChild(index);
if (left < size && heap[left].score < heap[smallest].score)
smallest = left;
if (right < size && heap[right].score < heap[smallest].score)
smallest = right;
if (smallest != index) {
swap(index, smallest);
heapifyDown(smallest);
}
}
void MinHeap::insert(const std::string& username, int score) {
std::cout << "MinHeap::insert - Username: '" << username << "', Score: " << score << std::endl;
if (size < MAX_SIZE) {
// If heap is not full, simply insert
std::cout << " Heap not full, inserting at position " << size << std::endl;
heap[size] = PlayerScore(username, score);
heapifyUp(size);
size++;
}
else if (score > heap[0].score) {
// If new score is higher than minimum, replace root
std::cout << " Heap full, replacing minimum score " << heap[0].score << " with " << score << std::endl;
heap[0] = PlayerScore(username, score);
heapifyDown(0);
}
else {
std::cout << " Score not higher than minimum, not inserted" << std::endl;
}
}
PlayerScore MinHeap::removeMin() {
if (isEmpty()) {
return PlayerScore();
}
PlayerScore min = heap[0];
heap[0] = heap[size - 1];
size--;
heapifyDown(0);
return min;
}
PlayerScore MinHeap::getMin() const {
if (isEmpty()) {
return PlayerScore();
}
return heap[0];
}
bool MinHeap::isEmpty() const {
return size == 0;
}
int MinHeap::getSize() const {
return size;
}
void MinHeap::updateScore(const std::string& username, int newScore) {
std::cout << "MinHeap::updateScore - Username: '" << username << "', New Score: " << newScore << std::endl;
// Find the player in the heap
for (int i = 0; i < size; i++) {
if (heap[i].username == username) {
int oldScore = heap[i].score;
std::cout << " Found existing user at index " << i << " with score " << oldScore << std::endl;
heap[i].score = newScore;
// If score increased, heapify up
if (newScore > oldScore) {
std::cout << " Score increased, heapifying up" << std::endl;
heapifyUp(i);
}
// If score decreased, heapify down
else if (newScore < oldScore) {
std::cout << " Score decreased, heapifying down" << std::endl;
heapifyDown(i);
}
return;
}
}
// If player not found, try to insert
std::cout << " User not found in heap, inserting as new entry" << std::endl;
insert(username, newScore);
}
PlayerScore* MinHeap::getTopPlayers() const {
std::cout << "MinHeap::getTopPlayers - Creating array of " << size << " players" << std::endl;
// Create a copy of the heap
PlayerScore* sortedPlayers = new PlayerScore[size];
for (int i = 0; i < size; i++) {
sortedPlayers[i] = heap[i];
std::cout << " Copying player " << i + 1 << ": " << heap[i].username << " - " << heap[i].score << std::endl;
}
// Sort in descending order
std::sort(sortedPlayers, sortedPlayers + size,
[](const PlayerScore& a, const PlayerScore& b) {
return a.score > b.score;
});
std::cout << "Sorted players by score:" << std::endl;
for (int i = 0; i < size; i++) {
std::cout << " Sorted position " << i + 1 << ": " << sortedPlayers[i].username << " - " << sortedPlayers[i].score << std::endl;
}
return sortedPlayers;
}
void MinHeap::clear() {
size = 0;
}