0

I have to maintain a the employee structure of a company. Each employee has associated with it a unique name (no two with the same name) and a level (integer). the level denotes where the person stands in the hierarchy. Level 1 = highest (only 1 employee can be at level 1, but there can be several employees at level i > 1). Each level i employee works under a level i-1 employee, who is his/her immediate boss. Now, given any employee say A, there is/are employees A', A''... such that A works under A', A' works under A'' and so on. I need to create a suitable tree structure.

I need to implement following methods:

addEmployee(new, boss) - new will work immediately under boss

delEmployee(e1, e2) - e1 will be removed and all employees under e1 will now work under e2. (note - e1 and e2 are given to be at the same level)

lowestCommonBoss(e1, e2) - self explanatory

printEmployees() - print names of all employees level wise

Now what i did was i create a node class:

public class Node {
    String element;
    Node parent;
    protected ArrayList<Node> children;
    protected int level;

    Node(String e) {
    this.element = e;
    this.parent = null;
    this.children = new Arraylist();
    this.level = 1;
    }
}

Now, what kind of tree structure should i follow? Is this the node i am looking for?

Saket Mehta
  • 2,438
  • 2
  • 23
  • 28
  • Seems matching your requirements, except the constructor. Make your constructor generic: consider other cases except the root. Is element the only information you want to keep? – Vaibhav Raj Jun 30 '13 at 10:11
  • Your question is too generic and don't specifies if you are using database. If so , if you will need to generate statistics or witch other functionality. Please specify. – surfealokesea Jun 30 '13 at 10:12
  • @VaibhavRaj yes, i guess so. all i need is the name of the employee as a string. – Saket Mehta Jun 30 '13 at 10:15
  • @surfealokesea no i am not using a database or anything of the sort. adding the employees will be done in the main() method itself. – Saket Mehta Jun 30 '13 at 10:17
  • @Saket: use Set children instead of ArrayList children and override the equals and hashCode method of the Node class, as your requirement says that every employee is unique. I think you are considering uniqueness on the property element. – Vaibhav Raj Jun 30 '13 at 10:22
  • @VaibhavRaj is right you can also override constructor – surfealokesea Jun 30 '13 at 10:23
  • Yeah that's fine. But I wanted to focus on the algorithm for addEmployee(and other methods) and what kind of tree to use. I guess I could modify this node class at any point of time according to the needs. – Saket Mehta Jun 30 '13 at 10:34

1 Answers1

0

Yes, you could use any tree structure you wish. The structure you have mentioned would work according to me.

apupak
  • 1
  • 2