Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print out a BST in C++

My C++ program creates a binary search tree. I know how to print out the values in pre-order, post-order, and in-order.

However, I want to do something a little more difficult. I want to print out the values the way they would look if someone drew the tree on paper. It would have the root at the center at the top, it's left child right under and to the left of it, and it's right child right under and to the right of it. The rest of the nodes would be drawn accordingly.

How can I do that?

like image 292
neuromancer Avatar asked Nov 30 '22 11:11

neuromancer


1 Answers

This article contains code for what you need, it seems:

alt text http://www.cpp-programming.net/wp-content/uploads/2007/12/ascii_tree.jpg

Edit: that site went offline

Here's another one exploring some other options.

like image 118
Eli Bendersky Avatar answered Dec 05 '22 04:12

Eli Bendersky