树数据结构和数据

发布于 2024-10-05 08:16:43 字数 924 浏览 0 评论 0原文

我碰巧有一个数据库,其中有一个表,其中包含主语、动词和补语的所有可能组合。

该表看起来像一个连接表,其中 Id 列映射到我的其他表(主语表、动词表、补语表)。

在我的程序中,我使用树结构来表示连接表,因此每个节点只是一个具有 Id 属性(subjectId 或 verbId ...)的对象。

但我无法理解的是,将 Id 映射到的实际数据放在哪里。我虽然有两个选择:

  1. 使数据成为每个节点的属性
  2. 使数据成为节点

在第一种情况下,我所做的唯一处理是加载组合表并创建树。当我需要附带的数据时,我会按需加载它。但是为了跟踪树中的数据位置,数据有一个属性指向它所属的节点。(显然是为了避免搜索整个树而进行的黑客攻击(即使它只是一个 O(log( n)) 操作)。此外,我将只处理整个程序中的一个节点,因为这是获取该节点的子节点的便捷方法,

如果我要使实际数据<。 em>a Node ,我必须在使用它之前一次加载所有数据,此外,如果“节点父兄弟”使用相同的数据,我仍然需要创建数据的副本

。一种干净的方式来实现我想要做的事情? 下面列出的是我目前所拥有的

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; }
}

或者

public class Word : Node{}

我希望问题足够清楚。如果您需要更多详细信息,请告诉我,我将用它来更新问题。 谢谢。

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:

  1. Make the Data a property of each Node
  2. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个少女永远怀春 2024-10-12 08:16:43

在任何情况下,在您提到的树中搜索都不会是 O(logn),因为该树不是二叉搜索树。这是一个无向图,你可以说你需要使用 BFS 或 DFS 进行搜索。

如果我正确地想象了您的情况,那么在 UI 上,您假设在窗口的左侧面板中有一棵树,在右侧面板上您想要显示节点的详细信息。

如果这是 UI 或您的情况,那么

我会说不要将数据存储在树中,按需加载,即当用户选择一个节点时,您将获得该节点的 ID,使用该 ID 获取数据并显示。

这样,您可以避免一次性加载所有可能不需要的数据。

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文