在 Mathematica 中,您所做的大部分工作都是基于表达式。表达式自然具有树结构。对于深度优先遍历(这可能是最常见的),您可以使用Scan、Map、Cases 等函数。与更传统的语言不同的是,没有简单的方法来保存身份表达式树中的单个节点,因为 Mathematica 中没有指针。此外,当您只需要在几个地方修改表达式时,许多对 Mathematica 惯用表达式的操作会复制整个表达式,因为表达式是不可变的。
使用不可变的 Mathematica 表达式作为树仍然有几个优点。一是,因为它们是不可变的,所以只需查看它们就很容易理解它们存储的内容(状态和行为没有混合)。另一个是有高效和通用的函数,例如Map、MapIndexed 或Scan,可以遍历它们。例如,访问者设计模式是invisible - 它只是Map[f,tree,Infinity],内置在语言中。此外,还有一些内置函数,如Cases、Replace、ReplaceAll 等,可以编写非常简洁和声明性的代码来解构树,找到具有特定语法或满足某些条件的树片段,等等。由于树不仅限于从列表构建并且从不同的头构建,因此可以有效地使用它来编写非常简洁的树处理代码。最后,根据exploratory and bottom-up programming 的精神,可以非常轻松地构建任何您想要的树结构,从而更轻松地进行实验和原型制作,从而缩短开发周期并最终带来更好的设计。
也就是说,您当然可以实现“有状态”(可变)树数据结构。我怀疑它尚未完成的真正原因通常是与构建、修改和遍历这样的树相关的性能损失,因为它将在每一步都经过完整的符号评估过程(有关更多详细信息,请参阅this 帖子在那)。有关如何在 Mathematica 上下文中使用二叉搜索树以获得相当高效的代码的 2 个示例,请参阅我的帖子 here(通用符号设置)和 here(在编译代码的上下文中)。对于在 Mathematica 中惯用地构造数据结构的一般方法,我推荐 Roman Maeder 的书籍:“Programming in Mathematica”、“Mathematica 程序员 I&II”,尤其是“Computer Science in Mathematica”。在后者中,他详细讨论了如何在 Mathematica 中实现二叉搜索树。 编辑正如@Simon 提到的,@Daniel Lichtblau 的谈话也是一个很好的资源,它展示了如何构建数据结构并使其高效。
关于在 Mathematica 中实现包含某些状态的数据结构的一般方法,这是从我在 this Mathgroup 线程中的帖子中提取的一个简单示例 - 它实现了“对”数据结构。
Unprotect[pair, setFirst, getFirst, setSecond, getSecond, new, delete];
ClearAll[pair, setFirst, getFirst, setSecond, getSecond, new, delete];
Module[{first, second},
first[_] := {};
second[_] := {};
pair /: new[pair[]] := pair[Unique[]];
pair /: pair[tag_].delete[] := (first[tag] =.; second[tag] =.);
pair /: pair[tag_].setFirst[value_] := first[tag] = value;
pair /: pair[tag_].getFirst[] := first[tag];
pair /: pair[tag_].setSecond[value_] := second[tag] = value;
pair /: pair[tag_].getSecond[] := second[tag];
Format[pair[x_Symbol]] := "pair[" <> ToString[Hash[x]] <> "]";
];
Protect[pair, setFirst, getFirst, setSecond, getSecond, new, delete];
你可以这样使用它:
pr = new[pair[]];
pr.setFirst[10];
pr.setSecond[20];
{pr.getFirst[], pr.getSecond[]}
{10, 20}
创建新配对对象列表:
pairs = Table[new[pair[]], {10}]
{"pair[430427975]", "pair[430428059]", "pair[430428060]", "pair[430428057]",
"pair[430428058]", "pair[430428063]", "pair[430428064]", "pair[430428061]",
"pair[430428062]", "pair[430428051]"}
设置字段:
Module[{i},
For[i = 1, i <= 10, i++,
pairs[[i]].setFirst[10*i];
pairs[[i]].setSecond[20*i];]]
检查字段:
#.getFirst[] & /@ pairs
{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
#.getSecond[] & /@ pairs
{20, 40, 60, 80, 100, 120, 140, 160, 180, 200}
在我提到的帖子中有更详细的讨论。以这种方式创建的“对象”的一个大问题是它们没有自动垃圾收集,这可能是顶级 Mathematica 本身实现的 OOP 扩展没有真正起飞的主要原因之一。
Mathematica 有几个 OOP 扩展,例如 Roman Maeder 的 classes.m 包(源代码在他的“Mathematica Programmer”一书中)、Objectica 商业包和其他几个。但是,除非 Mathematica 本身为构建可变数据结构(如果发生这种情况)提供有效的机制(可能基于某种指针或引用机制),否则这些数据结构的顶级实现可能会对性能造成很大影响在mma。此外,由于 mma 的核心思想之一是不可变性,因此要使可变数据结构与 Mathematica 编程的其他习语很好地契合并不容易。
编辑
这是一个基本的有状态树实现,类似于上面的示例:
Module[{parent, children, value},
children[_] := {};
value[_] := Null;
node /: new[node[]] := node[Unique[]];
node /: node[tag_].getChildren[] := children[tag];
node /: node[tag_].addChild[child_node, index_] :=
children[tag] = Insert[children[tag], child, index];
node /: node[tag_].removeChild[index_] :=
children[tag] = Delete[children[tag], index];
node /: node[tag_].getChild[index_] := children[tag][[index]];
node /: node[tag_].getValue[] := value[tag];
node /: node[tag_].setValue[val_] := value[tag] = val;
];
一些使用示例:
In[68]:= root = new[node[]]
Out[68]= node[$7]
In[69]:= root.addChild[new[node[]], 1]
Out[69]= {node[$8]}
In[70]:= root.addChild[new[node[]], 2]
Out[70]= {node[$8], node[$9]}
In[71]:= root.getChild[1].addChild[new[node[]], 1]
Out[71]= {node[$10]}
In[72]:= root.getChild[1].getChild[1].setValue[10]
Out[72]= 10
In[73]:= root.getChild[1].getChild[1].getValue[]
Out[73]= 10
有关使用这种可变树数据结构的一个重要示例,请参阅我的this 帖子。它还将这种方法与重用 Mathematica 原生数据结构和函数的方法进行了对比,并很好地说明了本文开头讨论的要点。