Can anybody give a link for a simple explanation on BFS and DFS with its implementation?
First of all, BFS and DFS are two ways to implement binary tree traversal. Breadth First means level order traversal. Depth First has three ways to implemnt -- , , .
Preorder:
a. Start with root,
b. mark it visited,
c. go to left node
d. (b) - mark it visited
e. Repeat (c) till there is not any new left node remaining
(We are/might be at leaf node at this point,)
f. Is there any right node available? If yes, go to (a).
Level Order Traversal Time Complexity O(n)- Number of times each node is visited is 1 only, means total is n times.
Space Complexity- Best Case: Tree only left nodes, is O(1) Average Case: Perfect binary tree is example, n/2 number of nodes, O(n)