Is Pre-Order traversal on a binary tree same as De

2019-02-04 05:32发布

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 wrong?

Thanks in advance!

3条回答
三岁会撩人
2楼-- · 2019-02-04 06:07

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.

查看更多
家丑人穷心不美
3楼-- · 2019-02-04 06:24

Pre-order is one type of DFS.

There are three types of depth-first traversal: pre-order, in-order, and post-order.

Check out here for more info.

查看更多
走好不送
4楼-- · 2019-02-04 06:25

It won't be. Pre-order has a strict fashion of visiting the Left node and then the Right node. But for DFS it can be either as there is no strict fashion. So, more than one traversal exists based on what you push on the stack.

查看更多
登录 后发表回答