Accessing objects in a hierarchy?

  • Thread starter Thread starter Dave Veeneman
  • Start date Start date
D

Dave Veeneman

I have an object hierarchy that's several layers deep, and I'm puzzling over
how to access an object at any point in the hierarchy, solely by its key.

The hierarchy is similar to an organizational chart, with a CEO at the top,
his/her subordinates in the next layer, their subordinates in the layer
below that, and so on. Each person is represented by an Employee object, and
Each Employee object has a Subordinates collection property (a dictionary),
which contains references to the Employee objects that represent their
subordinates, keyed by EmployeeID. So, the Employee objects are distributed
among a number of Subordinate collections.

Here's my problem: I need to get to any Employee object in the hierarchy,
based solely on its EmployeeID. I won't have a complete key path down the
hierarchy to the object. What's the best way to do this?

The obvious solution to me is to create another dictionary that contains
object references to all Employee objects, which I would use to access
employees by ID number. But is there a better solution? Thanks.
 
Dave Veeneman said:
I have an object hierarchy that's several layers deep, and I'm puzzling over
how to access an object at any point in the hierarchy, solely by its key.

The hierarchy is similar to an organizational chart, with a CEO at the top,
his/her subordinates in the next layer, their subordinates in the layer
below that, and so on. Each person is represented by an Employee object, and
Each Employee object has a Subordinates collection property (a dictionary),
which contains references to the Employee objects that represent their
subordinates, keyed by EmployeeID. So, the Employee objects are distributed
among a number of Subordinate collections.

Here's my problem: I need to get to any Employee object in the hierarchy,
based solely on its EmployeeID. I won't have a complete key path down the
hierarchy to the object. What's the best way to do this?

The obvious solution to me is to create another dictionary that contains
object references to all Employee objects, which I would use to access
employees by ID number. But is there a better solution? Thanks.

No, that's absolutely the right way to go.
 
Well, you could to a recursive loop

private Employee FindEmployee(int idNumber)
{
Employee emp = SearchEmployees(CEO.Subordinates, idNumber);
return emp;
}

private Employee SearchEmployees(Subordinates sub, int id)
{
foreach(Employee CurrentEmployee in CEO.Employees)
{
if(CurrentEmployee.ID == id)
return CurrentEmployee;
if(CurrentEmployee.Subordinates.Count > 0)
{
Employee check = SearchEmployees(CurrentEmployee.Subordinates,
idNumber);
if(check != null)
return check;
}
}
return null;
}

I haven't tested this, and since an employee can have another employee
subordinate, which in turn could have the first employee as its
subordinate (rather odd, but programmatically legal) you run the risk of
an eternal loop.
 
I did realize that I can probably use an ArrayList, rather than a
dictionary, for the Subordinates property, since I won't be looking up
subordinates by key.
 
Hmmm... So the benefit would be that I wouldn't need extra dictionaries, and
the disadvantage would be the overhead involved in the search. I suppose
that if I access employees by ID rarely, the recursive search makes the most
sense (avoid the hassle of maintaining an extra dictionary). But if I access
frequently by ID, the extra-dictionary approach makes the most sense (faster
access).
 
Back
Top