. Considerthe following declaration: struct node { intzip_code; char name[20]; s
ID: 3618767 • Letter: #
Question
. Considerthe following declaration:
struct node {
intzip_code;
char name[20];
struct node*left;
struct node*right;
};
typedef struct node node_type;
Provided that zip_code is used as the key for creating a BinarySearch Tree (BST) of node_type nodes, write a Cfunction search that takes target along with the root of a BST andsearches for target in the tree. If target is found, your functionshould copy its associated name to answer and return 1; otherwisereturn 0. Note that answer is the third parameter defined infunction search. You may use any C library functions youchoose.
intsearch(int target, node_type *root, char *answer)
Explanation / Answer
//Hope this will helpyou.int search(int target, node_type *root, char*answer) { if(root==NULL) { answer[0] = 'N'; //notfound return 0; }else if(target==root->zip_code) { answer[0] = 'Y'; //found return 1; } else if(target <root->zip_code) { return search(target,root->left,answer); }else return search(target,root->right,answer); } int search(int target, node_type *root, char*answer) { if(root==NULL) { answer[0] = 'N'; //notfound return 0; }else if(target==root->zip_code) { answer[0] = 'Y'; //found return 1; } else if(target <root->zip_code) { return search(target,root->left,answer); }else return search(target,root->right,answer); }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.