I have a question about the traversal of a tree.
When we print the values of a binary search tree using in order traversal are the values printed in an ascending order??
I have a question about the traversal of a tree.
When we print the values of a binary search tree using in order traversal are the values printed in an ascending order??
Copyright © 2021 JogjaFile Inc.
That depends on the tree, the order and what actually in-order means in your case. In most situations: yes, the sequence produced would be ascending.
Usually BSTs are sorted that left child is smaller than the parent and right child is bigger than the parent. And similarly in-order traversal goes to the left child first, then parent and then right child. In such case, the values will be in ascending order.
On the other hand, it's easy to imagine BST sorted with $\geq$ rather than $\leq$ (there's no difference for the structure), and then in-order would produce descending sequence.
Of course, you could try visiting the nodes right-parent-left to reverse the order of the tree (i.e. produce descending when BST was sorted with $\leq$ and ascending when BST was sorted using $\geq$).
I hope this helps $\ddot\smile$