用 XML 表示树的最佳方式是什么?

发布于 2024-10-07 09:52:15 字数 657 浏览 0 评论 0原文

我正在实现一些类来处理常见的数据结构(树、二叉树、搜索二叉树、BTree、AVL 等)。我想要一种将树信息存储在数据源中并将其解析为其中一个类的方法。最简单的方法是使用XML(可移植性非常重要)。问题是我制作的 XML 文档不容易阅读。他们不够“直观”。例如,我正在做这样的事情:

<?xml version="1.0" encoding="UTF-8"?>
<tree>
    <root>100</root>
    <node>
        <tree>
            <root>50</root>     
        </tree>
    </node>
    <node>
        <tree>
            <root>200</root>        
        </tree>
    </node>
</tree>

在那里,树会是这样的:

                    100
              50                200

你说什么?您还有其他推荐吗?

I'm implementing some classes to handle the common data structures (Tree, BinaryTree, Search Binary Tree, BTree, AVL, etc). I'd like a way to store tree information in a data source and parse it to one of those classes. The simplest way is to use XML (portability is very important). The thing is that the XML documents that i make are not easy to read. They're not enough "intuitive". For example, i was doing something like this:

<?xml version="1.0" encoding="UTF-8"?>
<tree>
    <root>100</root>
    <node>
        <tree>
            <root>50</root>     
        </tree>
    </node>
    <node>
        <tree>
            <root>200</root>        
        </tree>
    </node>
</tree>

There, the tree would be something like this:

                    100
              50                200

What do you say? Do you have any other recommendation?

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

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

发布评论

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

评论(4

冷情妓 2024-10-14 09:52:15

我在你的描述中没有看到树的根、叶子和其他节点之间有任何重要的结构差异。因此,它们都可以具有相同的内容模型,如本示例所示:

<tree>
    <value>100</value>
    <tree>
        <value>50</value>
    </tree>
    <tree>
        <value>200</value>
    </tree>
</tree>

此结构稍微简洁一些,并且 XML 文档树与您尝试表示的树相匹配。如果您的值可以表示为字符串,您甚至可以这样写:

<tree>
    100
    <tree>50</tree>
    <tree>200</tree>
</tree>

I don't see in your description any important structural difference between the root, leaves of the tree, and other nodes. So they could all have the same content model, as in this example:

<tree>
    <value>100</value>
    <tree>
        <value>50</value>
    </tree>
    <tree>
        <value>200</value>
    </tree>
</tree>

This structure is a bit less verbose, and the XML document tree is matching the tree you are trying to represent. If your values can be expressed as strings, you could even write it like that:

<tree>
    100
    <tree>50</tree>
    <tree>200</tree>
</tree>
嗼ふ静 2024-10-14 09:52:15

在 XML 中不存在表示树的“最佳”方法。这完全取决于您想要优化存储的目的 - 您是否希望能够快速追踪特定节点?那么平面(呃)表示可能会更好。如果您想让描述保持简洁,那么您概述的方法将非常好(尽管我可能会将“根”子项转换为属性(根/值),这样 xml 中的每个节点实际上都是一个树节点)。

There is no 'best' way to represent a tree in XML. It all depends what you want to optimize your storage for - do you want to be able to track down a specific node quickly? Then a flat(er) representation might be better. If you want to keep the description non-verbose, then the method you outlined will be pretty good (though I would probably turn the 'root' child into an attribute (root/value), that way every node in the xml is actually a tree node).

小梨窩很甜 2024-10-14 09:52:15
<node value="100">
    <node value="50" />
    <node value="200" />
</node>

树中的元素是节点,而不是树。整个节点集合构成一棵树。

另外,XML 本身就是一棵树,因此您不必显式地对其建模。

<node value="100">
    <node value="50" />
    <node value="200" />
</node>

Elements in the tree are nodes, not trees. The whole node assembly constitues a tree.

Also, XML is a tree itself, so you don't have to explicitly model it.

孤云独去闲 2024-10-14 09:52:15

在我看来,数据结构对于计算机来说应该是容易的,而不是对于人类来说。如果您想要人类可读的数据版本,只需创建一个漂亮的打印访问者

In my opinion, data structures are meant to be easy on the computer, not a human. If you want a human readable version of the data, just create a pretty printing visitor.

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