Explain and diagrammatically illustrate the function int main() in the below program

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
100%

Explain and diagrammatically illustrate the function int main() in the below program.

#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() 
{
    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');
    
    display(rootNode);
    
    cout<<endl<<endl;

    Print_2D(rootNode,0);

    cout<<endl<<endl;
    
    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 2 steps with 1 images

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