合金:按所需顺序对二叉树进行预先遍历

时间:2016-09-15 02:38:59

标签: data-structures binary-tree alloy

我希望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
}

1 个答案:

答案 0 :(得分:0)

你的问题有2部分。首先,定义N0,N1的排序.......其次,使用线性排序定义对称性中断。

首先,您可以通过列出所有有效关系来定义排序,例如

`N0.(Ordering.order) = N1`

其次,定义树的预先顺序遵循线性排序的谓词。基本上,有两种情况,第一种是微不足道的,没有t.root。但是,当root不为NULL时,树必须具有以下3个属性。

  1. 对于所有节点n,如果h = n.left,则h.val = n.val。(Ordering.order)
  2. 对于所有节点n,如果h = n.right,则h.val =(n-左子树中的一个节点或n)。(Ordering.order)
  3. t.root = N0
  4. 如果您将整个描述翻译成合金,它将类似于

    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
    }

    注意:可能有许多替代解决方案,这个绝对不是一个简单的解决方案。