#C1669. Company Hierarchy Management
Company Hierarchy Management
Company Hierarchy Management
You are required to implement a Company Hierarchy Management system. In this system, you will manage employees, their titles and reporting relationships. Each employee has a unique name, and optionally, a title. You must implement the following operations:
- ADD_EMPLOYEE name [title]: Adds a new employee with the given name. The title is optional.
- ASSIGN_TITLE name title: Assigns (or reassigns) a title to an existing employee.
- SET_MANAGER employee_name manager_name: Sets the manager for an employee. This operation should print
True
if successful. It printsFalse
if either the employee or manager does not exist, or if setting the manager would create a cyclic hierarchy. - GET_MANAGER name: Prints the name of the manager for the given employee, or
None
if the employee has no manager. - GET_SUBORDINATES name: Prints a space‐separated list of the direct subordinates of the employee, in alphabetical order. If there are no subordinates, print an empty line.
- PRINT_HIERARCHY: Prints the entire company hierarchy. The printed hierarchy starts from the root employees (employees with no manager) and shows the hierarchy as a tree. Each level is indented by 2 spaces. An employee is printed as "Name (Title)" if they have a title, otherwise just "Name".
The input will consist of a number of commands (each command on a separate line) that instruct you to perform these operations. Process each command in order and print any required outputs to stdout. All input is read from stdin and all output should be printed to stdout.
Note: Ensure that cyclic managerial relationships are not allowed. Use LaTeX format for any formulas if needed (none are required in this problem).
inputFormat
The input begins with an integer n, representing the number of commands. The following n lines each contain a command. Each command is one of the following:
ADD_EMPLOYEE name [title]
ASSIGN_TITLE name title
SET_MANAGER employee_name manager_name
GET_MANAGER name
GET_SUBORDINATES name
PRINT_HIERARCHY
Arguments in a command are separated by spaces. The title in ADD_EMPLOYEE is optional and, if missing, the employee is added without a title.
outputFormat
For commands that produce output, print the result on a new line. The outputs are as follows:
- SET_MANAGER: Print
True
if the manager is successfully set; otherwise, printFalse
. - GET_MANAGER: Print the manager's name, or
None
if there is no manager. - GET_SUBORDINATES: Print the direct subordinates' names in alphabetical order separated by a space (print an empty line if there are none).
- PRINT_HIERARCHY: Print the entire hierarchy, one employee per line. Employees should be printed as "Name (Title)" if they have a title, and just "Name" if not. Each level in the hierarchy is indented by 2 spaces.
For commands that do not require output (e.g. ADD_EMPLOYEE, ASSIGN_TITLE), nothing should be printed.## sample
6
ADD_EMPLOYEE Alice CEO
ADD_EMPLOYEE Bob CTO
SET_MANAGER Bob Alice
GET_MANAGER Bob
GET_SUBORDINATES Alice
PRINT_HIERARCHY
True
Alice
Bob
Alice (CEO)
Bob (CTO)
</p>