Explain the below given program, How it works? #include using namespace std; struct node {     char data;      struct node *left;      struct node *right; }; struct node* newNode(char data) {          struct node *node = new struct node();           node->data = data;     node->left = NULL;      node->right = NULL;          return node;  } // find and display father node with its child nodes void Childs(node *root,char D) {     if(root==NULL)     {         return;     }     if(root->data==D)     {         if(root->left!=NULL&&root->right!=NULL)         {             cout<data<<" is the father of : ";             cout<left->data<<" and "<right->data<left==NULL&&root->right!=NULL)         {             cout<data<<" has only one child i.e. : ";             cout<right->data<left!=NULL&&root->right==NULL)         {             cout<data<<" has only one child i.e. : ";             cout<left->data<data<<" Has No Child\n";             return;         }         }     Childs(root->left,D);     Childs(root->right,D); } // Display all the tree nodes in one line void display(node* r) {     if(r==NULL)     {         return;     }     else     {         display(r->left);         cout<data<<"  ";         display(r->right);     } } // Display all the tree nodes in 2D form int COUNT=10; void Print_2D(node *Root, int space) {     if (Root == NULL)     {         return;     }     else     {         // Increment of Space between levels         space +=COUNT;              // First Process right child         Print_2D(Root->right, space);                  cout<<"\n";                  // Adding the spaces         for(int i=COUNT; idata<<"\n";         // Process left child         Print_2D(Root->left, space);     }       } int main()  {      /*create root node*/      struct node *rootNode = newNode('A');       rootNode->left = newNode('K');      rootNode->right = newNode('L');          rootNode->left->left = newNode('P');     rootNode->left->right = newNode('Q');          rootNode->right->left = newNode('B');     rootNode->right->right = newNode('C');     rootNode->left->left->left = newNode('J');          rootNode->right->left->left = newNode('X');     rootNode->right->left->right = newNode('Y');          rootNode->right->right->left = newNode('Z');          cout<<"=========================\n";     cout<<"=  Display in One Line  =\n";     cout<<"=========================\n";     display(rootNode);          cout<

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Explain the below given program, How it works?

#include <iostream>
using namespace std;

struct node
{
    char data; 
    struct node *left; 
    struct node *right;
};

struct node* newNode(char data)
{
    
    struct node *node = new struct node(); 
    
    node->data = data;

    node->left = NULL; 
    node->right = NULL;
    
    return node; 
}

// find and display father node with its child nodes
void Childs(node *root,char D)
{
    if(root==NULL)
    {
        return;
    }
    if(root->data==D)
    {
        if(root->left!=NULL&&root->right!=NULL)
        {
            cout<<root->data<<" is the father of : ";
            cout<<root->left->data<<" and "<<root->right->data<<endl;
            return;
        }
        else if(root->left==NULL&&root->right!=NULL)
        {
            cout<<root->data<<" has only one child i.e. : ";
            cout<<root->right->data<<endl;
            return;
        }
        else if(root->left!=NULL&&root->right==NULL)
        {
            cout<<root->data<<" has only one child i.e. : ";
            cout<<root->left->data<<endl;
        }
        else
        {
            cout<<root->data<<" Has No Child\n";
            return;
        }    
    }
    Childs(root->left,D);
    Childs(root->right,D);
}

// Display all the tree nodes in one line
void display(node* r)
{
    if(r==NULL)
    {
        return;
    }
    else
    {
        display(r->left);
        cout<<r->data<<"  ";
        display(r->right);
    }
}

// Display all the tree nodes in 2D form
int COUNT=10;
void Print_2D(node *Root, int space)
{
    if (Root == NULL)
    {
        return;
    }
    else
    {
        // Increment of Space between levels
        space +=COUNT;
    
        // First Process right child
        Print_2D(Root->right, space);
        
        cout<<"\n";
        
        // Adding the spaces
        for(int i=COUNT; i<space; i++)
        {
            cout<<" ";
        }
        //Display Current Root DATA
        cout<<Root->data<<"\n";

        // Process left child
        Print_2D(Root->left, space);
    }      
}


int main() 

    /*create root node*/ 
    struct node *rootNode = newNode('A');  
    rootNode->left = newNode('K'); 
    rootNode->right = newNode('L');
    
    rootNode->left->left = newNode('P');
    rootNode->left->right = newNode('Q');
    
    rootNode->right->left = newNode('B');
    rootNode->right->right = newNode('C');

    rootNode->left->left->left = newNode('J');
    
    rootNode->right->left->left = newNode('X');
    rootNode->right->left->right = newNode('Y');
    
    rootNode->right->right->left = newNode('Z');
    
    cout<<"=========================\n";
    cout<<"=  Display in One Line  =\n";
    cout<<"=========================\n";
    display(rootNode);
    
    cout<<endl<<endl;
    
    cout<<"=========================\n";
    cout<<"=     Display in 2D     =\n";
    cout<<"=========================\n";
    Print_2D(rootNode,0);

    cout<<endl<<endl;
    
    cout<<"================================\n";
    cout<<"=   Display Father of Childs   =\n";
    cout<<"================================\n\n";
    
    Childs(rootNode,'A');
    Childs(rootNode,'K');
    Childs(rootNode,'L');
    Childs(rootNode,'P');
    Childs(rootNode,'Q');
    Childs(rootNode,'B');
    Childs(rootNode,'C');

    cout<<endl<<endl;    
    
    return 0; 
}

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY