How to graphically print a binary search tree

Binary Search Trees (BSTs) are fundamental and efficient data structures for organizing and managing data. A key aspect of working with data structures is the ability to visualize and understand their inner workings. In this Answer, we will create a function in C++ that allows us to visually represent a BST in a tree-like structure on the console.

Conception of the algorithm

We will create a function called printBSTVisual, that will recursively print the nodes of a BST along with their connections. The primary goal of the algorithm is to create a clear and visually appealing representation of the BST in the console. Here are the things we must keep in mind when designing our algorithm:

  • Printing the nodes: Our function will start at the root node of the BST and print the node’s data along with the appropriate formatting. We will use ├── for the left children and └── for the right children, making it easy to distinguish between them.

  • Recursion: After printing the current node, we will recursively call the function for the left and right subtrees if they exist. During these recursive calls, a prefix string will be adjusted to maintain the tree’s structure. The left children will get added to their prefix, while the right children will get four spaces.

  • Termination: The recursion will stop when it reaches nodes with no children (leaf nodes).

Writing the algorithm

Based on the outline discussed above, here is the complete function:

void printBSTVisual(TreeNode* root, const std::string& prefix = "", bool isLeft = false) {
if (root) {
std::cout << prefix;
std::cout << (isLeft ? "├── " : "└── ");
std::cout << root->val << std::endl;
//Print the left subtree
printBSTVisual(root->left, prefix + (isLeft ? "│ " : " "), true);
//Print the right subtree
printBSTVisual(root->right, prefix + (isLeft ? "│ " : " "), false);
}
}

The printBSTVisual function takes the following parameters:

  • root: The root node of the BST
  • prefix: A string which tells which symbol to output with the node
  • isLeft: A boolean that tells the function if the node passed is a left child

Code explanation

  • Lines 2: We check if the root is a nullptr, which would indicate an empty subtree. If it’s empty, the function returns, avoiding any further processing.

  • Lines 3–5: We print the current node’s data along with appropriate formatting based on whether it’s a left or right child of its parent.

  • Lines 7–10: We recursively call the function for the left and right subtrees. It adjusts the prefix string to maintain the tree structure. The isLeft parameter helps determine the proper formatting for each child.

Testing the algorithm

Here is a working example of the algorithm we have created. Feel free to change the values in the tree to see how the output changes.

#include <iostream>
#include <queue>
// Definition for a binary tree node
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
TreeNode* insert(TreeNode* root, int value) {
if (!root) {
return new TreeNode(value);
}
if (value < root->val) {
root->left = insert(root->left, value);
} else if (value > root->val) {
root->right = insert(root->right, value);
}
return root;
}
// Function to print the BST nodes with appropriate formatting
void printBSTVisual(TreeNode* root, const std::string& prefix = "", bool isLeft = false) {
if (root) {
std::cout << prefix;
std::cout << (isLeft ? "├── " : "└── ");
std::cout << root->val << std::endl;
//Print the left subtree
printBSTVisual(root->left, prefix + (isLeft ? "│ " : " "), true);
//Print the right subtree
printBSTVisual(root->right, prefix + (isLeft ? "│ " : " "), false);
}
}
int main() {
// Example usage with input: 7 4 9 6 5 3
TreeNode* root = nullptr;
int values[] = {7, 4, 9, 6, 5, 3};
for (int value : values) {
root = insert(root, value);
}
std::cout<<"Visual representation of BST"<<std::endl;
printBSTVisual(root);
// Don't forget to free the allocated memory
delete root->left->left;
delete root->left->right;
delete root->right->right;
delete root->left;
delete root->right;
delete root;
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved