Programming Assignment 2 - TextFile
Programming Assignment 2 - TextFile
CENG 213
Data Structures
Fall 2023
Programming Assignment 2
1 Objectives
In this assignment, the objective is to implement a specialized dictionary utilizing a unique data
structure for managing user-specific data on watched films. The primary structure of the dictionary
will be a trie comprising binary trees. Specifically, the trie will be responsible for managing user IDs
(keys), while the watched movies associated with each user will be stored as Binary Search Trees
(BSTs) serving as the corresponding values. Each node denoting a user ID in the Trie will point to
a uniquely associated Binary Search Tree as the set of movies watched by that user (value).
A Trie, short for retrieval tree or digital tree, is a tree-like data structure that is used to store a
dynamic set of keys, typically strings. Each node in the trie represents a single character of a key,
and the paths from the root to the leaf nodes represent the keys themselves. The key associated
with a node is obtained by concatenating the characters along the path from the root to that node.
Tries are particularly useful for efficient search operations, as the time complexity for searching,
inserting, and deleting keys is often proportional to the length of the key, making them well-suited
for tasks such as autocomplete features or dictionary implementations.
The implementation follows a dual-phase approach: first, the trie manages and organizes user IDs,
and second, the binary search trees associated with each trie node store the watched movies for the
respective users. This design facilitates the efficient organization and retrieval of watched films for
each user.
1
Figure 1: Specialized Two-Phase structure
and it facilitates the efficient organization and retrieval of watched films for individual users.
The combination of these two phases, where Trie handles user-specific organization and BST man-
ages movie data for each user, provides a specialized two-phase structure. This structure optimizes
the organization of the watched films database, enabling quick and efficient access to individual
users’ movie data based on their viewing history.
2.1.2 Trie();
The Trie class constructor initializes a new trie with a root node, using the null character (’\0’) as
the initial key character. This sets the foundation for the trie data structure, enabling the insertion
of keys and the construction of the trie hierarchy.
2.1.3 ∼Trie();
The destructor for the Trie class is responsible for freeing the memory allocated for the trie nodes.
It calls the private member function deleteTrieNode(root) to recursively traverse the trie and
delete each node. This ensures proper memory cleanup, preventing memory leaks associated with
the trie data structure.
2
given prefix. This method provides an efficient way to retrieve keys that share a common starting
sequence.
(a) This pattern matches any key that contains ”abc” as a substring with any characters
before and after it.
(b) Example Result: ”xabcz”, ”abc123”, ”defabcghi”
(a) This pattern matches any key that starts with ”a”, followed by any character, end with
any single character .
(b) Example Result: ”abc1”, ”axcf”, ”act”
(a) This pattern matches any key that starts with ”a”, followed by any single character,
and has zero or more characters afterward.
(b) Example Result: ”abc”, ”abcmnop”
3
2.1.9 void print(const std::string &primaryKey);
The ‘print‘ method in the ‘Trie‘ class serves to print either the entire trie or specific keys based on
the provided primary key. If the primary key is empty, indicating a request to print the entire tree,
it invokes the ‘printTrie‘ function to recursively print the trie starting from the root. If a specific
primary key is provided, it traverses the trie to the corresponding node, printing the subtree rooted
at that node. If the primary key is not found in the trie, it prints an empty set of braces.
2.2.2 BST();
The code declares a default constructor for the Binary Search Tree (BST) class, initializing a new
instance of the class.
2.2.3 ∼BST();
The code declares a destructor for the Binary Search Tree (BST) class, responsible for resource
cleanup when an instance is no longer needed.
4
2.2.7 bool search(std::string key) const;
The code presents a ‘search‘ method in the Binary Search Tree (BST) class. This method looks for
a node with a specified key in the tree, starting from the root. The method returns true if a match
is found and false otherwise.
2.2.13 void print(TreeNode* node, std::string indent, bool last, bool isLeftChild);
This function, when given a TreeNode pointer and specific parameters, prints the key of the current
node and recursively calls itself for the left and right children. This process creates a hierarchical
representation of the BST structure, facilitating visualization of its organization.
5
2.3 Movie Class
2.3.1 Movie();
The ‘Movie()’ constructor is a default constructor for the ‘Movie‘ class. It initializes a ‘Movie‘
object with default values: an empty string for ‘movieName’, 0 for ‘year’, and 0.0 for ‘rating’. This
constructor is used when an object of the ‘Movie‘ class is created without providing explicit values.
6
2.4.4 BST<Movie> *getMovies();
‘getMovies’ returns a pointer to the private member variable ‘movies’, which is a binary search tree
(BST). It provides controlled access to the movie data within the class.
2.5 UserInterface
2.5.1 void addUser(std::string username);
The ‘addUser’ method inserts a new user with the given username into the trie data structure in
the ‘UserInterface’ class.
7
2.5.3 void addWatchedMovie(std::string username, Movie movie);
The ‘addWatchedMovie’ method adds a watched movie to a user identified by the given ‘username‘.
It first attempts to find the user using the ‘findUser‘ method. If the user is found, it prints the
username and then adds the movie using the ‘addMovie’ method of the user. If the user is not
found (returns ‘NULL’), it prints ”NULL” to the console.
8
2.5.11 std::vector<User> findUsersEndingWith(std::string suffix);
‘findUsersEndingWith’ in ‘UserInterface’ locates users with usernames ending in a specified suffix. It
uses the ‘wildcardSearch’ method, populates a vector (‘foundUsersEndingWith’), and prints count
and details.The wildcard pattern can contain two types of wildcards: ‘?’ and ‘*’. The ‘?’ wildcard
matches any single character in the input string, while the ‘*’ wildcard matches zero or more
characters.
3 Regulations
1. Programming Language: You will use C++.
3. External libraries other than those already included are not allowed.
4. Those who do search, update, remove operations without utilizing the tree will receive 0
grade.
5. Those who modify already implemented functions and those who insert other data variables
or public functions and those who change the prototype of given functions will receive 0 grade.
6. Those who use STL vector or compile-time arrays or variable-size arrays (not existing in
ANSI C++) will receive 0 grade. Options used for g++ are -ansi -Wall -pedantic-errors
-O0.
8. Late Submission: Each student receives 5 late days for the entire semester. You may use
late days on programming assignments, and each allows you to submit up to 24 hours late
without penalty. For example, if an assignment is due on Thursday at 11:30 pm, you could
use 2 late days to submit on Saturday by 11:30 pm with no penalty. Once a student has used
up all their late days, each successive day that an assignment is late will result in a loss of 5
No assignment may be submitted more than 3 days (72 hours) late without permission from
the course instructor. In other words, this means there is a practical upper limit of 3 late days
usable per assignment. If unusual circumstances truly beyond your control prevent you from
submitting an assignment, you should discuss this with the course staff as soon as possible.
If you contact us well in advance of the deadline, we may be able to show more flexibility in
some cases
9. Cheating: We have zero tolerance policy for cheating. In case of cheating, all parts involved
(source(s) and receiver(s)) get zero. People involved in cheating will be punished according
to the university regulations.
9
10. Remember that students of this course are bounded to code of honor and its violation is
subject to severe punishment.
11. Newsgroup: You must follow ODTUClass for discussions and possible updates on daily
basis.
4 Submission
• Submission will be done via Moodle.
– You can submit your source files to CengClass and test your work with a subset of
evaluation inputs and outputs.
– Additional test cases will be used for evaluation of your final grade, and only the last
submission before the deadline will be graded.
– Only the last submission before the deadline will be graded.
10