0

I'm having trouble editing fields/properties in a hierarchical list with the help of a second lookup list. Everything I try looks like a mess and is hard to maintain. Are binary search trees my friend? How would I apply them? Flattening my hierarchical structure doesn't work as I need the hierarchy, just filtered.

public class Filter
{
  public string Name {get;set;}
  public bool IsActive {get;set;}
  public bool IsDisplayed {get;set;}
  public string List<Filter> Filters {get;set}
}

public List<string> ActiveFilters {"Green", "LighterYellow", "Red", "Meep")};

My base filters basically look like this:

  • Colors
    • Green
    • Yellow
      • LightYellow
        • LighterYellow
    • OrangishYellow
    • Red
      • DarkRed
  • Sounds
    • Meep
    • Moop
    • Maap

Now I'm trying to set some of these filters to active. But of course the parents of children who are in the ActiveFilters need to be displayed (IsDisplayed) otherwise you wouldn't be able to see the child items.

In the end I want the hierarchy to look like this (based on applying the ActiveFilters, bold indicates IsActive, italic IsDisplayed):

  • Colors
    • Green
    • Yellow
      • LightYellow
        • LighterYellow
    • Red
  • Sounds
    • Meep

I have been trying to recursively loop through the hierarchical list, but I don't know how to set the parents to IsDisplayed (as I would have to "go back up"). When looping the other way through the ActiveFilters I don't want to iterate over the whole filter list for each active filter performance wise.

Dennis G
  • 21,405
  • 19
  • 96
  • 133
  • Any reason you can't add a `Parent` property and traverse using that? – Steve Czetty Mar 24 '15 at 21:51
  • I can define the structure as I deem fit, so I could also add a `Parent` (e.g. `Parent = "Yellow"` for `LightYellow`) - how would that help me as I still would need to traverse more than one down from `LighterYellow`? – Dennis G Mar 24 '15 at 21:54
  • there are so many of these type of question! the easy solution is you give Filter an Id and ParentId and just have one flat list which you pass in to Filter.GetChildern(List allFilters) – Ewan Mar 24 '15 at 21:59
  • @Ewan you're right many http://stackoverflow.com/questions/4079665/how-do-i-efficiently-search-this-hierarchical-structure questions http://stackoverflow.com/questions/2117404/searching-hierarchical-list - yet I still have the problem. Care of linking some more questions if there are some questions with a similar solution? – Dennis G Mar 25 '15 at 07:38
  • the answer is don't have an hierarchical structure. – Ewan Mar 25 '15 at 09:07

1 Answers1

0

I would do something like this:

First, add a Parent property to Filter

public class Filter
{
  public string Name {get;set;}
  public bool IsActive {get;set;}
  public bool IsDisplayed {get;set;}
  public string List<Filter> Filters {get;set}

  // Parent
  public Filter Parent {get;set;}
}

In your initialization code for the hierarchy, make sure you populate Parent correctly. (I don't know what your code looks like, so I'll leave that part to you.)

Next, in your code that highlights the active filters, do something like:

var parent = activeFilter.Parent;
while (parent != null)
{
    parent.IsDisplayed = true;
    parent = parent.Parent;
}
Steve Czetty
  • 6,147
  • 9
  • 39
  • 48