1. Introduction / Aim

In this assignment, you are expected to design a Login System with Character Tree. The primary goal of this experiment is to get you to practice on the tree data structure.

2. Background Information

Strings can essentially be viewed as the most important and common topics for a variety of programming problems. String processing has a variety of real-world applications too, such as:

  • Search Engines
  • Genome Analysis
  • Data Analytics

All the content presented to us in textual form can be visualized as nothing but just strings.

A Character tree is a special form of tree data structure that is based on the prefix of a string. The prefix of a string is nothing but any n letters, n <=|S| that can be considered beginning strictly from the starting of a string.

A character tree is a special data structure used to store strings that can be visualized like a graph. It consists of nodes and edges. Each node consists of at max 26 children and edges connect each parent node to its children. These 26 pointers are pointers for each of the 26 letters of the English alphabet. A separate edge is maintained for every node.

Strings are stored in a top to bottom manner on the basis of their prefix in a character tree. All prefixes of length 1 are stored at until level 1, all prefixes of length 2 are sorted at until level 2 and so on.

For example, consider the following diagram : see image.

Now, one would be wondering why to use a data structure such as a character tree for processing a single string? Actually, Character trees are generally used on groups of strings, rather than a single string. When given multiple strings, we can solve a variety of problems based on them. For example, consider an English dictionary and a single string s, find the prefix of maximum length from the dictionary strings matching the string s. Solving this problem using a naive approach would require us to match the prefix of the given string with the prefix of every other word in the dictionary and note the maximum. This is an expensive process considering the amount of time it would take. Character trees can solve this problem in a much more efficient way. Before processing each Query of the type where we need to search the length of the longest prefix, we first need to add all the existing words into the dictionary. A Character tree consists of a special node called the root node. This node doesn't have any incoming edges. It only contains max 26 outgoing edges for each letter in the alphabet and is the root of the character tree.

So, the insertion of any string into a character tree starts from the root node. All prefixes of length one are direct children of the root node. In addition, all prefixes of length 2 become children of the nodes existing at level one.

3. Experiment

In this experiment, you are expected to write an application that constructs a Login System with Character Tree. The application will take the input.txt file in the current directory and read its contents. In order to create the Character tree, there are some commands for adding, deleting and refreshing.

Structure of Commands:

-a username password #add username to the tree with the given password
-s username #search with the given username and return the password if it is
-q username password #send query for the password with the given username
-d username #delete username and its password
-l #list the tree

With -a command; The application will read the given username character by character and add them to the tree. With the last character of the username store the password.

  • If the first character of the username is not referenced by the root node, the username will be added to the tree starting from the root node.
  • If the first n character of the username exists on the tree, a branch occurs on the n th node for the last characters
  • The node which is the last character of the username has to hold the password for the given username.
  • If there is a username same as the given username, the application will give an output that reserved username.

See example: see image.

With -s command; The application will read the given username and according to the questioned username it will return an information.

  • If the first character of the username is not referenced by the root node the application will give an output that no record.
  • If the first n character of the username exists on the tree, but the remainder is not, the application will give an output that incorrect username.
  • If all characters of the username exist on the tree, but the last character has no password, the application will give an output that not enough username.
  • If all characters of the username exist on the tree and the last character has its password, the application will give an output that password xxx.

See example: see image.

With -q command; The application will read the username and its password. According to the matching, it will return an information.

  • If the first character of the username is not referenced by the root node the application will give an output that no record.
  • If the first n character of the username exists on the tree, but the remainder is not, the application will give an output that incorrect username.
  • If all characters of the username exist on the tree, but the last character has no password, the application will give an output that not enough username.
  • If all characters of the username exist on the tree, but the last character has a different password from the given, the application will give an output that incorrect password.
  • If all characters of the username exist on the tree, and the last character has the same password with the given, the application will give an output that successful login.

See example: see image.

With -d command; With the given username the application will delete the username and its password from the tree.

  • If the first character of the username is not referenced by the root node the application will give an output that no record.
  • If the first n character of the username exists on the tree, but the remainder is not, the application will give an output that incorrect username.
  • If all characters of the username exist on the tree, but the last character has no password, the application will give an output that not enough username.
  • If all characters of the username exist on the tree, and the last character has the password, the application will delete all nodes which are not connected to another username. Then it will give an output that deletion is successful.

See example: see image.

With -l command; The application will list the all username by preorder traversal.

  • After the first level of the tree, each branch will be displayed with a new tab

See example: see image.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.