Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question

C++

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!”.

 

#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;

}

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