I have a difficulty in understanding the differences between dfs, bfs, and in-order, pre-order and post-order in tree traversal (binary tree).
My understanding that
dfs is neither pre-order, in-order or post-order
Bfs is also neither pre-order, in-order or post-order
But I see pre-order, in-order or post-order versions in dfs in wikipedia, anybody knows why?
https://en.wikipedia.org/wiki/Tree_traversal
It depended on what order you visit the nodes in your recursive function.
https://en.wikipedia.org/wiki/Tree_traversal#Depth-first_search.28DFS.29
If you visit the left child tree, then current node, then right child tree, the iterator will come put in order because the first node to be read will be the far right leaf, then it's parent, then it's parent's right leaf, etc.
Other arrangements of these operations will result in other orderings.
Related
Binary Tree traversal such as preorder traversal, inorder traversal, postorder traversal, and level order traversal is commonly interviewed by many IT companies.
It confuses me to remember the iterative implementation of pre-order traversal and inorder traversal.
Here are the problems from leetcode.
https://leetcode.com/problems/binary-tree-inorder-traversal/
https://leetcode.com/problems/binary-tree-preorder-traversal/
I remember it thinking about it with reference to the root node.
Inorder -> meaning the root is InBetween Left and Right,
PreOrder -> The Root is 'pre'/before Left and right
Post -> Root is after Left and Right.
preorder traversal:
We walk the graph, from top going counter-clockwise. Shout every time we pass the LEFT of a node.
inorder traversal:
We walk the graph, from top going counter-clockwise. Shout every time Shout when you cross the bottom.
post-order traversal
We walk the graph, from top going counter-clockwise. Shout every time Shout when you cross the right
If you want to see more recursive and iterative implementation details, please read the following post
For a Binary SEARCH Tree, a preorder or a postorder traversal is sufficient to reconstruct its original binary search tree unambigiously.
But I cannot find a proof or a explanation for why it is so.
For inorder traversal, it is trivial to come up with a counter-example to show that there may be many different BSTs correspond to a given inorder traversal.
Is there any proof or reference that a preorder or a postorder traversal is sufficient to reconstruct its original BST unambiguously?
This is for a BST, not for a general binary tree.
By the definition of pre-order traversal, it is guaranteed that root will be the first element.
Now, given the root, there is a unique set of elements that will come in the left half of the root - i.e. all elements having value less that root->val.
Likewise for the right subtree.
Now, we have to apply the above logic recursively for the left subtree and the right subtree.
Since, post-order traversal is just the reverse of pre-order traversal on the reverse tree, the same logic applies to post-order traversal too.
Given:
Pre-Order Traversal.
Post-Order Traversal.
Level-Order Traversal.
One can not contruct a Binary Tree with 12 or 23 or 31 or even if 123 are given! Why is this? and Why InOrder Traversal is very important to construct the original Tree?
We can't build a tree without the in-order traversal. Why? Let's say you are given the pre-order and post-order traversals only.A simple example is shown below.
Consider two different trees,
TREE 1:
root=a;
root->left=b;
root->left->right=c;
Tree 2:
root=a;
root->right=b;
root->right->left=c;
Both the trees are different, but have same pre-order and post-order sequence.
pre-order - a b c
post-order - c b a
This is so because we cannot separate the left sub-tree and right sub-tree using the pre-order or post-order traversal alone.
Pre-order, as its name, always visits root first and then left and right sub-trees. That is to say, walking through a pre-order list, each node we hit would be a "root" of a sub-tree.
Post-order, as its name, always visits left and right sub-trees first and then the root. That is to say, walking through a post-order list backward, each node we hit would be a "root" of a sub-tree.
In-order, on the other hand, always visits left sub-tree first and then root and then right sub-tree, which means that given a root(which we can obtain from the pre-order or post-order traversal as stated above), in-order traversal tells us the sizes of the left and right sub-trees of a given root and thus we can construct the original tree.(Think this out)
Same is the case with level-order traversal. Thus if we want to obtain a unique tree we need an in-order traversal along with any other of the three traversals.
Note - The exception is of course a full binary tree, in which pre-order and post-order traversals can be used to construct the tree, as there is no ambiguity in tree structure.
I have Binary Search tree and have to perform three types of tree traversal:
Are this results correct?
Pre-order (root,left,right): 30,15,59,43,40,92
In-order (left,root,right): 15,30,59,40,43,92
Post-order (left,right,root): 15,59,40,43,92,30
UPDATE:
In-order: 15,30,40,43,59,92 (projection?)
Post-order: 15,40,43,92,59,30.
Is it right?
Given this updated tree, your preorder traversal is correct.
Your inorder traversal, though, is incorrect. As a hint, doing an inorder traversal of a binary tree always lists the values off in sorted order.
Finally, your postorder traversal is incorrect. The value 59 won't be produced until after all of the nodes in its two subtrees are produced, so it should come second-to-last. Using this fact, try seeing if you can come up with the correct answer.
Hope this helps!
I know inorder traversal works like this:
Traverse the left subtree.
Visit the root.
Traverse the right subtree.
But what if we have an algorithm that does the following
Traverse the right subtree.
Visit the root.
Traverse the left subtree.
Would such a tree traversal be consdered inorder as well?
I was wondering the same thing for some time.
I would say, it can be called inorder traversal also. The result would be reverse sorted array instead of the sorting that would come up with left-root-right.
But the definitions are strict on saying left-root-right.
It will still be an inorder traversal. Inorder is simetrical.
From wikipedia:
To traverse a non-empty binary tree in
inorder (symmetric), perform the
following operations recursively at
each node:
Traverse the left subtree.
Visit the root.
Traverse the right subtree.
Reference.
Yes, assuming it's a binary search tree, it's still an inorder traversal. The order would be the reverse of that obtained from visiting the left subtree first, instead of the right.