Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we accommodate trees defined in a "bottom-up" way? (nodes pointing to parents) #28

Open
LeaVerou opened this issue Mar 25, 2024 · 2 comments

Comments

@LeaVerou
Copy link
Member

I recently came across some use cases of trees where familial relationships were defined in the inverse way of what we assume: instead of nodes pointing to their children and assuming the parents can be derived, the nodes were pointing to their parents, and the children could be derived. I cannot remember what they were, but will update this comment when I do.

How could we accommodate such use cases?

At the very least, we should be able to accommodate use cases where the parent is part of the data, and does not need to be hidden away in a weakmap.

@LeaVerou
Copy link
Member Author

LeaVerou commented Apr 1, 2024

Just realized an example of such a case, though a little far-fetched: OOP superclasses and subclasses.

Each class has a reference to its parent (super) but not to its child classes. However, referencing the children is pretty frequently needed. Consider the common pattern where we have a static factory method on the base class that gives us an instance of a suitable subclass. This means the base class needs to keep track of subclasses (children) manually.

I’ve personally had to deal with this many times:

  • In Mavo and Madata’s Backend classes
  • In Mavo’s Node class
  • In Madata’s Format class
  • In Color.js’s ColorSpace class

Typically, subclasses have to "register" themselves by calling something like BaseClass.register(SubClass) and then maintain a data structure of subclasses.

@LeaVerou
Copy link
Member Author

LeaVerou commented Apr 4, 2024

Another example of bottom-up trees is Color.js color spaces: Each color space object has a base property that points to another color space (with the exception of a few basic ones) but there are no pointers to the color spaces that use a given color space as their base.

This produces a tree that looks like this (though it would be a lot more complex today): https://colorjs.io/docs/spaces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant