//This is what we are asked to print in the assignment of Binary Search Trees.The Nodes are printed like:

 F
     +-- D
     |    +-- NULL
     |    \-- NULL
     \-- R
          +-- P
          |    +-- M
          |    |    +-- NULL
          |    |    \-- N
          |    |         +-- NULL
          |    |         \-- NULL
          |    \-- NULL
          \-- V
               +-- NULL
               \-- NULL


/*But I am not  able to print "|".
Please help.
I have printed the bst like:*/

 F
     +-- D
            +-- NULL
             \-- NULL
     \-- R
          +-- P
             +-- M
               +-- NULL
               \-- N
                       +-- NULL
                    \ -- NULL
             \-- NULL
          \-- V
               +-- NULL
               \-- NULL

Dani AI

Generated

The problem is not that the character | cannot be printed (as demonstrated), but that the program must preserve where those pipes belong across later lines. A simple and robust pattern is to carry a prefix string down the recursion: when recursing to a left child append a vertical-slot (for example |) so descendants print a | in that column, and when recursing to a right child append the same number of spaces so the column stays blank.

A compact implementation idea (assumes a Node with left, right, and val) follows:

void printTree(Node* n, const std::string& prefix = "", bool isLeft = false) {
    if (!n) {
        std::cout << prefix << (isLeft ? "+-- " : "\\-- ") << "NULL\n";
        return;
    }
    if (prefix.empty())
        std::cout << n->val << '\n';
    else
        std::cout << prefix << (isLeft ? "+-- " : "\\-- ") << n->val << '\n';

    std::string childPrefix = prefix + (isLeft ? "|   " : "    ");
    printTree(n->left,  childPrefix, true);
    printTree(n->right, childPrefix, false);
}

Notes and troubleshooting: the right-branch marker uses a backslash, so in a C++ string literal write "\\-- " (backslash escaped). Use a monospaced font and consistent spacing (avoid tabs) so vertical bars line up. Also print NULL placeholders for empty children when you want the vertical pipes preserved for deeper siblings — that is what keeps the | columns visible on later lines. This approach should reproduce the formatting expected by the assignment.

Recommended Answers

All 3 Replies

Why not? Just print '|'.

#include <iostream>
using namespace std;

int main()
{
    cout << "||||||||||" << endl;
    
    return 0;
}

Why not? Just print '|'.

#include <iostream>
using namespace std;

int main()
{
    cout << "||||||||||" << endl;
    
    return 0;
}

I have to print the "|" between the left node and right node of a binary search tree.

/*This is the assignment statement.
For BST insertion, if the input contains the following

    F
    D
    R
    P
    V
    M
    N
The resulting BST should be displayed as:
    F
     +-- D
     |    +-- NULL
     |    \-- NULL
     \-- R
          +-- P
          |    +-- M
          |    |    +-- NULL
          |    |    \-- N
          |    |         +-- NULL
          |    |         \-- NULL
          |    \-- NULL
          \-- V
               +-- NULL
               \-- NULL
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.