Question
A Sample Binary tree is shown below: The C++ code to create the previous tree is given below: #include using namespace std; //declaration for new
A Sample Binary tree is shown below:
The C++ code to create the previous tree is given below:
#include
using namespace std;
//declaration for new tree node
struct node {
int data;
struct node *left;
struct node *right;
};
//allocates new node
struct node* newNode(int data) {
// declare and allocate new node
struct node* node = new struct node();
node->data = data; // Assign data to this node
// Initialize left and right children as NULL
node->left = NULL;
node->right = NULL;
return(node);
}
int main()
{
node* root = newNode(0);
root->left = newNode(1);
root->right = newNode(2);
root->left->left = newNode(3);
root->left->right = newNode(4);
root->right->left = newNode(5);
root->right->right = newNode(6);
// The code for print should be here
delete root->right->right;
delete root->right->left;
delete root->left->right;
delete root->left->left;
delete root->right;
delete root->left;
delete root;
}
Complete the main code to print the data in the tree to the console as the following figure:
0 Left Right 1 2 Left Right Left Right 3 5 0 1 1 2 3 4 5 6Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started