I was using a approch where I catch the Last Element (Id=5) first and then checking all other nodes to get its Parent Nodes. n Complexity comes to be as n*n-1, Is there any other better approach as this approach is not a succes when we have lasrge data in Database.
Database Structure is flexible, we can change that, if there is something better...
Looking for your help and support.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
从您的输出结构来看,我假设您的数据代表一棵树。因此,您有一个简单的“获取节点的所有子节点”请求,如
SELECT childId, name FROM tree WHERE ParentId = ?
,它允许您执行标准的树遍历。伪代码:如果每个节点运行一个查询太多(可能就是这种情况),则一种优化是运行单个查询来获取所有数据并将其放入优化的数据结构中:
From your output structure, I assume your data represents a tree. You therefore have a simple "get all children of a node" request as
SELECT childId, name FROM tree WHERE parentId = ?
, which lets you perform a standard tree traversal. Pseudocode:An optimization, if running one query per node is too much (which probably is the case) is to run a single query to fetch all the data and place it in an optimized data structure: