In a company, there are management ladders, such as CEO, Director, Manager, Team
ID: 3854395 • Letter: I
Question
In a company, there are management ladders, such as CEO, Director, Manager, Team Leader, Software Engineer, etc. Assume each employee has only one superior, and manages several lower-level employees (one level lower). For example, a team leader reports to one manager, and manages zero or more software engineers. CEO is the highest role and does not need to report to anyone. Software Engineer is the lowest role, no one reports to Software Engineers.
a)You want to store all employee’s names in this company. Design a data structure to represent an Employee. (hint: tree)
b)Assuming all the employees are stored based on your data structure, give an algorithm to print everyone’s name in this company. (hint: tree traversal)
c)Following the previous problems, give an algorithm to find out how many employees in each role. (hint: BFS)
ALL CODES IN JAVA!!!
Explanation / Answer
import java.util.LinkedList;
import java.util.Queue;
class Node {
String position;
Node left;
Node right;
public Node(String position) {
this.position = position;
this.left = null;
this.right = null;
}
}
class Company_Search {
public void BFS(Node root) {
Queue<Node> q = new LinkedList<Node>();
if (root == null)
return;
q.add(root);
while (!q.isEmpty()) {
Node n = (Node) q.remove();
System.out.print(" " + n.position + " ");
if (n.left != null)
q.add(n.left);
if (n.right != null)
q.add(n.right);
}
}
public static void main(String[] args) throws java.lang.Exception {
Node root = new Node("CEO");
root.left = new Node("Director1");
root.right = new Node("Director2");
root.left.left = new Node("Manager1");
root.left.right = new Node("Manager2");
root.left.left.left = new Node("TeamLeader1");
root.left.right.left = new Node("TeamLeader2");
root.left.left.left.left = new Node("SoftwareEngineer1");
root.left.right.left.left = new Node("SoftwareEngineer2");
Company_Search i = new Company_Search();
System.out.println("Hierarchy is as follows: ");
i.BFS(root);
}
}