Interesting tree/hierarchical data structure problem

后端 未结 6 885
無奈伤痛
無奈伤痛 2020-12-16 05:24

Colleges have different ways of organizing their departments. Some schools go School -> Term -> Department. Others have steps in between, with the longe

6条回答
  •  时光说笑
    2020-12-16 05:50

    Here's one design possibility:

    This option takes advantage of your special constraints. Basically you generalize all hierarchies as that of the longest form by introducing generic nodes. If school doesn't have "sub campus" then just assign it a generic sub campus called "Main". For example, School -> Term -> Department can be thought of same as School -> Sub_Campus = Main -> Program=Main -> Term -> Division=Main -> Department. In this case, we assign a node called "Main" as default when school doesn't have that nodes. Now you can just have a boolean flag property for these generic nodes that indicates that they are just placeholders and this flag would allow you to filter it out in middle layer or in UX if needed.

    This design will allow you to take advantage of all relational constraints as usual and simplify handling of missing node types in your code.

提交回复
热议问题