It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

I'm trying to create a function that prints the binary tree out to the screen making each column start 4 spaces to the right of the precious column. Here is what I have so far. I seem to have something out of order though. Any thoughts ?

```
template <class ItemType>
void RTreeType<ItemType>::ScreenPrint (std::ostream& outFile, NodeType* tree, int spaces) const
{
if ( tree != NULL )
{
ScreenPrint(outFile,tree->right,spaces + 4);
for ( int x=1; x <= spaces; ++x)
cout << setw(4) << " ";
cout << tree->info << endl;
ScreenPrint(outFile,tree->left, spaces + 4);
}
}
```

1

2 3

4 5 6 7

If so, that's quite hard to do with a recursive function.

Then print one level of the tree at a time. The spaces at the beginning and between the values can be calculated based on the current level and the total depth of the tree.

999

500

123

2

So something is not working properly

By the way, your output can be fine if the root node contains 2, the right subtree has 999 in the top node, the left subtree is empty, etc.

```
cout << std::string(spaces, ' ') << tree->info << endl;
```

You could fill arrays instead of printing. And print the arrays with an outer function

template <class ItemType>

void RTreeType<ItemType>::Scree

{

std::vector<std::vector<It

// now call the below recursive function with root node and level 0

...

// here you need to iterate the arrays level by level and print

}

template <class ItemType>

void RTreeType<ItemType>::getVa

{

if ( tree != NULL )

{

if ((int)out.size() <= level)

out.push_back(std::vector<

out[level].push_back(tree-

ScreenPrint(out, tree->left,level+1);

ScreenPrint(outFile,tree->

}

}

That should bring you further but you still have to handle the spaces somehow. Maybe, by using a string instead of the inner vector<ItemType>. Then you have to insert spaces when an empty node occurs. And have to calculate offsets for each higher level.

You might consider to print the tree turned by 90 degrees:

2

123

500

999

While the depth of a (somehow balanced) tree hardly was getting big, the width of a tree was growing exponentially.

Regards, Alex

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Here's a way that is close to what I described earlier. Call the function recursively, rising in the tree starting from the lowest level, until you get to the root (highest level). Using the same calculation of amount of spaces, you can then print the current level. Something like :

void printTree(TreeNode node, int level) {

printTree(node->father, level + 1);

// calculate amount of spaces based on current level (level starts counting from the bottom of the tree)

// print the current level, using node->sibling to get the next sibling

}

call by passing the leftmost node on the lowest level as the first parameter, and 0 as second parameter.