Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6367251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:31:29+00:00 2026-05-25T00:31:29+00:00

I have a class Department: class Department{ string des; string name; string id; List<Department>

  • 0

I have a class Department:

class Department{
  string des;
  string name;
  string id;
  List<Department> subDeps;
}

As the code shown,one department may have several sub-departmens.

Now,I have read the information from database,and I get the root Department object.

Then I want to generate a excel worksheet like this manner:

enter image description here

In fact,The root "Department" object is a tree structure .

When I try to write the items to the excel sheet,I have to decide the rows and cols for the item,but I can not make it.

And one can do me a favor?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:31:30+00:00Added an answer on May 25, 2026 at 12:31 am

    Look at it this way:

    0 00 000 | (0,0) (0,1) (0,2)
         001 |             (1,2)
    1 10 100 | (2,0) (2,1) (2,2)
         101 |             (3,2)
      11     |       (4,1)
    

    I used the same syntax as you to name the nodes (left side) and the corresponding position “(row,column)” into the grid (right side). There are two top level departments here, 0 and 1.
    You can label your nodes with “(x,y)” coordinates with a depth first visit for every tree/top level department. Whenever you descend from father to child, you have to increase the column number by 1.
    Every time you visit a new sibling your row index must refer to a new, empty one (in the example, the sibling of 10 is 11 but you can’t put it in (3,1) because row 3 is already occupied by dept 101).

    I would write the algorithm following these guidelines. Keep track of the current (x,y) with two variables that are passed down as parameters of a recursive depth-first visit, together with the current node/department to be visited. Make sure that the function edits as required the excel representation but returns the maximum row index used (so that you can use it to know which is the following row when visiting a new sibling — as in the previous example).
    Every time you make a recursive call you have to call it with coords (x, y+1) because it’s a new column. In a similar manner, when you visit a new sibling you call it with (coords prev_call_retn+1, y) because it’s a new row (where coords_prev_call_retn is the value of the last recursive call on the previous sibling).
    An helper function will call the recursive one on the root node and using as base coords (0,0), or whatever you like to start from.

    #include <iostream>
    #include <list>
    #include <string>
    
    using namespace std;
    
    class Dept {
    public:
        string id;
        list<Dept*> *subDepts;
    
        Dept(string id) {
            this->id = id;
            this->subDepts = new list<Dept*>();
        }
    
        Dept *addChild(Dept *d) {
            this->subDepts->push_back(d);
            return d;
        }
    
        Dept *addChild(string id) {
            Dept *d = new Dept(id);
            return this->addChild(d);
        }
    
    };
    
    void visit(Dept *d, int row, int col) {
        cout << "Dept " << d->id  << ": (" <<  row << ", " << col << ")\n";
    }
    
    int label(Dept *d, int row, int col) {
        if (d == 0) return row;
        visit(d, row, col);
        list<Dept*> *lst = d->subDepts;
        for (list<Dept*>::iterator it = lst->begin() ; it != lst->end(); it++)
        {
            row = label(*it, row, col+1) + 1;
        }
        if (lst->size() > 0) row--;
        return row;
    }
    
    int label(Dept *d) {
        label(d, 0, 0);
    }
    

    In this C++ code, label() is the function you’re interested in.

    The parameters row and col are supposed to be the correct coordinates of the department *d, so that we can visit the node immediately.

    The loop recursively calls label() on each children (if any). Note that I use the variable row in order to keep track of the last used row + 1.

    Lastly, we have to return the last used row by the function, but being careful to subtract one if the d->subDepts is not empty. This is because the for loop that adds 1 extra row in the last iteration.

    Here is an example main:

    int main(int argc, char **argv) {
        Dept *d0   = new Dept("0");
        Dept *d00  = d0->addChild("00");
        Dept *d01  = d0->addChild("01");
        Dept *d000 = d00->addChild("000");
        Dept *d001 = d00->addChild("001");
        Dept *d002 = d00->addChild("002");
        Dept *d010 = d01->addChild("010");
        Dept *d02  = d0->addChild("02");
        label(d0);
        return 0;
    }
    

    And here the result:

    bash-4.2$ g++ dept.cpp  && ./a.out
    Dept 0: (0, 0)
    Dept 00: (0, 1)
    Dept 000: (0, 2)
    Dept 001: (1, 2)
    Dept 002: (2, 2)
    Dept 01: (3, 1)
    Dept 010: (3, 2)
    Dept 02: (4, 1)
    

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a User class which may or may not have an associated Department.
I have two classes class Deptartment{ int deptid, String deptname; List<Employee> employees; } class
I have these two entities. One for Employees: [Table(Name = Employees)] public class Employee
I have two different entities, class Department { int DepID; string DepName; } class
I have a domain classes like this Class Rules{ List <Department> departments = new
Given the following classes: class Department { public String Name { get; set; }
I have class method that returns a list of employees that I can iterate
I have class Cab(models.Model): name = models.CharField( max_length=20 ) descr = models.CharField( max_length=2000 )
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
I have two classes university and department, suppose there is one to many relationship

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.