I happen to have a database with a table that holds all possible combination of subject, verbs and complements possible.
That table looks like a junction table with Id columns mapping to my other tables (subject table, verbs table, complements table).
In my program I use a tree structure to represent the junction table and each node is therefore just an object with an Id property (subjectId or verbId …).
What I fail to understand though is where to put the actual data the Id maps to. I though I had two options:
- Make the Data a property of each Node
- Make the Data a node
In the first case the only processing I do is to load the combination table and create the tree. And when I need the data that goes with it, I load it on demand. But to keep track of the data position in the tree, the data has a property that points back to the node it belongs to.( a hack evidently to avoid having to search the whole tree (even if it is only a O(log(n)) operation). In addition,I will only be dealing with a node in the whole program since it is a convenient way to get to the children of then node.
In the second case, if I was to make the actual data a Node , I would have to load all the data at once before consuming it. besides, I still need to create a copy of the data if the “node parent siblings” are using the same data.
Is there a clean way to achieve what I am trying to do?
Listed below is what I currently have
public class Node<Word>{
public Word Data { get; set; }
public Guid ID { get; set; }
..... // other necessary tree like stuff
}
public class Word
{
public Node NodeItem { get; set; }
}
Or
public class Word : Node{}
I hope that the question is clear enough. let me know if you need more details and I will update the question with it.
Thank you.
Searching in the tree which you are mentioning will not be O(logn) in any case because this tree is not a Binary Search Tree. It’s an Undirected graph , you can say so you need to search either by using BFS or DFS.
if i am imaging your situation correctly then on UI , you have a tree suppose in a left panel of your window and on the right panel you want to show the details of the node.
if this is the UI or your situation then
i would say do not store data in the tree , load on demand i.e. when user select a node , then you will have the ID of that node ,fetch the data with the ID and display.
this way , you can avoid the possible loading of all data which may not be require at once .