I am trying to think of an efficient way to synchronize a tree's nodes with the following rules.
Consider for instance this tree:
so the top of the heap is always
B which can have many leaf-type nodes called
G, or children which are node themselves called
S, which can have leaves as well (
it is possible in the future that each
G will have many leaves, but I assume the algorithm will be the same
G has a list of
categories, that I want to syncronize (add and remove) with their parents.
g1 has categories
B will have
[a,b,c], but if
g2 has categories
B will have
[a,b,c,g,w] (all the categories of its children)
S nodes' leaves have categories it goes the same, each
G of an
S node will have categories that will propagate to their parent (which will get all the categories of its children) and in return will propagate the categories up to
B will have a union of all categories below it.
if any of the
G leaves changes categories, or if the a
G is added or an
S is added the entire tree/heap need to syncronize according the new categories added.
I need the categories to be syncronized when added or removed. So every time a category is added I need the parent to get the categories of its children, which sort of scan the whole tree again
Is there an efficient way to do this?