How to display flat data structure into hierarchical data structure (Java)?

前端 未结 5 1702
我在风中等你
我在风中等你 2021-02-02 15:11

I have recently faced this question in a practical test for a job .

Suppose you are given a flat data structure like this :

**Category**         **Name**         


        
5条回答
  •  一个人的身影
    2021-02-02 15:28

    public class FileReader {
        Map employees;
        Employee topEmployee;
        class Employee {
            int id;
            int mgrId;
            String empName;
            List subordinates;
            public Employee(String id, String mgrid, String empName) {
                try {
                    int empId = Integer.parseInt(id);
                    int mgrId = 0;
                    if (!"Null".equals(mgrid)) {
                        mgrId = Integer.parseInt(mgrid);
                    }
                    this.id = empId;
                    this.mgrId = mgrId;
                    this.empName = empName;
                } catch (Exception e) {
                    System.out.println("Unable to create Employee as the data is " + id + " " + mgrid + " " + empName);
                }
            }
    
            List getSubordinates() {
                return subordinates;
            }
            void setSubordinates(List subordinates) {
                this.subordinates = subordinates;
            }
            int getId() {
                return id;
            }
    
            void setId(int id) {
                this.id = id;
            }
    
            int getMgrId() {
                return mgrId;
            }
    
        }
    
        public static void main(String[] args) throws IOException {
            FileReader thisClass = new FileReader();
            thisClass.process();
        }
    
        private void process() throws IOException {
            employees = new HashMap();
            readDataAndCreateEmployees();
            buildHierarchy(topEmployee);
            printSubOrdinates(topEmployee, tabLevel);
        }
    
        private void readDataAndCreateEmployees() throws IOException {
            BufferedReader reader = new BufferedReader(new java.io.FileReader("src/main/java/com/springapp/mvc/input.txt"));
            String line = reader.readLine();
            while (line != null) {
                Employee employee = createEmployee(line);
                employees.put(employee.getId(), employee);
                if (employee.getMgrId() == 0) {
                    topEmployee = employee;
                }
                line = reader.readLine();
            }
        }
    
        int tabLevel = 0;
    
        private void printSubOrdinates(Employee topEmployee, int tabLevel) {
            for (int i = 0; i < tabLevel; i++) {
                System.out.print("\t");
            }
            System.out.println("-" + topEmployee.empName);
            List subordinates = topEmployee.getSubordinates();
            System.out.print(" ");
            for (Employee e : subordinates) {
                printSubOrdinates(e, tabLevel+1);
            }
        }
        public List findAllEmployeesByMgrId(int mgrid) {
            List sameMgrEmployees = new ArrayList();
            for (Employee e : employees.values()) {
                if (e.getMgrId() == mgrid) {
                    sameMgrEmployees.add(e);
                }
            }
            return sameMgrEmployees;
        }
    
        private void buildHierarchy(Employee topEmployee) {
            Employee employee = topEmployee;
            List employees1 = findAllEmployeesByMgrId(employee.getId());
            employee.setSubordinates(employees1);
            if (employees1.size() == 0) {
                return;
            }
    
            for (Employee e : employees1) {
                buildHierarchy(e);
            }
        }
    
        private Employee createEmployee(String line) {
            String[] values = line.split(" ");
            Employee employee = null;
            try {
                if (values.length > 1) {
                    employee = new Employee(values[0], values[2], values[1]);
                }
            } catch (Exception e) {
                System.out.println("Unable to create Employee as the data is " + values);
            }
            return employee;
        }
    }
    

提交回复
热议问题