Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Maybe fast/simple Question. I have a a Binary Tree Implemented already, Then I w

ID: 3540136 • Letter: M

Question

Maybe fast/simple Question. I have a a Binary Tree Implemented already, Then I was hoping to convert binary search tree into an array or at least print it out as if in an array. Where I am having trouble with is how to get the NULL/flags in there ''.


for example lets say I have a tree like:



10

/

6 12

/

1 8 15

4   



And I want it to print how its supposed to print. Like:


[10,6,12,1,8,,15,,4,,,,,,]

^Something Like this^ I don't know if I counted the NULL correctly.




Or Another Option on how i want to go about showing Visually my Tree is how to get the spacing correctly outputted like with the '/' and '' pointing to the keys from the parents:


10

/

6 12

/

1 8 15

4   


Here is something that I tried elaborating on code wise but im stuck:


void BreadthFirstTravseral(struct node* root)

{

queue<node*> q;

  

if (!root) {

return;

}

for (q.push(root); !q.empty(); q.pop()) {

const node * const temp_node = q.front();

cout<<temp_node->data << " ";

  

if (temp_node->left) {

q.push(temp_node->left);

}

if (temp_node->right) {

q.push(temp_node->right);

}

}

}




Any Kind of Help or Link and or advice and or example code would be very much appreciated.

Explanation / Answer

when you traverse a breadth, at each node check if the left or roght leaf are empty.

For example

if (temp->left == null) printf("");

else{

  

.... // add the node to the queue aftr printing the key

}

if (temp->left == null) printf("");

else{

.... // add the node to the queue after printing the key

}