C++ Program to Implement Threaded Binary Tree



Threaded binary tree is a binary tree that provides the facility to traverse the tree in a particular order. In this article, we will learn all about threaded binary trees, their types, and how to implement them in C++.

What is a Threaded Binary Tree?

A threaded binary tree is a type of binary tree in which NULL pointers are replaced with pointers to the in-order predecessor and successor nodes. This treading will help in faster traversal of the tree without using a stack or recursion. The image below shows a threaded binary tree.

Threaded Binary Tree

There are two types of threaded binary trees:

Single Threaded: In a single threaded binary tree, only one type of pointer is used. Either the left child pointer points to the in-order predecessor or the right child pointer points to the in-order successor. The other pointer will be NULL.

Double threaded: In a double threaded binary tree, two types of pointers are used. The left child pointer points to the in-order predecessor and the right child pointer points to the in-order successor.

Threaded Binary Tree Representation

The threaded binary tree is represented same as a normal binary tree, but we will add two boolean values to the structure of the node to indicate whether the left and right pointers are threaded or not. The structure of a threaded binary tree node is shown below:

struct Node {
    int data;              // Data of the node
    Node* left;            // Pointer to the left child
    Node* right;           // Pointer to the right child
    bool isThreadedLeft;   // Flag to indicate if left pointer is threaded
    bool isThreadedRight;  // Flag to indicate if right pointer is threaded
};

Steps to Create a Threaded Binary Tree

To create a threaded binary tree, we will follow these steps:

  • Create a normal binary tree.
  • Then, perform in-order traversal of the binary tree and keep track of the previously visited node.
  • For each node, if the left child is NULL, make it point to the previously visited node (in-order predecessor).
  • If the right child is NULL, make it point to the next node in the in-order traversal (in-order successor).

C++ Program to Implement a Threaded Binary Tree

The code below implements a threaded binary tree in C++. It creates a binary tree and converts it into a threaded binary tree by performing in-order traversal.

#include <iostream>
using namespace std;

class Node {
public:
    int data;
    Node *left, *right;
    bool isLeftThread, isRightThread;

    Node(int val) {
        data = val;
        left = right = nullptr;
        isLeftThread = isRightThread = true;
    }
};

class ThreadedBinaryTree {
private:
    Node *root;

public:
    ThreadedBinaryTree() {
        root = nullptr;
    }

    void insert(int key) {
        Node *ptr = root;
        Node *par = nullptr;

        while (ptr != nullptr) {
            if (key == ptr->data) {
                cout << "Duplicate Key!\n";
                return;
            }

            par = ptr;

            if (key < ptr->data) {
                if (!ptr->isLeftThread)
                    ptr = ptr->left;
                else
                    break;
            } else {
                if (!ptr->isRightThread)
                    ptr = ptr->right;
                else
                    break;
            }
        }

        Node *newNode = new Node(key);

        if (par == nullptr) {
            root = newNode;
        } else if (key < par->data) {
            newNode->left = par->left;
            newNode->right = par;
            par->isLeftThread = false;
            par->left = newNode;
        } else {
            newNode->left = par;
            newNode->right = par->right;
            par->isRightThread = false;
            par->right = newNode;
        }
    }

    void inorder() {
        Node *curr = root;
        if (curr == nullptr) return;

        // Go to the leftmost node
        while (!curr->isLeftThread)
            curr = curr->left;

        while (curr != nullptr) {
            cout << curr->data << " ";

            // If right pointer is a thread
            if (curr->isRightThread)
                curr = curr->right;
            else {
                // Go to leftmost in right subtree
                curr = curr->right;
                while (!curr->isLeftThread)
                    curr = curr->left;
            }
        }
    }
};

int main() {
    ThreadedBinaryTree tbt;

    tbt.insert(50);
    tbt.insert(30);
    tbt.insert(70);
    tbt.insert(20);
    tbt.insert(40);
    tbt.insert(60);
    tbt.insert(80);

    cout << "Inorder Traversal: ";
    tbt.inorder();  // Should print sorted values
    return 0;
}

The output of the above code will be:

Inorder Traversal: 20 30 40 50 60 70 80

Time and Space Complexity

Time Complexity: The time complexity of this algorithm is O(n), where n is the number of nodes in the tree. This is because each node is visited exactly once during the in-order traversal.

Space Complexity: The space complexity is O(n), where n is the number of nodes in the tree. This is due to the storage used for the threaded pointers.

Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-06-16T18:10:10+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started