Created
April 3, 2020 17:01
-
-
Save pettan93/c31de384b167219ef638204aebe56fa8 to your computer and use it in GitHub Desktop.
complexity of this algorithm?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public TreeNode parseHiearchy(List<OrganizationUnit> organizationUnits) { | |
HashMap<String, OrganizationUnit> divisions = new HashMap<>(); | |
HashMap<String, List<OrganizationUnit>> departments = new HashMap<>(); | |
HashMap<String, List<OrganizationUnit>> jobs = new HashMap<>(); | |
for (OrganizationUnit organizationUnit : organizationUnits) { | |
switch (organizationUnit.getOrganizationUnitType()) { | |
case DIVISION: { | |
divisions.put(organizationUnit.getPrefix(), organizationUnit); | |
break; | |
} | |
case DEPARTMENT: { | |
departments.computeIfAbsent(getPrefixAbove(organizationUnit.getPrefix()), k -> new ArrayList<>()) | |
.add(organizationUnit); | |
break; | |
} | |
case JOB: { | |
jobs.computeIfAbsent(getPrefixAbove(organizationUnit.getPrefix()), k -> new ArrayList<>()) | |
.add(organizationUnit); | |
break; | |
} | |
} | |
} | |
TreeNode root = new TreeNode(); | |
root.isRoot = true; | |
for (String divisionPrefix : divisions.keySet()) { | |
TreeNode divisionNode = new TreeNode(divisions.get(divisionPrefix)); | |
root.addChild(divisionNode); | |
for (OrganizationUnit department : departments.get(divisionPrefix)) { | |
TreeNode departmentNode = new TreeNode(department); | |
divisionNode.addChild(departmentNode); | |
for (OrganizationUnit job : jobs.get(department.getPrefix())) { | |
TreeNode jobNode = new TreeNode(job); | |
departmentNode.addChild(jobNode); | |
} | |
} | |
} | |
return root; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment