二叉树预订访问伪代码

时间:2017-02-05 17:10:02

标签: algorithm

已在网上搜索超过5个小时,无法找到一般BT预购访问伪代码。 提前致谢。 我只是找到像这样的短伪代码

 Algorithm postorder(T, v)
 Input: A binary tree T and a node v of T.
 Output: Depends on the action performed on a visit to a node.
  if T.hasLeft(v)
  postorder(T, T.left(v))   // recursively traverse left subtree
 if T.hasRight(v)
   postorder(T, T.right(v))  // recursively traverse right subtree
visit node v

1 个答案:

答案 0 :(得分:1)

预订,依次,后期订单之间的区别仅仅是订单,其中相对于子项访问节点:< / p>

你发布了这个:

 Algorithm postorder(T, v)
 Input: A binary tree T and a node v of T.
 Output: Depends on the action performed on a visit to a node.

 if T.hasLeft(v)
   postorder(T, T.left(v))   // recursively traverse left subtree
 if T.hasRight(v)
   postorder(T, T.right(v))  // recursively traverse right subtree
 visit node v

要更改行为,请更改执行顺序。这是一些通用代码:

<强> AnyOrder:

AnyOrder(T, v, order)

    if order is 'pre'
        visit(v)

    AnyOrder(T, T.left(v), order)

    if order is 'in'
        visit(v)

    AnyOrder(T, T.right(v), order)

    if order is 'post'
        visit(v)