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;
}
Step by stepSolved in 2 steps
- HELP Write C code that implements a soccer team as a linked list. 1. Each node in the linkedlist should be a member of the team and should contain the following information: What position they play whether they are the captain or not Their pay 2. Write a function that adds a new members to this linkedlist at the end of the list.arrow_forwardIn OCaml Programming Language, write a function: val prime_separator : int list -> int list * int list = <fun> that takes an integer list as an input, and returns a pair of integer lists where the prime numbers are in the first while the composite numbers are in the second returned list. #prime_separator [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11];; - : int list * int list = ([11; 7; 5; 3; 2], [10; 9; 8; 6; 4; 1])arrow_forwardWrite a user defined function named as “length_list” function that finds the length of a list of list_node_t nodes. This code should fit with the following code.Partial Code: #include <stdio.h> #include <stdlib.h> /* gives access to malloc */ #define SENT -1 typedef struct list_node_s { int digit; struct list_node_s *restp; } list_node_t; list_node_t *get_list(void); int main(void) { list_node_t *x;int y; x=get_list(); printf("%d\n",x->digit); printf("%d\n",x->restp->digit); printf("%d\n",x->restp->restp->digit); // your length_list function should be called from here printf("Length is: %d",y); } /* * Forms a linked list of an input list of integers * terminated by SENT */ list_node_t * get_list(void) { int data; list_node_t *ansp; [20]scanf("%d", &data); if (data == SENT) { ansp = NULL; } else { ansp = (list_node_t *)malloc(sizeof (list_node_t)); ansp->digit = data; ansp->restp = get_list(); } return (ansp);arrow_forward
- // FILL IN THE BLANKS (LINKED-LISTS CODE) (C++)#include<iostream>using namespace std; struct ________ {int data ;struct node *next; }; node *head = ________;node *createNode() { // allocate a memorynode __________;temp = new node ;return _______ ;} void insertNode(){node *temp, *traverse;int n;cout<< "Enter -1 to end "<<endl;cout<< "Enter the values to be added in list"<<endl;cin>>n; while(n!=-1){temp = createNode(); // allocate memorytemp->data = ________;temp->next = ________;if ( ___________ == NULL){head = _________;} else {traverse = ( );while (traverse->next != ________{traverse = traverse-> ___________;} traverse->next= temp;} cout<<"Enter the value to be added in the list"<<endl;cin>>n; }} void printlist(){node *traverse = head; // if head == NULLwhile (traverse != NULL) { cout<<traverse->data<<" ";traverse = traverse->next;}} int main(){int option; do{cout<<"\n =============== MAIN…arrow_forwardtypedef struct node_t node_t; struct node_t { }; int32_t value; node_t* next; node_t* insert (node_t* list, int32_t v, int32_t* flag); 3) As you may have noticed from MT1, Howie is lazy. He wants you to write the interface for the following implementation of function copy_to_linked_list. He really appreciates your help! /* copy_to_linked_list int32_t copy_to_linked_list (int32_t a[], int32_t size, node_t** list_p) { int32_t flag, i; for (i = 0; i < size; ++i) { *list_p = insert (*list_p, a[i], &flag); if (-1== flag) return -1; } return 0;arrow_forwardUse the following node definition for this problem.struct NodeInt32{int32_t value; NodeInt32* next;} Write a function which searches a non-empty linked list for a target value. Its exact signature should be: NodeInt32* find(NodeInt32* head, int32_t target); The function should return the first node whose value equals target. If the target is not found in the list, then the function should return NULL.arrow_forward
- in c++ Write a function that takes a queue with 20 integer values and deletes the elements thatare between 15 and 20. In other words, only the elements less than 15 or greater than 20remain in the queue. This means that you need to have an auxiliary queue to store theelements between 15 and 20 and restore them back to the original queue.arrow_forwardC++ The List class represents a linked list of dynamically allocated elements. The list has only one member variable head which is a pointer that leads to the first element. See the following code for the destructor to List. ~ List () { for (int i = 0; i <size (); i ++) { pop_back (); } } What problems does the destructor have? Select one or more options: 1. There are no parameters for the destructor. 2. The return value from pop_back (if any) is nerver handled. 3. The destructor will create a stack overflow. 4. The destructor will create dangling pointers. 5.The destructor will create memory leaks. 6.The destructor will create undefined behavior (equivalent to zero pointer exception). 7.The condition must be: i <size () - 1 8. There is at least one problem with the destructor, but none of the above.arrow_forwardIn OCaml Programming Language, write a function: val prime_separator : int list -> int list * int list = <fun> that takes an integer list as an input, and returns a pair of integer lists where the prime numbers are in the first while the composite numbers are in the second returned list. #prime_separator [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11];; - : int list * int list = ([11; 7; 5; 3; 2], [10; 9; 8; 6; 4; 1])arrow_forward
- c++ program please don't use any builtin functionarrow_forwardQ3 Racket Number Function Suppose that into the Racket evaluator we enter the definition: (define dozen 12) Give the value of the Racket expression: (number? dozen). Note that the value should be consistent with the Racket language. Enter your answer here Save Answer Q4 Racket Empty Lists Give two ways to write a Racket expression of which the value is a one-element list with the empty list as its element. Enter your answer here Save Answerarrow_forwardI was given the below code in c++, //used headesr#include<iostream>#include<string>using namespace std; //queueu structstruct queue{ //to store each order data string ord_name; string ord_dsc; float ord_total; float ord_tip; string ord_date; //next and previouse order record queue *next; queue *prev;}; //to hold first node address of linklistqueue *root; //function to check is queu empty or notbool is_Empty(){ if (root == NULL) return true; else return false;} //function to add ordervoid addOrder(){ queue *order = new queue(); cout << "\nName on order: "; cin.ignore(); getline(cin, order->ord_name); cout << "\nOrder description: "; getline(cin, order->ord_dsc); cout << "\nOrder Total (integer): "; cin >> order->ord_total; cout << "\nOrder Tip (integer): "; cin >> order->ord_tip; cout << "\nDate of order: "; cin.ignore(); getline(cin,…arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY