Is Pre-Order traversal on a binary tree same as Depth First Search?

前端 未结 4 1033
情书的邮戳
情书的邮戳 2020-12-24 02:18

It seems to me like Pre-order traversal and DFS are same as in both the cases we traverse till the leaf node in a depth wise fashion. Could anyone please correct me if I am

4条回答
  •  旧时难觅i
    2020-12-24 02:27

    It probably depends on the definition and implementation of the depth-first algorithm. The DefaultMutableTreeNode class of the Java Swing's JTree component has the following enumeration methods used for tree traversal:

    • depthFirstEnumeration()
    • postorderEnumeration()
    • preorderEnumeration()
    • breadthFirstEnumeration()

    In Java Swing's implementation the depthFirstEnumeration is the same as the postOrderEnumeration. My tests and the official documentation confirms this.

    Others can define what depth-first means differently. For instance, an article on Wikipedia states that pre-order and post-order traversals are specific types of a depth-first traversal. This would mean that the depth-first traversal is not a concrete traversal algorithm.

提交回复
热议问题