-
Notifications
You must be signed in to change notification settings - Fork 2
/
MM_2.cpp
274 lines (226 loc) · 7.12 KB
/
MM_2.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
// Book structure
class Book {
public:
string title;
string author;
string genre;
int pageCount;
Book() {}
Book(string title, string author, string genre, int pageCount)
{
this->title = title;
this->author = author;
this->genre = genre;
this->pageCount = pageCount;
}
};
// Book Database class
class Database {
public:
unordered_map<string, Book> books;
void addBook(Book newBook);
Book getBook(string title);
};
void Database::addBook(Book newBook)
{
books[newBook.title] = newBook;
}
Book Database::getBook(string title)
{
return books[title];
}
// Node class for k-ary tree
class listNode {
public:
vector<Book> readingList;
vector<listNode *> children;
listNode(vector<Book> readingList)
{
this->readingList = readingList;
}
};
// ReadingList class for managing k-ary tree of reading lists
class ReadingList {
public:
listNode *root;
int k; // Degree of the k-ary tree
ReadingList(int k)
{
root = NULL;
this->k = k;
}
void addList(vector<Book> readingList);
void displayLists();
void removeBook(string title);
void reorderBooks(vector<string> newOrder);
// Additional functions for k-ary tree operations
void createFolder();
void moveListToFolder(listNode *list, listNode *folder);
};
// Function declarations for k-ary tree operations
listNode *insertList(listNode *root, vector<Book> newList, int k);
void inOrderTraversal(listNode *root);
listNode *removeBookHelper(listNode *root, string title);
listNode *reorderBooksHelper(listNode *root, vector<string> newOrder);
// Function definitions for k-ary tree operations
listNode *insertList(listNode *root, vector<Book> newList, int k)
{
if (root == NULL)
{
return new listNode(newList);
}
// If the current node has less than k children, insert the new list as a child
if (root->children.size() < k)
{
root->children.push_back(new listNode(newList));
}
else
{
// Find the first child with fewer than k children and insert the new list there
for (auto &child : root->children)
{
if (child->children.size() < k)
{
child = insertList(child, newList, k);
return root;
}
}
// If all children have k children, create a new child and insert the new list
root->children.push_back(new listNode(newList));
}
return root;
}
void inOrderTraversal(listNode *root)
{
if (root == NULL)
return;
// Display the contents of the current node
for (const auto &book : root->readingList)
{
cout << "Title: " << book.title << ", Author: " << book.author << ", Genre: " << book.genre << ", Page Count: " << book.pageCount << endl;
}
// Traverse each child
for (auto child : root->children)
{
inOrderTraversal(child);
}
}
listNode *removeBookHelper(listNode *root, string title)
{
if (root == NULL)
return root;
// Find the book to be removed and remove it from the reading list
root->readingList.erase(remove_if(root->readingList.begin(), root->readingList.end(),
[title](const Book &book) { return book.title == title; }),
root->readingList.end());
// Remove any child nodes that have become empty after the book removal
root->children.erase(remove_if(root->children.begin(), root->children.end(),
[](listNode *child) { return child->readingList.empty() && child->children.empty(); }),
root->children.end());
// Recur for each remaining child
for (auto &child : root->children)
{
child = removeBookHelper(child, title);
}
return root;
}
listNode *reorderBooksHelper(listNode *root, vector<string> newOrder)
{
if (root == NULL)
return root;
vector<Book> reorderedList;
// Reorder the books based on the new order
for (const auto &title : newOrder)
{
auto it = find_if(root->readingList.begin(), root->readingList.end(),
[title](const Book &book) { return book.title == title; });
if (it != root->readingList.end())
{
reorderedList.push_back(*it);
}
}
// Replace the existing reading list with the reordered one
root->readingList = reorderedList;
// Recur for each remaining child
for (auto &child : root->children)
{
child = reorderBooksHelper(child, newOrder);
}
return root;
}
// Function definitions for ReadingList class
void ReadingList::addList(vector<Book> readingList)
{
root = insertList(root, readingList, k);
}
void ReadingList::displayLists()
{
inOrderTraversal(root);
}
void ReadingList::removeBook(string title)
{
root = removeBookHelper(root, title);
}
void ReadingList::reorderBooks(vector<string> newOrder)
{
root = reorderBooksHelper(root, newOrder);
}
void ReadingList::createFolder()
{
// Create a new list node as a folder
listNode *folder = new listNode(vector<Book>());
root->children.push_back(folder);
}
void ReadingList::moveListToFolder(listNode *list, listNode *folder)
{
// Remove the list from its current position within its parent
auto it = find(root->children.begin(), root->children.end(), list);
if (it != root->children.end())
{
root->children.erase(it);
// Add the list to the specified folder
folder->children.push_back(list);
}
}
int main()
{
// Initialize your book database and reading list
Database bookDB;
ReadingList readingLists(3); // Create a k-ary tree with degree 3
// Add books to the database
Book book1("The Hobbit", "J.R.R. Tolkien", "Fantasy", 300);
Book book2("To Kill a Mockingbird", "Harper Lee", "Fiction", 350);
Book book3("1984", "George Orwell", "Dystopian", 400);
bookDB.addBook(book1);
bookDB.addBook(book2);
bookDB.addBook(book3);
// Create a reading list
vector<Book> myReadingList = {book1, book2};
readingLists.addList(myReadingList);
// Display reading lists
cout << "Initial Reading Lists:" << endl;
readingLists.displayLists();
// Remove a book from the reading list
readingLists.removeBook("To Kill a Mockingbird");
cout << "\nReading List after removing a book:" << endl;
readingLists.displayLists();
// Reorder books in the reading list
vector<string> newOrder = {"1984", "The Hobbit"};
readingLists.reorderBooks(newOrder);
cout << "\nReading List after reordering books:" << endl;
readingLists.displayLists();
// Create a folder and move the reading list to the folder
readingLists.createFolder();
cout << "\nReading List after creating a folder:" << endl;
readingLists.displayLists();
listNode *folder = readingLists.root->children[0];
readingLists.moveListToFolder(readingLists.root, folder);
cout << "\nReading List after moving to folder:" << endl;
readingLists.displayLists();
return 0;
}