0

First of all I'd like to apologize but I could not come up with a better title.

I have implemented a BST in C++ with 3 properties: left, right and data.

Suppose after some insertions it look like this.

How could I print the BST in the console like this?

The number of X's used should be the minimum possible. I've been looking at this question for quite some time and I'm not able to come up with anything. Any help is appreciated.

  • I suspect any number of questions about the same topic (top-down printing a binary tree) on this site would prove more helpful than launching another one. [For example, this one](https://stackoverflow.com/questions/36802354/print-binary-tree-in-a-pretty-way-using-c). – WhozCraig Jul 11 '21 at 08:27
  • 2
    Do it with a pen and paper, then write down the steps you took, find common patterns (loops) and rewrite it in C++. Step 1: need to find the width of the output somehow, right? How would you do that by hand? – rustyx Jul 11 '21 at 08:55
  • 1
    make a recursive function that takes a current node and current level, and returns ```vector>```. process nodes with *in-order traversal*. each node returns a vector of pair of a value and a level for its left, itself, and it right representing one digit. append the three vectors and return them as a result. pass the root and level = 0 for the first call. process the vector answer (let it be v1) and find max level, you can make a ```vector> v2``` of(max*v1.size()) and initialize it with 'X'. for each element in v1, change the corresponding v2 element & print all. – Mohamed Akram Jul 11 '21 at 08:56

0 Answers0