All Exams Test series for 1 year @ ₹349 only
Question

Which traversal of binary search tree is in ascending order?

I. Pre Order Traversal

II. Post Order Traversal

III. In Order Traversal

The correct answer is
Only III

Understanding Binary Search Tree Traversals for Ascending Order

A Binary Search Tree (BST) is a tree data structure where for each node:

  • All values in the left subtree are less than the node's value.
  • All values in the right subtree are greater than the node's value.
  • Both the left and right subtrees are also BSTs.

Tree traversals are methods for visiting all the nodes in a tree exactly once. The order in which nodes are visited determines the output sequence. Let's examine the common traversals mentioned:

Exploring Different Binary Search Tree Traversals

  • Pre Order Traversal: The order of visiting nodes is Root, then Left subtree, then Right subtree (Node → Left → Right).
  • Post Order Traversal: The order is Left subtree, then Right subtree, then Root (Left → Right → Node).
  • In Order Traversal: The order is Left subtree, then Root, then Right subtree (Left → Node → Right).

Which Traversal Yields Ascending Order in a BST?

Consider the properties of a BST. The left subtree contains smaller values, the node itself has a value between its left and right children (if they exist), and the right subtree contains larger values.

Let's analyze how each traversal interacts with this property:

  • Pre Order: The root is visited first. This doesn't guarantee ascending order because the root's value might be anywhere within the range of values in the tree. For example, in a simple tree with root 10, left child 5, right child 15, Pre Order is 10, 5, 15 — not ascending.
  • Post Order: The root is visited last. The left and right subtrees are visited first. Again, this doesn't naturally produce ascending order because the larger values in the right subtree might be visited before the smaller values in the root node. For the example tree (10, 5, 15), Post Order might be 5, 15, 10 — not ascending.
  • In Order: The left subtree is visited first (all smaller values), then the root node, then the right subtree (all larger values). Due to the BST property, visiting the left subtree nodes, then the root, then the right subtree nodes naturally sorts the elements in ascending order. For the example tree (10, 5, 15), In Order is 5, 10, 15 — ascending.

Example Binary Search Tree Traversal Results

Consider a BST with nodes 10, 5, 15, 3, 7, 12, 18.

Traversal Type Order Visited
Pre Order 10, 5, 3, 7, 15, 12, 18
In Order 3, 5, 7, 10, 12, 15, 18
Post Order 3, 7, 5, 12, 18, 15, 10
Traversal Orders for a Sample BST

As seen in the table, the In Order Traversal produces the nodes in ascending numerical order.

Conclusion on BST Traversal Order

Based on the structure and traversal methods of a Binary Search Tree, only the In Order traversal guarantees visiting the nodes in ascending order.

Revision Table: BST Traversal Summary

Traversal Name Visit Order Output for BST
Pre Order Node, Left, Right Not necessarily sorted
In Order Left, Node, Right Ascending order (Sorted)
Post Order Left, Right, Node Not necessarily sorted
Summary of BST Traversal Properties

Additional Information: Other BST Operations

Besides traversal, common operations on a Binary Search Tree include:

  • Search: Efficiently finding if a specific value exists in the tree by comparing with node values and moving to the left or right subtree. Time complexity is typically $\mathcal{O}(h)$, where $h$ is the height of the tree.
  • Insertion: Adding a new node while maintaining the BST property. Time complexity is typically $\mathcal{O}(h)$.
  • Deletion: Removing a node while maintaining the BST property. This can be more complex, especially for nodes with two children. Time complexity is typically $\mathcal{O}(h)$.
  • Finding Minimum/Maximum: The minimum value is the leftmost node, and the maximum value is the rightmost node. Time complexity is $\mathcal{O}(h)$.

The efficiency of BST operations largely depends on the tree's height. In the best case (a balanced tree), $h \approx \log n$ where $n$ is the number of nodes. In the worst case (a skewed tree), $h = n$.

Was this answer helpful?

Important Questions from Tree Traversal - Teaching

  1. Consider the traversal of a tree

    Preorder → ABCEIFJDGHKL

    Inorder → EICFJBGDKHLA

    Which of the following is correct post order traversal?

  2. Postorder traversal of a given binary search tree T produces following sequence of keys: 3, 5, 7, 9, 4, 17, 16, 20, 18, 15, 14 Which one of the following sequences of keys can be the result of an in-order traversal of the tree T?

  3. The inorder traversal of the following tree is :

  4. Level order Traversal of a rooted Tree can be done by starting from root and performing :

  5. The inorder and preorder Traversal of binary Tree are dbeafcg and abdecfg respectively. The post-order Traversal is __________.

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App