Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
580 changes: 580 additions & 0 deletions .idea/editor.xml

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ add_executable(NextFlick main.cpp
admin.h
Media.h
Film.h
Series.h)
Series.h
CompressedTrie.cpp
CompressedTrie.h)
135 changes: 135 additions & 0 deletions CompressedTrie.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//
// Created by ZBook Fury on 23/01/2025.
//

#include "CompressedTrie.h"

int CompressedTrie::findCommonPrefix(const string &str1, const string &str2) {
int i = 0;
while (i < str1.size() && i < str2.size() && str1[i] == str2[i]) {
++i;
}
return i;
}

void CompressedTrie::insert(Media *film) {
Node* current = root;
string remainingKey = film->getName();

while (!remainingKey.empty()) {
bool isEdgeFound = false;

for (auto it = current->children.begin(); it != current->children.end(); ++it) {
string edgeLabel = it->first;
int commonPrefixLength = findCommonPrefix(remainingKey, edgeLabel);

if (commonPrefixLength > 0) {
isEdgeFound = true;

if (commonPrefixLength == edgeLabel.size()) {
current = it->second;
remainingKey = substring(remainingKey, commonPrefixLength);
} else {
Node* newNode = new Node();
string remainingEdge = substring(edgeLabel, commonPrefixLength);

Node* oldChild = it->second;
current->children.erase(it);
current->children[edgeLabel.substr(0, commonPrefixLength)] = newNode;

newNode->children[remainingEdge] = oldChild;

if (remainingKey.size() > commonPrefixLength) {
string remainingKeyPart = substring(remainingKey, commonPrefixLength);
newNode->children[remainingKeyPart] = new Node();
newNode->children[remainingKeyPart]->isEnd = true;
newNode->children[remainingKeyPart]->mediaMap[film->getName()] = film;
} else {
newNode->isEnd = true;
newNode->mediaMap[film->getName()] = film;
}
}
break;
}
}

if (!isEdgeFound) {
current->children[remainingKey] = new Node();
current->children[remainingKey]->isEnd = true;
current->children[remainingKey]->mediaMap[film->getName()] = film;
return;
}
}

current->isEnd = true;
current->mediaMap[film->getName()] = film;

// Debug: Print the tree structure after inserting each film
cout << "Inserted: " << film->getName() << endl;
printTree(root, "");
}

void CompressedTrie::printTree(Node *node, const string &prefix) {
if (!node) return;

for (const auto& child : node->children) {
cout << prefix << child.first << " -> ";
for (const auto& media : child.second->mediaMap) {
cout << media.first << " ";
}
cout << endl;
printTree(child.second, prefix + " ");
}
}

void CompressedTrie::collectResults(Node* node, vector<Media*>& results) {
if (node->isEnd) {
for (const auto& mediaPair : node->mediaMap) {
results.push_back(mediaPair.second);
}
}

for (const auto& child : node->children) {
collectResults(child.second, results);
}
}
vector<Media*> CompressedTrie:: search(const string& key) {
vector<Media*> results;
Node* current = root;
string remainingKey = key;

while (!remainingKey.empty()) {
bool isEdgeFound = false;


for (const auto& it : current->children) {
string edgeLabel = it.first;
int commonPrefixLength = findCommonPrefix(remainingKey, edgeLabel);

if (commonPrefixLength > 0) {
isEdgeFound = true;

if (commonPrefixLength == edgeLabel.size()) {
current = it.second;
remainingKey = substring(remainingKey, commonPrefixLength);


if (remainingKey.empty()) {
collectResults(current, results);
}
} else {
collectResults(it.second, results);
remainingKey.clear();
break;
}
break;
}
}

if (!isEdgeFound) {
break;
}
}

return results;
}
64 changes: 64 additions & 0 deletions CompressedTrie.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Created by ZBook Fury on 23/01/2025.
//

#ifndef COMPRESSEDTRIE_H
#define COMPRESSEDTRIE_H

#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include "Media.h"
using namespace std;

class Node {
public:
unordered_map<string, Node*> children;
unordered_map<string, Media*> mediaMap;
bool isEnd;

Node() : isEnd(false) {}
};


class CompressedTrie {
private:
Node* root;


string substring(const string& str, int index) {
return str.substr(index);
}

int findCommonPrefix(const string& str1, const string& str2);

public:
// Constructor for the trie
CompressedTrie() {
root = new Node();
}

/* ~CompressedTrie() {
deleteTree(root);
}*/

/*void deleteTree(Node* node) {
if (!node) return;
for (auto& child : node->children) {
deleteTree(child.second);
}
delete node;
}*/

Node* getRoot() {
return root;
}
void insert(Media* film);
void printTree(Node* node, const string& prefix);
vector<Media*> search(const string& query);
void collectResults(Node* node, vector<Media*>& results);

};

#endif //COMPRESSEDTRIE_H
5 changes: 3 additions & 2 deletions Film.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include "Media.h"
class Film : public Media {
private:
int duration;
int duration{};

public:
Film(const std::string& name, int releaseYear, int duration, const std::string& country,
const std::string& genre, const std::string& language, double rating, const std::string& summary)
: Media(name, releaseYear, country, genre, language, rating, summary), duration(duration) {}

Film(const std::string &name) : Media(name) {}
void displayDetails() const override {
std::cout << "Film: " << name << "\n"
<< "Year: " << releaseYear << "\n"
Expand All @@ -25,5 +25,6 @@ class Film : public Media {
<< "Rating: " << rating << "\n"
<< "Summary: " << summary << "\n";
}

};
#endif //NEXTFLICK_FILM_H
4 changes: 2 additions & 2 deletions Media.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class Media {
public:
Media(const std::string& name, int releaseYear, const std::string& country,const std::string& genre, const std::string& language, double rating, const std::string& summary)
: name(name), releaseYear(releaseYear), country(country),genre(genre), language(language), rating(rating), summary(summary) {}

Media(const std::string& name):name(name) {}
virtual ~Media() = default;

std::string getName() const { return name; }
virtual void displayDetails() const = 0;
};

Expand Down
Loading