Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose you want a binary search tree where the key is a string ofarbitrary leng

ID: 3611283 • Letter: S

Question

Suppose you want a binary search tree where the key is a string ofarbitrary length and the keys are ordered from smallest to largestby sum of the characters in the string:

b) give the definition for a Cfunction that will determine whether a node that matches a givenkey exists in the binary search tree. (coderequested)

(you may assume that every node in the tree has a key with a uniquesum, but you may not assume that they key being searched for has asum that matches the sum of one of the nodes' keys only if the keyis the same as the node's key)

Explanation / Answer

#include#include#includestruct node{char *str;struct node *left,*right;}*root=NULL;void insert(char *p){struct node *ptr,*prev;if(root ==NULL){root= (struct node*)malloc(sizeof(struct node));root->str = (char *)malloc(strlen(p) +1);strcpy(root->str,p);root->left = root->right = NULL;}else{ptr = root;while(ptr !=NULL){prev =ptr;if(strlen(p) str))ptr = ptr->left; elseptr = ptr->right; }if(strlen(p) str)){prev->left = (struct node*)malloc(sizeof(struct node));ptr = prev->left;ptr->str = (char *)malloc(strlen(p) +1);strcpy(ptr->str,p);ptr->right = NULL;}else{prev->right = (struct node*)malloc(sizeof(struct node));ptr = prev->right;ptr->str = (char *)malloc(strlen(p) +1);strcpy(ptr->str,p);ptr->left = NULL;}}}void search(char *p){struct node *ptr;ptr = root;while(ptr !=NULL){if(strlen(p) == strlen(ptr->str)){printf("String found of this length = %s",ptr->str);break;}elseif(strlen(p) str))ptr = ptr->left; elseptr = ptr->right; }}int main(){insert("a");insert("aaaa");insert("bbb");insert("cc");search("bbb");printf(" ");system("pause");}
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote