Given Code: #include #include struct treeNode{     char dataItem;     struct treeNode *firstChild;     struct treeNode *nextSibling; }; /* Preorder Traversal preorder (v) visit(v) for each child w of v preorder(w)*/ void preorder(treeNode *currNode){     if(currNode != NULL){         std::cout << currNode->dataItem << " ";         preorder(currNode->firstChild);         preorder(currNode->nextSibling);     }     return; }          /* Postorder Traversal postorder (v) for each child w of v postOrder(w) visit(v)*/ void postorder(treeNode *currNode){     if(currNode != NULL){         preorder(currNode->firstChild);         preorder(currNode->nextSibling);         std::cout << currNode->dataItem << " ";     }     return; } /*Inorder Traversal if(v==NULL) return inOrder(v.left) visit(v) inOrder(v.right)*/ void inorder(treeNode *currNode){     if(currNode != NULL){         inorder(currNode->firstChild);         std::cout << currNode->dataItem << " ";         inorder(currNode->nextSibling);     }     return; } /*Print the tree*/ int main(){     treeNode *root, *nodeB, *nodeC, *nodeD, *nodeE, *nodeF, *nodeG, *nodeH, *nodeI, *nodeJ, *nodeK, *nodeL, *nodeM, *nodeN, *nodeP, *nodeQ;     root = new treeNode; nodeB = new treeNode; nodeC = new treeNode; nodeD = new treeNode; nodeE = new treeNode; nodeF = new treeNode;     nodeG = new treeNode; nodeH = new treeNode; nodeI = new treeNode; nodeJ = new treeNode; nodeK = new treeNode; nodeL = new treeNode;     nodeM = new treeNode; nodeN = new treeNode; nodeP = new treeNode; nodeQ = new treeNode;          root->dataItem = 'A';     root->firstChild = nodeB;     root->nextSibling = NULL;          nodeB->dataItem = 'B';     nodeB->firstChild = NULL;     nodeB->nextSibling = nodeC;          nodeC->dataItem = 'C';     nodeC->firstChild = NULL;     nodeC->nextSibling = nodeD;          nodeD->dataItem = 'D';     nodeD->firstChild = nodeH;     nodeD->nextSibling = nodeE;          nodeE->dataItem = 'E';     nodeE->firstChild = nodeI;     nodeE->nextSibling = nodeF;          nodeF->dataItem = 'F';     nodeF->firstChild = nodeK;     nodeF->nextSibling = nodeG;          nodeG->dataItem = 'G';     nodeG->firstChild = nodeN;     nodeG->nextSibling = NULL;          nodeH->dataItem = 'H';     nodeH->firstChild = NULL;     nodeH->nextSibling = NULL;          nodeI->dataItem = 'I';     nodeI->firstChild = NULL;     nodeI->nextSibling = nodeJ;          nodeJ->dataItem = 'J';     nodeJ->firstChild = nodeP;     nodeJ->nextSibling = NULL;          nodeK->dataItem = 'K';     nodeK->firstChild = NULL;     nodeK->nextSibling = nodeL;          nodeL->dataItem = 'L';     nodeL->firstChild = NULL;     nodeL->nextSibling = nodeM;          nodeM->dataItem = 'M';     nodeM->firstChild = NULL;     nodeM->nextSibling = NULL;          nodeN->dataItem = 'N';     nodeN->firstChild = NULL;     nodeN->nextSibling = NULL;          nodeP->dataItem = 'P';     nodeP->firstChild = NULL;     nodeP->nextSibling = nodeQ;          nodeQ->dataItem = 'Q';     nodeQ->firstChild = NULL;     nodeQ->nextSibling = NULL;              std::cout << "Pre-order: "; preorder(root);     std::cout << std::endl;     std::cout << "Post-order: "; postorder(root);     std::cout << std::endl;     std::cout << "In-order: "; inorder(root);          return 0;      } Question: 1. Create a function called findData that will take the following parameter: “CHOICE, KEY”. Choice will determine which traversal will be used to traverse the tree for the second parameter KEY. If the KEY is found, the value will be displayed such that: “{KEY} was found!”.

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%

Given Code:

#include<iostream>
#include<queue>

struct treeNode{
    char dataItem;
    struct treeNode *firstChild;
    struct treeNode *nextSibling;
};

/* Preorder Traversal
preorder (v)
visit(v)
for each child w of v
preorder(w)*/
void preorder(treeNode *currNode){
    if(currNode != NULL){
        std::cout << currNode->dataItem << " ";
        preorder(currNode->firstChild);
        preorder(currNode->nextSibling);
    }
    return;
}    
    

/* Postorder Traversal
postorder (v)
for each child w of v
postOrder(w)
visit(v)*/
void postorder(treeNode *currNode){
    if(currNode != NULL){
        preorder(currNode->firstChild);
        preorder(currNode->nextSibling);
        std::cout << currNode->dataItem << " ";
    }
    return;
}

/*Inorder Traversal
if(v==NULL) return
inOrder(v.left)
visit(v)
inOrder(v.right)*/
void inorder(treeNode *currNode){
    if(currNode != NULL){
        inorder(currNode->firstChild);
        std::cout << currNode->dataItem << " ";
        inorder(currNode->nextSibling);
    }
    return;
}

/*Print the tree*/

int main(){
    treeNode *root, *nodeB, *nodeC, *nodeD, *nodeE, *nodeF, *nodeG, *nodeH, *nodeI, *nodeJ, *nodeK, *nodeL, *nodeM, *nodeN, *nodeP, *nodeQ;
    root = new treeNode; nodeB = new treeNode; nodeC = new treeNode; nodeD = new treeNode; nodeE = new treeNode; nodeF = new treeNode;
    nodeG = new treeNode; nodeH = new treeNode; nodeI = new treeNode; nodeJ = new treeNode; nodeK = new treeNode; nodeL = new treeNode;
    nodeM = new treeNode; nodeN = new treeNode; nodeP = new treeNode; nodeQ = new treeNode;
    
    root->dataItem = 'A';
    root->firstChild = nodeB;
    root->nextSibling = NULL;
    
    nodeB->dataItem = 'B';
    nodeB->firstChild = NULL;
    nodeB->nextSibling = nodeC;
    
    nodeC->dataItem = 'C';
    nodeC->firstChild = NULL;
    nodeC->nextSibling = nodeD;
    
    nodeD->dataItem = 'D';
    nodeD->firstChild = nodeH;
    nodeD->nextSibling = nodeE;
    
    nodeE->dataItem = 'E';
    nodeE->firstChild = nodeI;
    nodeE->nextSibling = nodeF;
    
    nodeF->dataItem = 'F';
    nodeF->firstChild = nodeK;
    nodeF->nextSibling = nodeG;
    
    nodeG->dataItem = 'G';
    nodeG->firstChild = nodeN;
    nodeG->nextSibling = NULL;
    
    nodeH->dataItem = 'H';
    nodeH->firstChild = NULL;
    nodeH->nextSibling = NULL;
    
    nodeI->dataItem = 'I';
    nodeI->firstChild = NULL;
    nodeI->nextSibling = nodeJ;
    
    nodeJ->dataItem = 'J';
    nodeJ->firstChild = nodeP;
    nodeJ->nextSibling = NULL;
    
    nodeK->dataItem = 'K';
    nodeK->firstChild = NULL;
    nodeK->nextSibling = nodeL;
    
    nodeL->dataItem = 'L';
    nodeL->firstChild = NULL;
    nodeL->nextSibling = nodeM;
    
    nodeM->dataItem = 'M';
    nodeM->firstChild = NULL;
    nodeM->nextSibling = NULL;
    
    nodeN->dataItem = 'N';
    nodeN->firstChild = NULL;
    nodeN->nextSibling = NULL;
    
    nodeP->dataItem = 'P';
    nodeP->firstChild = NULL;
    nodeP->nextSibling = nodeQ;
    
    nodeQ->dataItem = 'Q';
    nodeQ->firstChild = NULL;
    nodeQ->nextSibling = NULL;
        
    std::cout << "Pre-order: "; preorder(root);
    std::cout << std::endl;
    std::cout << "Post-order: "; postorder(root);
    std::cout << std::endl;
    std::cout << "In-order: "; inorder(root);
    
    return 0;
    
}

Question:

1. Create a function called findData that will take the following parameter: “CHOICE, KEY”. Choice will determine which traversal will be used to traverse the tree for the second parameter KEY. If the KEY is found, the value will be displayed such that: “{KEY} was found!”.

 

2. Add a new leaf node called nodeM which will possess the data value of M. Make this node the child of nodeB. Perform the findData function you created in #3, what can you observe about the output?

Expert Solution
steps

Step by step

Solved in 2 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