递归函数的顺序编号?例如 2、2.1、2.1.1、2.2、2.2.1

发布于 2024-08-29 09:26:40 字数 278 浏览 5 评论 0原文

我有一个递归函数从数据库中读取文档的“目录”。 我想用反映该项目在树中位置的文档打印编号,例如

1. First item,
    1.1 Child of first item,
        1.1.1 Child of child of first item,
    1.2 Child of first item,
2. Second item,
    2.1 Child of second item,

等。

目前对此感到困惑 - 请帮忙?

I have a recursive function reading a "table of contents" of documents from a database.
I would like to print numbering with the document that reflects where the item is in the tree, e.g.

1. First item,
    1.1 Child of first item,
        1.1.1 Child of child of first item,
    1.2 Child of first item,
2. Second item,
    2.1 Child of second item,

etc.

Rather stumped about this at the moment - help please?

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

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

发布评论

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

评论(3

梦幻的心爱 2024-09-05 09:26:40

查看您的代码会很有用。假设数据以某种分层表示形式存储,则递归的结构可能如下所示:

void PrintTOC(string prefix, List<Sections> sections) {
  // Iterate over all sections at the current level (e.g. "2")
  for(int i = 0; i<sections.Length; i++) {
    // Get prefix for the current section (e.g. "2.1")
    string num = String.Format("{0}.{1}", prefix, i+1);
    // Write the current section title
    Console.WriteLine("{0} {1}", num, sections[i].Titles);

    // Recursively process all children, passing "2.1" as the prefix
    if (sections[i].Children != null)
      PrintTOC(num, sections[i].Children);
  }
}

这保留了一个包含父节索引的前缀参数。当前部分中的所有数字都附加在此前缀之后。

It would be useful to see your code. Assuming that the data is stored in some hierarchical representation, the structure of the recursion might look like this:

void PrintTOC(string prefix, List<Sections> sections) {
  // Iterate over all sections at the current level (e.g. "2")
  for(int i = 0; i<sections.Length; i++) {
    // Get prefix for the current section (e.g. "2.1")
    string num = String.Format("{0}.{1}", prefix, i+1);
    // Write the current section title
    Console.WriteLine("{0} {1}", num, sections[i].Titles);

    // Recursively process all children, passing "2.1" as the prefix
    if (sections[i].Children != null)
      PrintTOC(num, sections[i].Children);
  }
}

This keeps a prefix parameter that contains the index of the parent section. All numbers in the current section are appended after this prefix.

舞袖。长 2024-09-05 09:26:40

只需在函数中包含一个“路径”参数并随时添加即可。伪代码:

function print_rec(String path, Node node) {
  print(path + node.title)
  for (int i=1; i<=node.children.length; i++) {
    print_rec(path+"."+i, node.children[i])
  }
}

Simply include a "path" argument in the function and add to it as you go. Pseudo-code:

function print_rec(String path, Node node) {
  print(path + node.title)
  for (int i=1; i<=node.children.length; i++) {
    print_rec(path+"."+i, node.children[i])
  }
}
神也荒唐 2024-09-05 09:26:40

你的数据库平台是什么?如果您使用的是 SQL Server 2005 或更高版本,则可以使用单个 CTE 查询从“树”表(具有自引用键的表)获取此类路径。 (还有基于 XML 的技术用于在 SQL Server 中运行递归查询。)

否则,您将必须在客户端代码中执行递归,正如其他人所建议的那样。在这种情况下,您可能需要考虑在开始递归并使用对象遍历结构之前选择所有相关数据。这样您就可以避免对数据库进行潜在的大量查询。

What's your database platform? If you're using SQL Server 2005 or higher, you can get this kind of path from a "tree" table (one with a self-referencing key) using a single CTE query. (There are also XML-based techniques for running recursive queries in SQL Server.)

Otherwise, you're going to have to perform the recursion in the client code, as others have suggested. In that case, you might want to consider selecting all of the relevant data before starting the recursion and walking the structure using the object. This way you can avoid a potentially huge number of queries against the database.

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