Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print binary tree nodes

I'm programming a BinaryTree project. I finished all (insert, delete, create, find) but one function, the printing operation. I'm supposed to print it like this:

5
46
X557
XXX6XXX9

Basically print all the nodes, but print an X if the node is empty. I've been trying to figure out how to do this and I keep hitting a dead end. Would this be something like inorder-traversal?? Thank you

like image 244
Richard Avatar asked Aug 27 '26 12:08

Richard


1 Answers

Use a Level-Order traversal (Breadth First Search) printing each node as you go through a level, with a newline at the end of each level.

You can find BFS pseudo-code here

like image 62
Dream Lane Avatar answered Aug 29 '26 19:08

Dream Lane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!