修改的先序树遍历-确定“顶部”树 当没有指定父级时

发布于 2024-07-13 08:15:56 字数 504 浏览 3 评论 0原文

我正在为网站上的类别树实现修改的预序树遍历类,但我在一种情况下遇到了麻烦。 通常,当插入新类别时,会指定顶级父类别,使用其在树中的左值来确定新类别应位于树中的位置。 但是,有时可能没有指定父类别,这意味着新类别必须位于树的顶部,位于树顶部任何其他类别的右侧。

查看具有类似结构的其他一些应用程序,其中许多似乎在安装时在树中插入“根”节点。 我想知道这是不是这样他们就不必检测它是否是第一个插入,并且他们总是有一个左参考。 任何想法或伪代码将不胜感激。 如果重要的话我会用 PHP 来做这件事。 我的树可能看起来像这样:

Electronics         Apparel         My New Category
    / \               / \
MP3     TVs    Shirts     Shoes

我的想法是,在这种情况下,Apparel 的正确值将始终是表中最大的,但我不确定如何使用它来确定它是最后一个。 任何帮助或提示将不胜感激。

I'm implementing a Modified preorder tree traversal class for a category tree on a Web site, but I'm having trouble with one scenario. Typically when inserting a new category a top-level parent is specified, whose left value in the tree is used in order to determine where the new category should go in the tree. However, there could be times when no parent is specified, which means the new category must go in at the top of the tree, to the right of any other categories at the top of the tree.

Looking at some other applications with similar structures, many of them seem to be inserting a "root" node in the tree at the time of installation. I'm wondering if this is so they don't ever have to detect if it is the first insert, and they always have a left reference. Any thoughts or pseudo-code would be much appreciated. I'm doing this in PHP if it matters. My tree might look something like this:

Electronics         Apparel         My New Category
    / \               / \
MP3     TVs    Shirts     Shoes

My thought is that in this scenario, Apparel's right value will always be the greatest in the table, but I'm not sure how I might use that to determine it is the last. Any help or hints would be appreciated.

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

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

发布评论

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

评论(3

血之狂魔 2024-07-20 08:15:56

在您提供的示例中,如果电子产品和服装没有共同的父级,那么从技术上讲,它们是两个独立的树。 如果您添加“我的新类别”,它也是一棵新树。 如果您尝试在“电子”、“服装”和“我的新类别”之间进行遍历,则需要一个高于所有三个类别的值,请说“全部”,即您的根节点。

请参阅在数据库中存储分层数据以获取包含以下内容的示例:枚举树和示例或实际存储在数据库中。

In the example you've presented, Electronics and Apparel are technically two separate trees if they have no common parent. If you add "My New Category" it's also a new tree. If you're trying to traverse between Electronics, Apparel and My New Category you need a value above all three, say "All" that is your root node.

See Storing Hierarchical Data in a Database for a example with both a enumerated tree and examples or actual storage in the database.

绅刃 2024-07-20 08:15:56

存储此类数据的另一种非常有效的方法是使用 嵌套集,它只需要一个查询就可以进行许多常见的操作,并且无需其他方案的递归。

Another very efficient way of storing that kind of data is using Nested Sets, it allows many common operations with just one query, and eliminates the need for recursion of other schemes.

神妖 2024-07-20 08:15:56

插入时。 运行一个查询,按 LEFT 排序,取最后一个,即最后一个根类别,将其命名为 last_tree。 插入新树时,为其指定左值为last_tree + 1,右值为last_tree + 2。

查看此处的 cakephp 示例:

http://bakery.cakephp.org/articles/view/modified-preorder-tree-traversal-component

When inserting. Run a query, ordered by LEFT, take the last one, that is your last root category, call it last_tree. When inserting your new tree, give it a left value of last_tree + 1 and right value of last_tree + 2.

Take a look here for a cakephp example:

http://bakery.cakephp.org/articles/view/modified-preorder-tree-traversal-component

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