Dictionary Operations in a Binary Search Tree Using C++



A dictionary is a collection of key-value pairs where, keys are unique and used to identify corresponding values in the dictionary. In this article, we will learn how to perform dictionary operations such as insertion, search, and traversal using a binary search tree (BST) in C++.

What is BST?

A binary search tree (BST) is a tree data structure, where each node has at most two children and follows two rules: the left subtree contains keys less than the node's key, and the right subtree contains keys greater than the node's key. This structure is same as how a binary search works. A BST is known for it's efficient searching, insertion, and deletion operations. In a dictionary using BST, each node of the BST will store a key-value pair and pointers for traversing left and right.

For example, a node in a dictionary BST will look like this:

struct Node {
    string key;
    string value;
    Node* left;
    Node* right;
};

Implement Dictionary using BST in C++

To implement a dictionary using a binary search tree, we define a node structure containing key-value pairs and pointers to left and right children. Below are some key points:

  • Structure: Each node contains a key (as string type), a value (as string type), and two pointers for left and right child nodes.
  • Operations: Insert key-value pairs, search for a key, and perform inorder traversal to list entries in sorted order.

Steps to Implement Dictionary using BST in C++

Following are steps/algorithm to implement dictionary operations in BST:

  • Create a structure or class for dictionary nodes.
  • Each node should store a key, a value, and pointers to left and right children.
  • Write a function to insert a new key-value pair into the BST.
  • Write a function to search for a key and return its value.
  • Write a function to perform inorder traversal to print dictionary in sorted order of keys.

C++ Program to Perform Dictionary Operations in BST

In the below code, we have implemented dictionary using binary search tree and supported insertion, search, and traversal operations.

#include <iostream>
using namespace std;

// Define a node of the dictionary BST
struct Node {
    string key;
    string value;
    Node* left;
    Node* right;

    Node(string k, string v) {
        key = k;
        value = v;
        left = right = nullptr;
    }
};

// Insert a new key-value pair into BST
Node* insert(Node* root, string key, string value) {
    if (root == nullptr)
        return new Node(key, value);
    if (key < root->key)
        root->left = insert(root->left, key, value);
    else if (key > root->key)
        root->right = insert(root->right, key, value);
    else
        root->value = value; // Update value if key already exists
    return root;
}

// Search for a key in BST
string search(Node* root, string key) {
    if (root == nullptr)
        return "Key not found";
    if (key == root->key)
        return root->value;
    else if (key < root->key)
        return search(root->left, key);
    else
        return search(root->right, key);
}

// Inorder traversal to print dictionary in sorted order
void inorder(Node* root) {
    if (root == nullptr)
        return;
    inorder(root->left);
    cout << root->key << ": " << root->value << endl;
    inorder(root->right);
}

int main() {
    Node* root = nullptr;

    // Insert dictionary entries
    root = insert(root, "apple", "A fruit");
    root = insert(root, "dog", "A domestic animal");
    root = insert(root, "book", "A set of printed pages");
    root = insert(root, "cat", "A small domesticated carnivorous mammal");

    cout << "Inorder Traversal (Dictionary in sorted order):" << endl;
    inorder(root);

    cout << endl << "Search for 'cat': " << search(root, "cat") << endl;
    cout << "Search for 'zebra': " << search(root, "zebra") << endl;

    return 0;
}

The output of above code will be:

Inorder Traversal (Dictionary in sorted order):
apple: A fruit
book: A set of printed pages
cat: A small domesticated carnivorous mammal
dog: A domestic animal

Search for 'cat': A small domesticated carnivorous mammal
Search for 'zebra': Key not found

Time and Space Complexity

Time Complexity: O(h), where h is the height of the BST for both insertion and search operations.

Space Complexity: O(h), due to recursion stack during insertion, search, or traversal.

Updated on: 2025-05-15T19:35:26+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements