Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 886
Srikanth Kandalam Avatar asked Feb 05 '14 08:02

Srikanth Kandalam


People also ask

Is pre-order traversal same as depth first search?

Preorder Traversal is another variant of DFS. Where atomic operations in a recursive function, are as same as Inorder traversal but with a different order.

Which tree traversal is equivalent to depth first search?

Depth First Search is equivalent to which of the traversal in the Binary Trees? Explanation: In Depth First Search, we explore all the nodes aggressively to one path and then backtrack to the node. Hence, it is equivalent to the pre-order traversal of a Binary Tree.

Is PreOrder same as BFS?

BFS is like - first go for my siblings then their child then their child and so on. pre-order DFS technique is generally used in graph traversal . BFS is a level order traversal in the case of tree. These four are different techniques of traversal and results are also different.


2 Answers

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.

like image 173
herohuyongtao Avatar answered Sep 29 '22 21:09

herohuyongtao


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.

like image 35
lu5er Avatar answered Sep 29 '22 20:09

lu5er