Tutorialspoint
Problem
Solution
Submissions

Binary Search Tree Operations

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C++ program to implement a binary search tree (BST) and perform basic operations such as insertion, deletion, searching, and traversals (in-order, pre-order, and post-order).

Example 1
  • Input:
    • Operations:
      • 1. Insert: 50, 30, 70, 20, 40, 60, 80
      • 2. In-order traversal
      • 3. Pre-order traversal
      • 4. Post-order traversal
      • 5. Search for 40
      • 6. Delete 30
      • 7. In-order traversal
  • Output:
    • In-order traversal: 20 30 40 50 60 70 80
    • Pre-order traversal: 50 30 20 40 70 60 80
    • Post-order traversal: 20 40 30 60 80 70 50
    • Element 40 found in the BST
    • In-order traversal after deletion: 20 40 50 60 70 80
  • Explanation:
    • Step 1: Create BST by inserting values
    • Step 2: In-order traversal visits nodes in ascending order
    • Step 3: Pre-order traversal visits root first, then left & right subtrees
    • Step 4: Post-order traversal visits left & right subtrees, then root
    • Step 5: Search finds element 40
    • Step 6: Delete node 30 (has two children)
Example 2
  • Input:
    • Operations:
      • 1. Insert: 45, 25, 75, 15, 35, 65, 85
      • 2. In-order traversal
      • 3. Search for 100
      • 4. Delete 75
      • 5. In-order traversal
  • Output:
    • In-order traversal: 15 25 35 45 65 75 85
    • Element 100 not found in the BST
    • In-order traversal after deletion: 15 25 35 45 65 85
  • Explanation:
    • Step 1: Create BST by inserting values
    • Step 2: In-order traversal visits nodes in ascending order
    • Step 3: Search fails to find element 100
    • Step 4: Delete node 75 (has one child)
    • Step 5: Updated in-order traversal shows 75 is removed
Constraints
  • 1 ≤ Number of operations ≤ 10^4
  • -10^9 ≤ Element values ≤ 10^9
  • Time Complexity: O(h) for insertion, deletion, and search operations, where h is the height of the tree
  • Space Complexity: O(n) where n is the number of elements in the tree
Functions / MethodsRecursionBinary TreeGoogleIBM
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Define a Node structure with 'data', 'left' child pointer, and 'right' child pointer.
  • For insertion, compare the value with the current node and move to the left or right subtree accordingly.
  • For deletion, handle three cases: node with no children, node with one child, and node with two children.
  • For in-order traversal, recursively visit left subtree, then the node, then right subtree.
  • For pre-order traversal, recursively visit the node, then left subtree, then right subtree.
  • For post-order traversal, recursively visit left subtree, then right subtree, then the node.
  • For searching, compare the value with the current node and move to the left or right subtree accordingly.

Steps to solve by this approach:

 Step 1: Create a Node class with data and left/right pointers.

 Step 2: Implement a BinarySearchTree class with a root pointer.
 Step 3: Create recursive helper functions for tree operations.
 Step 4: Implement insert operation to maintain BST property.
 Step 5: Implement delete operation handling all cases (0, 1, or 2 children).
 Step 6: Implement traversal methods (in-order, pre-order, post-order).
 Step 7: Implement search operation to find values in the tree.

Submitted Code :