我希望BinaryTree中的节点“预订”遍历访问它们 这个顺序[N0,N1,N2,N3] 我应该怎么做以下结构?
one sig Ordering { // model a linear order on nodes
first: Node, // the first node in the linear order
order: Node -> Node // for each node n, n.(Ordering.order) represents the
// node (if any) immediately after n in order
}
fact LinearOrder { // the first node in the linear order is N0; and
// the four nodes are ordered as [N0, N1, N2, N3]
}
pred SymmetryBreaking(t: BinaryTree) { // if t has a root node, it is the
//first node according to the linear order; and
// a "pre-order" traversal of the nodes in t visits them according
//to the linear order
}
答案 0 :(得分:0)
你的问题有2部分。首先,定义N0,N1的排序.......其次,使用线性排序定义对称性中断。
首先,您可以通过列出所有有效关系来定义排序,例如
`N0.(Ordering.order) = N1`
其次,定义树的预先顺序遵循线性排序的谓词。基本上,有两种情况,第一种是微不足道的,没有t.root。但是,当root不为NULL时,树必须具有以下3个属性。
如果您将整个描述翻译成合金,它将类似于
no t.root or
{
t.root = N0 // 3
all h : t.root.^(left+right) | one n : t.root.*(left+right) |
(n.left = h and n.(Ordering.order) = h ) or //1
( one l :( n.left*(left+right) + n)| n.right = h and l.(Ordering.order) = h)//2
}
注意:可能有许多替代解决方案,这个绝对不是一个简单的解决方案。